通过递归计数整数中的数字数量
Counting number of digits in an integer through recursion
我的代码如下:
/整数中的数字数
#include <iostream>
using namespace std;
int countNum(int n,int d){
if(n==0)
return d;
else
return (n/10,d++);
}
int main(){
int n;
int d;
cout<<"Enter number"<<endl;
cin>>n;
int x=countNum();
cout<<x;
return 0;
}
我无法弄清楚错误,它说:很少有参数来起作用`int countnum(int,int)'什么是问题?
,因为您声明了函数进行两个参数:
int countNum(int n,int d){
您没有传递:
int x = countNum();
您可能打算这样称呼它,而是:
int x = countNum(n, d);
另外:
return (n/10,d++);
应该是这样的:
return countNum(n/10,d++);
您也不初始化n
和d
变量:
int n;
int d;
最后,您根本不需要d
参数。这是一个更好的版本:
int countNum(int n){
return (n >= 10)
? 1 + countNum(n/10)
: 1;
}
这是工作示例。
int x=countNum();
呼叫者函数应将实际参数传递给调用函数。您已经定义了countNum(int, int)
的函数,这意味着它将接收两个INT作为来自调用函数的参数,因此呼叫者应将其传递给它们,而它们在您的情况下丢失了。这就是错误的原因太少。
您的代码在这里:
int x=countNum();
countnum需要与两个整数一起调用。例如
int x=countNum(n, d);
,因为您尚未将参数传递给countNum
函数。像int x=countNum(n,d);
假设这不是为了分配,有更好的方法可以做到这一点(只有几个示例):
转换为字符串
unsigned int count_digits(unsigned int n)
{
std::string sValue = std::to_string(n);
return sValue.length();
}
loop
unsigned int count_digits(unsigned int n)
{
unsigned int cnt = 1;
if (n > 0)
{
for (n = n/10; n > 0; n /= 10, ++cnt);
}
return cnt;
}
尾部末端递归
unsigned int count_digits(unsigned int n, unsigned int cnt = 1)
{
if (n < 10)
return cnt;
else
return count_digits(n / 10, cnt + 1);
}
注意:随着尾端递归优化的打开,您的编译器将为您转换为循环 - 防止呼叫堆栈的不必要的洪水。
将其更改为:
int x=countNum(n,0);
您不需要通过d
,您只需将0
作为种子传递。
还将countNum
更改为:
int countNum(int n,int d){
if(n==0)
return d;
else
return coutNum(n/10,d+1); // NOTE: This is the recursive bit!
}
#include <iostream>
using namespace std;
int countNum(int n,int d){
if(n<10)
return d;
else
return countNum(n/10, d+1);
}
int main(){
int n;
cout<<"Enter number"<<endl;
cin>>n;
int x=countNum(n, 1);
cout<<x;
return 0;
}
YouTube上的PMI(数学归纳原则)。它使递归非常容易。尝试以下操作:
#include<iostream>
using namespace std;
int count(int n){
//Base Case
if(n == 0){
return 0;
}
// Recursive Step
// We don't have to worry about the smaller steps.
// Don't go deep into the recursion!
int smallerInput = count(n/10);
//Calculation Step
return smallerInput + 1;
}
int main()
{
int num;
cout << "Enter the number: ";
cin >> num;
cout << "The number of digits are : " << count(num) << endl;
return 0;
}
您的功能编写不正确。例如,尚不清楚它为什么有两个参数或递归召集的位置。
我会以以下方式写下
int countNum( int n )
{
return 1 + ( ( n /= 10 ) ? countNum( n ) : 0 );
}
,甚至最好将其定义为
constexpr int countNum( int n )
{
return 1 + ( ( n / 10 ) ? countNum( n/10 ) : 0 );
}
相关文章:
- 检查输入是否不是整数或数字
- 如何找到大于整数的最小数字
- 在数字之间插入 + 或 - 符号以使其等于整数
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 整数区间(或 int 数组)中每个数字的出现次数
- 当我输入字符类型的数字时,为什么我无法获得整数?
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 分隔整数 (C++) 的数字
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- C++:从文件中读取字符串和整数,并获得最大数字
- 如何在C ++中替换整数的前两位数字?
- 如何计算整数的十进制数字?
- 如何计算整数链中使用了多少次数字?
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- R 中的算术在数字上比整数更快。这是怎么回事?
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 将大数字(10-12 位数字)存储在无序映射中<字符串,整数>
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- 如何从向量转换为<char>数字整数
- 如何将.txt文件中的未知数字整数保存到数组中