繁体   English   中英

布尔递归函数返回错误的值

[英]Boolean recursive function returning the wrong value

这是一个递归函数,我确实要找出数字中给定的数字是否降序,所以我确定我的基数是正确的,因为我看到该函数的前几位确实返回false,但是因为后两位函数以降序排列,最后返回true。

我无法弄清楚如何保留该假值并将其返回给该函数的原始调用。

#include<iostream>
using namespace std;
bool dec(int n);

void main()
{
    cout << dec(1231);
}

bool dec(int n)
{
    bool res;

    if (n < 10)
        return true;
    else
    {
        res = dec(n / 10);
        if ((n / 10) % 10 > n % 10)
            return true;
        else
            return false;
    }
}

如果res也为true,则仅应return true true。 尝试

return (res && (n / 10) % 10 > n % 10);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM