找到用户输入的每个4位数字,并使用递归进行计数
find the each number of 4 digit of user input and count it using recursion
我有我的代码。这是关于递归的。我必须创建函数digitAppear( int findDigit, int value)
,其中value
是用户输入,findDigit
是从0到9的个位数。该功能读取用户输入并从用户输入中返回每个数字,并计算每个数字在用户输入中出现的次数。例如,如果我键入1234,那么输出说1出现1次,2出现1次等等(我希望我的解释很清楚)问题是只运行一次,只能返回1个值。
#include <iostream>
using namespace std;
int countOccurence(int, int);
int main()
{
int findDig;
int value;
int n = 0;
cout << "Please enter a positive number: " << endl;
cin >> value;
cout << "The value is " << value << endl;
while ((value < 0) || (value > 9999))
{
cout << "Invalid value. Please try again!" << endl;
cout << "Please enter a positive number: " << endl;
cin >> value; //you need this here, otherwise you're going to be stuck in an infinite loop after the first invalid entry
}
//process the value
for (findDig = 0; findDig < 9; findDig++)
{
cout << endl;
cout << cout << "the " << findDig << "appear in digit " << value << " is " << countOccurence(findDig, value) << " times" << endl;
}
//countOccurance(findDig, value);
//cout
}
int countOccurence(int findDig, int value)
{
int n = value;
while( n > 10 )
{
int a = n / 10; //eliminate the right most integer from the rest
int aa = n % 10; //separate the right most integer from the rest
int b = a / 10; //eliminate the second integer from the rest
int bb = a % 10; //separate the second integer from the rest
int c = b / 10; // eliminate the third integer from the rest
int cc = b % 10; //separate the third integer from the rest
for (findDig = 0; findDig < 9; findDig++)
{
int i = 0;
if (findDig == aa) // see if the findDigit value is equal to single digit of b;
{
i += 1;
} else
{
i += 0;
}
return i;
if (findDig == bb)
{
i += 1;
} else
{
i += 0;
}
return i;
if (findDig == cc)
{
i += 1;
} else
{
i += 0;
}
return il;
}
}
}
问题是我的函数countOccurence()
似乎不对。我想知道是否有办法做到这一点。我已经坚持了好几天了,我真的很感谢你的投入,谢谢你。
要使用递归,必须以不同的方式思考问题。
思考如何将递归融入函数的最简单方法是"剥离"每个数字的过程。
一个非常简单的方法是查看数字中的第一个/最后一个数字,计算它,然后根据数字的剩余部分调用它自己。
希望您能从中找出代码。
如果你的意思是函数digitalAppear本身必须是递归的,那么它可以像下面的演示程序中所示的那样看起来如下
#include <iostream>
#include <cstdlib>
size_t digitAppear( int findDigit, int value )
{
return ( std::abs( value ) % 10u == std::abs( findDigit ) ) +
( ( value /= 10 ) ? digitAppear( findDigit, value ) : 0 );
}
int main()
{
int i = 0;
for ( int x : { 0, 11111, 1234, 34343 } )
{
std::cout << "There are " << digitAppear( i, x )
<< " digit " << i
<< " in number " << x
<< std::endl;
++i;
}
return 0;
}
程序输出为
There are 1 digit 0 in number 0
There are 5 digit 1 in number 11111
There are 1 digit 2 in number 1234
There are 3 digit 3 in number 34343
当然,您可以随心所欲地重写函数main,例如,它会计算一个数字中的每个数字。
相关文章:
- 递归形成字符串中所有数字字符的中间和?
- 如何使用递归检查数字是否有重复数字?
- 仅使用加法/递归C++对数字进行平方
- 使用带有一个参数函数的递归找到数字的平方
- C++[递归]将一个数字写成2的升序之和
- 如何编写一个递归函数,以随机的方式混淆从0到6的数字
- 如何使用递归计算从 0 到给定数字的奇数?
- C++ 递归:返回数字的最小偶数
- Fibbonaci 递归代码返回错误值,始终返回下一个数字
- 递归推动POP POSTFIX计算器无法正确执行操作,只需将最后一个数字作为结果
- 递归函数有助于显示反向数字
- 递归地将给定字符串转换为它所表示的数字
- 优化递归问题以计算超级数字
- 递归函数用于计算 n 个数字之和的意外输出
- 编写一个函数,用递归函数检查数字是否是正方形
- 递归计算满足条件的值数并返回该数字
- 将目标数字拆分为一系列数字(递归任务)
- 如何使用递归按从 1 到 n 的升序打印数字,其中 n 在该函数的参数中给出
- 递归函数,用于计算数字中的位数
- 使用递归擦除字符串上的所有非数字