Swift程式:計算整數的位數


本教程將討論如何編寫一個Swift程式來計算整數的位數。

下面是一個演示:

假設我們輸入以下內容:

Number = 3454634

期望輸出如下:

Total count is 7

我們可以使用以下方法計算位數:

  • 迭代法

  • 遞迴法

迭代法

我們可以藉助迴圈(如if語句、while迴圈等)來計算給定整數中存在的總位數。這是最簡單也是成本最低的方法。

演算法

演算法解釋如下:

  • 步驟1 - 建立一個函式。

  • 步驟2 - 宣告兩個變數,值為 count = 0 和 num = n。

  • 步驟3 - 檢查給定數字是否等於0。如果是,則返回1。

  • 步驟4 - 使用條件 num > 0 執行While迴圈,透過將數字除以10並使count的值加1來計算總位數。

    num = num / 10

    count += 1

  • 步驟5 - 宣告一個變數,值為 val = 8776

  • 步驟6 - 使用一個引數呼叫函式並顯示最終輸出。

示例

以下程式演示瞭如何使用迭代法計算整數的位數。

import Foundation import Glibc func countNumbers(n: Int)->Int{ // Store the total count var count = 0 // Store the number var num = n // Checking the number for 0 // If yes print 1 if (num == 0){ return 1 } // Check for the positive number while (num > 0){ // Divide the num by 10 and store // the quotient into the num variable num = num / 10 // If the quotient is not zero, then update the // count by one. If the quotient is zero, // then stop the count count += 1 } // Return the final count return count } let val = 8776 print("The total digits present in \(val) are-", countNumbers(n: val))

輸出

The total digits present in 8776 are- 4

在上面的程式碼中,我們建立了一個名為countNumbers()的函式來計算給定整數中存在的總位數。在這個函式中,我們建立了兩個名為count = 0和num = n的變數,其中count用於儲存總數,num儲存整數。現在我們使用if語句來檢查給定數字是否等於零。如果給定數字等於零,則它將返回1。如果給定數字不等於零,則控制進入while迴圈:

while (num > 0){
   num = num / 10
   count += 1
}

上述程式碼的工作原理:

while (8776 > 0){
   num = 8776 / 10 = 877
   count = 0 + 1 = 1
}
while (877 > 0){
   num = 877 / 10 = 87
   count = 1 + 1 = 2
}
while (87 > 0){
   num = 87 / 10 = 8
   count = 2 + 1 = 3
}
while (8 > 0){
   num = 8 / 10 = 0
   count = 3 + 1 = 4
}

建立函式後,我們建立一個名為val = 8776的變數,並透過將val作為引數傳遞來呼叫countNumbers()函式,並顯示最終計數,即4。

遞迴法

我們還可以使用遞迴法來計算給定整數中的總位數。遞迴是一個函式呼叫自身來解決問題的過程。

演算法

演算法解釋如下:

  • 步驟1 - 宣告一個變數,值為 count = 0。

  • 步驟2 - 建立一個遞迴函式。

  • 步驟2 - 使用條件 n > 0 執行if迴圈。將count的值加1,並透過呼叫函式本身來返回給定數字中存在的總位數。

  • 步驟5 - 宣告一個變數,值為 val = 764434365

  • 步驟6 - 使用一個引數呼叫函式並顯示最終輸出。

示例

以下程式演示瞭如何使用遞迴法計算整數的位數。

import Foundation import Glibc var count : Int = 0 func countNumbers(n: Int)->Int{ // Checking for positive value if (n > 0) { // Increase the count by one count = count + 1 // Finding the quotient by calling the function itself return countNumbers(n: n / 10) } return count } let val = 7644 print("The total digits present in \(val) are-", countNumbers(n: val))

輸出

The total digits present in 7644 are- 4

在上面的程式碼中,首先,我們建立一個名為“count”的整數型別變數來儲存總計數。現在我們建立一個名為countNumber()的遞迴函式。此方法透過自我呼叫來計算總位數。現在我們使用val = 7644作為引數來呼叫此函式。因此,此函式的工作原理:

1st countNumbers(7644) function call:
if (7644 > 0) {
   count = 0 + 1 = 1
   return countNumbers(n: 7644 / 10)
   // Returning the quotient 764
}
2nd countNumbers(764) function call:
if (764 > 0) {
   count = 1 + 1 = 2
   return countNumbers(n: 764 / 10)
   // Returning the quotient 76
}
3rd countNumbers(76) function call:
if (76 > 0) {
   count = 2 + 1 = 3
   return countNumbers(n: 76 / 10)
   // Returning the quotient 7
}
4th countNumbers(7) function call:
if (7 > 0) {
   count = 3 + 1 = 4
   return countNumbers(n: 7 / 10)
   // Returning the quotient 0
}
5th countNumbers(0) function call:
// Here condition is false so the recursive call of the function is stop and return the count.
if (0 > 0)  {
   count = count + 1 
   return countNumbers(n: n / 10)
}
return count // Returning count = 4

顯示最終計數,即4。

更新於:2022年8月5日

2K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告