Python程序查找首位和末位数字之和
本文的任务是将一个整数的首位和末位数字相加。这个整数可以非常小,也可以非常大。因此,这些程序将包含两个部分。首先,我们需要找到整数的大小,然后从中获取首位数字。第二部分是从给定的整数中获取末位数字,这可以通过将数字除以10并找到余数来轻松完成。在这篇Python文章中,通过四个不同的示例,给出了添加整数首位和末位数字的方法。
在第一个示例中,使用反复除以10的方法来获取整数的位数。在示例2中,使用math.log10()来获取整数的位数。在示例3中,将整数转换为字符串以查找其长度,在示例4中,首先将整数转换为字符串,然后使用索引值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文件
打开cmd窗口。在cmd窗口中检查输出。
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 − 通过除以10然后获取余数来获取末位数字。
步骤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文件
打开cmd窗口。在cmd窗口中检查输出。
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窗口中的输出
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
示例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文件
打开cmd窗口。在cmd窗口中检查输出。
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
这些数字是从数组中指定和获取的。
结论
我们在此提供各种方法来展示如何将整数的首位和末位数字相加。不同长度的不同整数被写入数组中。然后将不同的方法应用于这些整数。这些方法之间的差异主要在于查找整数位数的方法或从这些整数中查找首位和末位数字的方法。