我正在练习递归,并且我对该问题的解决方案似乎不起作用。 我想写一个递归的代码,将确定一个数字的数字是否升序或不。这里是我的代码:确定数字的数字是否递增的递归函数
#include <stdio.h>
int isAscending(int num);
int main(){
int result;
result = isAscending(123);//Should print "The number is in ascending order!"
if (result == 0) {
printf("The number is in ascending order!\n");
}
else {
printf("The number is not in ascending order!\n");
}
}
int isAscending(int num) {
int new = num/10;
int result = 0;
if ((num % 10) == 0) {
return 0;
}
else if ((num % 10) > (new % 10)) {
result += isAscending(num/10);
return result;
}
else {
return 1;
}
}
所以。这是什么意思“似乎不起作用”?你为什么终止如果一个数字是'0'? –
通常递归函数自我调用。我在你的'isAscending(...)' – cleblanc
中看不到这些。另外,你为什么要添加结果? –