首頁  >  文章  >  後端開發  >  Python程式找到第一個和最後一個數字的和

Python程式找到第一個和最後一個數字的和

王林
王林轉載
2023-09-13 11:17:081142瀏覽

Python程式找到第一個和最後一個數字的和

在本文中,給定的任務是將整數的第一個數字和最後一個數字相加。現在整數可以非常小,也可以很大。因此,這些計劃將分為兩部分。首先,我們要找出這個整數有多大,然後從中得到第一個數字。第二部分是從給定的整數中獲取最後一個數字,這可以透過將數字除以十並找到餘數來輕鬆完成。在這篇 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 

這些數字是從一個陣列中指定和提取的。

結論

我們在這裡給出了各種方法來展示如何將整數的第一個數字和最後一個數字相加。不同長度的不同整數被寫入一個陣列中。然後對這些整數使用不同的方法。這些方法的差異主要在於尋找整數中位數的方法或從中查找第一個和最後一個數字的方法。

以上是Python程式找到第一個和最後一個數字的和的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除