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視窗中的輸出

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

這些數字是從陣列中指定和獲取的。

結論

我們在此提供各種方法來展示如何將整數的首位和末位數字相加。不同長度的不同整數被寫入陣列中。然後將不同的方法應用於這些整數。這些方法之間的差異主要在於查詢整數位數的方法或從這些整數中查詢首位和末位數字的方法。

更新於: 2023年7月10日

1K+ 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.