找到用户输入的每个4位数字,并使用递归进行计数

find the each number of 4 digit of user input and count it using recursion

本文关键字:递归 数字 输入 用户 4位      更新时间:2023-10-16

我有我的代码。这是关于递归的。我必须创建函数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,例如,它会计算一个数字中的每个数字。