首页 > 文章列表 > Python程序找到第一个和最后一个数字的和

Python程序找到第一个和最后一个数字的和

Python程序 数字 找到 第一个 最后一个
431 2023-08-30

在本文中,给定的任务是将整数的第一个数字和最后一个数字相加。现在整数可以非常小,也可以很大。因此,这些计划将分为两部分。首先,我们需要找到这个整数有多大,然后从中得到第一个数字。第二部分是从给定的整数中获取最后一个数字,这可以通过将数字除以十并找到余数来轻松完成。在这篇 Python 文章中,使用四个不同的示例,给出了将整数的第一位和最后一位相加的方法。

在第一个示例中,使用重复除以 10 的方法来获取整数的位数。在示例 2 中,math.log10() 用于获取整数的位数。在示例 3 中,将整数转换为字符串以查找其长度;在示例 4 中,首先将整数转换为字符串,然后使用索引值 0 和 -1 来获取第一个和最后一个数字。然后将第一个和最后一个数字相加即可得到结果。

Example 1: 通过使用重复除法来查找整数的首位和末位数字之和,以找到数字的数量。

算法

第 1 步 - 编写一个 countDigits 函数来计算整数的位数。

第二步 - 使用重复除法方法。

第三步 - 现在将整数除以10**count以获取第一个数字。

第四步 - 通过除以10并取余数来获取最后一个数字。

第 5 步 - 添加第一个和最后一个数字。

第 6 步 - 对数组中给出的不同长度的数字执行此操作。

第 7 步 - 打印输出。

Code

的中文翻译为:

代码

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 并得到余数,得到最后一个数字。

第五步 - 要得到总和,将第一个数和最后一个数相加。

第 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 函数来计算整数的位数。

第二步 - 在这个函数内部,对于计数,首先将int转换为str,然后获取其长度。

第 3 步 - 现在将整数除以 10**count 以获得第一个数字。

第四步 - 通过除以十并获取余数来获取最后一个数字。

第 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窗口中的输出

示例 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 

这些数字是从一个数组中指定和提取的。

结论

我们在这里给出了各种方法来展示如何将整数的第一个数字和最后一个数字相加。不同长度的不同整数被写入一个数组中。然后对这些整数使用不同的方法。这些方法的区别主要在于查找整数中位数的方法或从中查找第一个和最后一个数字的方法。