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+ 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.