Python 查找整数的第一个和最后一个数字的和程序
在本文中,给定的任务是计算一个整数的第一个和最后一个数字的和。现在整数可以非常小,也可以是一个大数。因此,这些程序将有两部分。首先,我们需要找出整数的大小,然后从中获取第一个数字。第二部分将是获取给定整数的最后一个数字,这可以通过将数字除以十并找到余数来轻松完成。在本Python文章中,使用四个不同的示例,给出了计算一个整数的第一个和最后一个数字的方法。
在第一个示例中,使用重复除以10的方法来获取整数中的数字的数量。在第二个示例中,使用math.log10()函数来获取一个整数中的数字的数量。在第三个示例中,将整数转换为字符串以找到其长度,而在第四个示例中,首先将整数转换为字符串,然后使用索引值0和-1来提取第一个和最后一个数字。然后将这些第一个和最后一个数字相加以得到结果。
示例1:通过使用重复除法查找整数的第一个和最后一个数字的和,以查找数字的计数。
步骤
步骤1 - 编写一个countDigits函数来计算一个整数的数字数。
步骤2 - 使用重复除法的方法进行计算。
步骤3 - 现在将整数除以10 ** count,以获取第一个数字。
步骤4 - 通过除以10并获取余数来获取最后一个数字。
步骤5 - 将第一个数字和最后一个数字相加。
步骤6 - 对数组中给定的不同长度的数字执行此操作。
步骤7 - 打印输出结果
代码
listofnumbers =[881234,954321, 7178952, 20033, 459, 20069]
import math
#define function
def countDigits(thenumber):
count=0
while thenumber != 0:
thenumber //= 10
count += 1
return count
#Use for loop
for item in listofnumbers:
c=countDigits(item)
firstnum=math.floor(item/10**(c-1))
lastnum=item%10
total=firstnum+lastnum
print("\nThe Given number is: " , item)
print("The first digit is ", firstnum)
print("The last digit is ", lastnum)
print("The sum of first and the last digit is " , total)
示例1输出
在命令窗口中运行Python文件
打开命令提示符窗口。在命令提示符窗口中查看输出。
The Given number is: 881234
The first digit is 8
The last digit is 4
The sum of first and the last digit is 12
The Given number is: 954321
The first digit is 9
The last digit is 1
The sum of first and the last digit is 10
The Given number is: 7178952
The first digit is 7
The last digit is 2
The sum of first and the last digit is 9
The Given number is: 20033
The first digit is 2
The last digit is 3
The sum of first and the last digit is 5
The Given number is: 459
The first digit is 4
The last digit is 9
The sum of first and the last digit is 13
The Given number is: 20069
The first digit is 2
The last digit is 9
The sum of first and the last digit is 11
示例2:使用math.log10函数计算整数的第一位和最后一位的和,用于计算数字的数量。
步骤
步骤1 − 为了计算一个整数的位数,编写一个countDigits函数。
步骤2 − 在这个函数中使用公式math.floor(math.log10(thenumber) + 1)。
步骤3 − 现在将整数除以10**count获取第一个数字
步骤4 − 通过除以10并获取余数来获得最后一个数字。
步骤5 − 为了获得和,将第一个数字与最后一个数字相加。
步骤6 − 使用包含不同整数的数组对不同长度的数字进行操作。
步骤7 − 打印输出的和。
listofnumbers =[1234,54321, 678952, 200, 45, 10069]
#Import the required module
import math
#define function
def countDigits(thenumber):
return math.floor(math.log10(thenumber) + 1)
#Use for loop to iterate item
for item in listofnumbers:
c=countDigits(item)
firstnum=math.floor(item/10**(c-1))
lastnum=item%10
total=firstnum+lastnum
print("\nThe Given number is: " , item)
print("The first digit is ", firstnum)
print("The last digit is ", lastnum)
print("The sum of first and the last digit is " , total)
示例2输出
在命令窗口中运行python文件
打开cmd窗口。在cmd窗口中检查输出。
The Given number is: 1234
The first digit is 1
The last digit is 4
The sum of first and the last digit is 5
The Given number is: 54321
The first digit is 5
The last digit is 1
The sum of first and the last digit is 6
The Given number is: 678952
The first digit is 6
The last digit is 2
The sum of first and the last digit is 8
The Given number is: 200
The first digit is 2
The last digit is 0
The sum of first and the last digit is 2
The Given number is: 45
The first digit is 4
The last digit is 5
The sum of first and the last digit is 9
The Given number is: 10069
The first digit is 1
The last digit is 9
The sum of first and the last digit is 10
示例3: 通过将int转换为str并使用len函数来查找整数的第一个和最后一个数字的和
步骤
第1步 - 编写一个countDigits函数来计算整数的位数。
第2步 - 在此函数中,通过将int转换为str,然后获取其长度来计算位数。
第3步 - 现在将整数除以10**count,以获得第一个数字。
第4步 - 通过除以十再取余数来获取最后一个数字。
第5步 - 现在将第一个和最后一个数字相加。
第6步 - 对数组中的所有数字都执行此方法。
第7步 - 打印输出的和。
listofnumbers =[11234,554321, 6789521, 2004, 3455, 60069]
import math
def countDigits(thenumber):
snum=str(thenumber)
l=len(snum)
return l
for item in listofnumbers:
c=countDigits(item)
firstnum=math.floor(item/10**(c-1))
lastnum=item%10
total=firstnum+lastnum
print("\nThe Given number is: " , item)
print("The first digit is ", firstnum)
print("The last digit is ", lastnum)
print("The sum of first and the last digit is " , total)
示例3输出
在命令窗口中运行Python文件
打开命令窗口。在命令窗口中检查输出。
The Given number is: 11234
The first digit is 1
The last digit is 4
The sum of first and the last digit is 5
The Given number is: 554321
The first digit is 5
The last digit is 1
The sum of first and the last digit is 6
The Given number is: 6789521
The first digit is 6
The last digit is 1
The sum of first and the last digit is 7
The Given number is: 2004
The first digit is 2
The last digit is 4
The sum of first and the last digit is 6
The Given number is: 3455
The first digit is 3
The last digit is 5
The sum of first and the last digit is 8
The Given number is: 60069
The first digit is 6
The last digit is 9
The sum of first and the last digit is 15
图3:示例3的CMD窗口输出
示例4:通过使用字符串索引值来查找整数的第一个和最后一个数字的和
步骤
步骤1 −首先将整数转换为字符串。
步骤2 −使用索引0获取第一个数字,然后将其转换回整数。
步骤3 −使用索引-1获取最后一个数字,然后将其转换回整数。
步骤4 −将第一个数字和最后一个数字相加。
步骤5 −对给定在数组中的不同长度的数字进行此操作。
步骤6 −打印计算得出的总和。
listofnumbers =[12343,543210, 6789529, 9200, 45, 810069]
#Use for loop
for item in listofnumbers:
snum=str(item)
firstnum=int(snum[0])
lastnum=int(snum[-1])
total=firstnum+lastnum
print("\nThe Given number is: " , item)
print("The first digit is ", firstnum)
print("The last digit is ", lastnum)
print("The sum of first and the last digit is " , total)
示例4输出
在命令窗口中运行Python文件
打开命令提示符窗口。在命令提示符窗口中查看输出。
The Given number is: 12343
The first digit is 1
The last digit is 3
The sum of first and the last digit is 4
The Given number is: 543210
The first digit is 5
The last digit is 0
The sum of first and the last digit is 5
The Given number is: 6789529
The first digit is 6
The last digit is 9
The sum of first and the last digit is 15
The Given number is: 9200
The first digit is 9
The last digit is 0
The sum of first and the last digit is 9
The Given number is: 45
The first digit is 4
The last digit is 5
The sum of first and the last digit is 9
The Given number is: 810069
The first digit is 8
The last digit is 9
The sum of first and the last digit is 17
这些数字是从一个数组中指定和提取的。
结论
我们在这里列出了各种方法来展示如何添加整数的第一个和最后一个数字。不同长度的整数写在一个数组中。然后使用不同的方法来处理这些整数。这些方法的不同之处主要在于找到整数的位数或找到这些位数的第一个和最后一个数字的方法。