我需要编写一个递归函数,它将整数作为输入并返回输入数字的偶数位的串联,即我们应该删除奇数位。
例如:
创作者(1234); 返回数:24。
创作者(459876); 返回编号:486。
好吧,我陷入了死胡同。我不知道如何返回正确的数字。
那是我的代码,我什至不知道我的做法是否正确。
我的代码:
int Creator(int n)
{
if (n == 0)
return;
if ((n % 10) % 2 != 0)
Creator(n / 10);
return n % 10;
}
最佳答案
int Creator(int n){
if (n == 0)
return 0;//You must always return a value.
if (n % 2 != 0)
return Creator(n / 10);
return Creator(n / 10) * 10 + n % 10;//It is necessary to accumulate the call result.
}
关于c - 返回一个偶数递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44202145/