奇怪的(对我来说)返回语句-can';在谷歌上找不到任何关于它的信息 我用递归法生成C++程序,它返回一个数字中最大的数字。 我让它按我的方式运行,但我找到了另一种选择: int cifmax(int n) { if(n <= 9) return n; return max(n % 10, cifmax(n / 10)); } int-cifmax(int-n){ 如果(n

奇怪的(对我来说)返回语句-can';在谷歌上找不到任何关于它的信息 我用递归法生成C++程序,它返回一个数字中最大的数字。 我让它按我的方式运行,但我找到了另一种选择: int cifmax(int n) { if(n <= 9) return n; return max(n % 10, cifmax(n / 10)); } int-cifmax(int-n){ 如果(n,c++,recursion,C++,Recursion,您可以认为此代码等同于以下更详细的版本: int cifmax(int n) { if (n <= 9){ return n; // trivial base case } else { int currentdigit = n % 10; // the least significant digit int otherdigits = n / 10; // other digits past the least signif

您可以认为此代码等同于以下更详细的版本:

int cifmax(int n) {
    if (n <= 9){
        return n; // trivial base case
    } else {
        int currentdigit = n % 10; // the least significant digit
        int otherdigits = n / 10; // other digits past the least significant
        int maxofotherdigits = cifmax(otherdigits); // make the recursive call

        // compute maximum of the two
        if (currentdigit > maxofotherdigits){
            return currentdigit;
        } else {
            return maxofotherdigits;
        }
    }
}
相当于:

return std::max(currentdigit, maxofotherdigits);

其中返回两个参数中较大的一个。

我假设它是调用
cifmax(n/10)
你在想什么?那么请对递归进行一些研究。递归通常是最好避免的。尽管学术界声称。我根本没有得到第二次回报。我从未见过这样的回报,我也不知道它是如何工作的。如果你知道它是关于什么的,你能给我一些文档吗?@Ron是的,我真的很难做到e掌握它,但我必须为学校做。所以请帮助我理解它:)@OctavianNiculescu您对哪个部分有问题?您查过
max
的功能吗?您知道
%
的功能吗?我没有意识到max是一个函数,因为我在学校没有使用它。我想这会返回max=n%10,然后调用cifmax,或者类似的东西。非常感谢。
return std::max(currentdigit, maxofotherdigits);