一种递归函数,用于确定数字的数字是否按升序排列

时间:2017-09-18 16:12:45

标签: c recursion

我正在练习递归,而我对这个问题的解决方案似乎并不奏效。 我试图编写一个递归代码来确定数字的数字是否按升序排列。这是我的代码:

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

6 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

请你试试下面的反复代码:

`

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;

答案 2 :(得分: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);
}

答案 3 :(得分:0)

此解决方案在失败时返回0,否则在成功时返回其他整数。当将0作为失败值返回时,似乎isDescending()更容易编写,但我相应地扭曲了它:

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

<强> TESTS

% ./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!
% 

不,它没有处理负数!它也没有处理&#39; 0&#39; 0正确作为输入 - 其他单位数字也没问题。

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

答案 4 :(得分:0)

您的测试不正确。对于具有单个数字的数字,您应该返回true;如果最后一个数字小于或等于前一个数字,则返回false,然后对其余数字进行递归:

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;
    }
}

答案 5 :(得分:-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);
    }
}
相关问题