私は再帰を練習していて、問題に対する私の解決策はうまくいかないようです。 私は、数字の数字が昇順であるかどうかを判断する再帰的なコードを書こうとしています。ここに私のコードは次のとおりです。数字の数字が昇順であるかどうかを判断する再帰関数
#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;
}
}
So. 「うまくいかない」とはどういう意味ですか?そして、数字が「0」の場合、なぜあなたは終了していますか? –
通常、再帰関数はそれ自身を呼び出します。私はあなたの 'isAscending(...)でそれを見ない' – cleblanc
また、あなたはなぜ*結果を追加していますか? –