2017-09-18 221 views
1

我正在练习递归,并且我对该问题的解决方案似乎不起作用。 我想写一个递归的代码,将确定一个数字的数字是否升序或不。这里是我的代码:确定数字的数字是否递增的递归函数

#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; 
    } 
} 
+1

所以。这是什么意思“似乎不起作用”?你为什么终止如果一个数字是'0'? –

+0

通常递归函数自我调用。我在你的'isAscending(...)' – cleblanc

+1

中看不到这些。另外,你为什么要添加结果? –

回答

0

能否请您尝试以下recurrsive代码:

`

boolean isascending(int num){ 

if(num == 0) return true; 
if(num%10>num%100) return isascending(num/10); 
else return false; 
}` 

,或者你可以使用while循环:

while(num>0){ 
if(num%10 > num%100){ 
    num = num/10; 
    continue; 
} return false; 
} return true; 
+0

OP将这个问题标记为'C'。你的程序不完整/不会按原样运行。 – babon

+0

两次都失败了各种测试。你可能想要审查/测试。 – chux

0

这将是更好的使用另一个参数来存储最后一位数字,该数字在当前迭代中将被“丢弃”。

于是我想出了下面的递归的逻辑:

  • 使用它存储的最后一位数字下降

    参数
  • 基本情况:如果数字为0,return 0true

  • 计算当前数字的最后一位数(数字%10)

  • 如果当前的最后一个数字大于最后一个数字下降:是这样的情况下,return 1false

  • 如果没有,在新的数字下降为当前的上次数字返回isAscendingRecursive()并把它作为下一次迭代最后一位数字

代码:

#include <stdlib.h> 
#include <stdio.h> 

int main(int argc, char** args){ 
    int num=0; 
    printf("Insert a number:\n"); 
    scanf("%d",&num); 
    if(isAscending(num)==0) 
     printf("Ascending\n"); 
    else 
     printf("Not ascending\n"); 
} 

int isAscending(int num){ 
    return isAscendingRecursive(num,9); 
} 

int isAscendingRecursive(int num, int lastDigit){ 
    if(num == 0) 
     return 0; 

    int temp = num%10; 
    if(temp > lastDigit) 
     return 1; 
    else 
     return isAscendingRecursive(num/10, temp); 
} 
-1

我固定我的代码和它的作品,感谢您的帮助!:

#include <stdio.h> 
int isAscending(int num); 
int main(){ 
    int result; 
    result = isAscending(2589);//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)) { 
     return isAscending(num/10); 
    } 
    else { 
     return 1 + isAscending(num/10); 
    } 
} 
+0

此答案不正确地报告'10,20,101等'是递增的。同样,'isAscending()'在从函数名称向后发出的升序时返回零。 'int result = 0;'没有任何价值。 – chux

+0

为什么你为'0'返回'0'? – chqrlie

2

下面是另一个(裸机)的方式去了解它。基本的想法是,如果我们有一位数字,我们返回肯定的,否则我们检查最右边的数字是否大于仅剩下的数字。我们为剩下的数字做这个。

#include <stdio.h> 

int isAsc(int i) 
{ 
    int rem = i % 10; // remainder 
    int quo = i/10; // quotient 

    if (rem == i) 
     return 1; 
    else if (rem <= (quo % 10)) 
     return 0; 
    else 
     return 1 && isAsc(quo); 
} 

int main(void) 
{ 
    int i = 123123; 
    if (isAsc(i)) 
     printf("%s\n", "Ascending"); 
    else 
     printf("%s\n", "Not ascending"); 

    return 0; 
} 
+0

请注意'isAsc(-123)'返回0.不清楚OP如何处理负值。 – chux

+0

可以简化'return 1 && isAsc(quo);' - >'return isAsc(quo);' – chux

+0

@chux是的,我不知道如何处理负值,因此“裸骨”:)。是的,它可以被简化,但有点想像所有的结果正在通过'&&'运行,并返回最终结果。 – babon

0

该解决方案在失败时返回0,否则返回成功的其他整数。看来,isDescending()更容易返回0作为失败值时写的,但我这个扭曲相应:

#include <stdio.h> 
#include <stdlib.h> 

int isAscending(int num) { 
    int quotient = num/10; 
    int remainder = num % 10; 

    if (quotient != 0) { 

     int result = isAscending(quotient); 

     if (result == 0 || result >= remainder) { 
      return 0; 
     } 
    } 

    return remainder; 
} 

int main(int argc, char **argv) { 
    if (isAscending(atoi(argv[1]))) { 
     printf("The number is in ascending order!\n"); 
    } else { 
     printf("The number is not in ascending order!\n"); 
    } 

    return 0; 
} 

试验

% ./a.out 123 
The number is in ascending order! 
% ./a.out 321 
The number is not in ascending order! 
% ./a.out 101 
The number is not in ascending order! 
% 

不,它不处理负数!它也不能正确处理'0'作为输入 - 其他单个数字的数字没有问题。

再次,isDescending()更容易编写但不幸的是,!isDescending()!= isAscending()

0

你的测试是不正确的。如果最后一位是小于或等于前一个递归的休息,你应该对数字返回true用一个单一的数字,假:

int isAscending(int num) { 
    int new = num/10; 

    if (new == 0) { 
     return 1; 
    } else 
    if (num % 10 <= new % 10) { 
     return 0; 
    } else { 
     return isAscending(new); 
    } 
} 

这种递归被称为尾递归你返回结果的递归调用。好的编译器会生成相当于此的迭代代码:

int isAscending(int num) { 
    for (;;) { 
     int new = num/10; 

     if (new == 0) { 
      return 1; 
     } 
     if (num % 10 <= new % 10) { 
      return 0; 
     } 
     num = new; 
    } 
}