简体   繁体   English

你能帮我解决我的递归 function 吗?

[英]Can you help me with my recursive function?

I have this simple function that I am calculating correctly, but my output statements are off.我有这个简单的 function 计算正确,但我的 output 语句已关闭。 I have tried other places were the cout statement is commented, but not working.我已经尝试过其他地方,如果 cout 声明被评论,但没有工作。

int recursiveFunc(int n) {
    int val;    // value at nth sequence


    //cout << "(" << n << ") = " << val << endl;

    if (n == 1 ) {      // base case 1
        val = -1;
        // outputs before function call
    }
    else if (n == 2) {  // base case 2
        val = -1;
        // outputs before function call
    }
    else {      // recursive case
        //cout << "(" << n << ") = << val << endl;
        val = 2*(recursiveFunc(n-1) + recursiveFunc(n-2));
        cout << "(" << n << ") = " << val << endl;   // not sure where to put cout statement

    }

    return val;
}

I am looking for an output like (for example n = 5):我正在寻找 output 之类的(例如 n = 5):

(1) = -1
(2) = -1
(3) = -4
(4) = -10
(5) = -28

currently, my output looks like:目前,我的 output 看起来像:

(1) = -1
(2) = -1
(3) = -4
(4) = -10
(3) = -4    // here an nth term is displayed twice
(5) = -28

Move your cout statement to outside the function and it works fine.将您的cout语句移到 function 之外,它工作正常。

int recursiveFunc(int n) {
    int val;    // value at nth sequence

    if (n == 1) {      // base case 1
        val = -1;
    }
    else if (n == 2) {  // base case 2
        val = -1;
    }
    else {      // recursive case
        val = 2 * (recursiveFunc(n - 1) + recursiveFunc(n - 2));
    }

    return val;
}

int main() {
    for (int i = 1; i < 10; i++) {

        std::cout << "(" << i << ") = " << recursiveFunc(i) << endl;
    }
    return 0;
}

Output: Output:

(1) = -1
(2) = -1
(3) = -4
(4) = -10
(5) = -28
(6) = -76
(7) = -208
(8) = -568
(9) = -1552

Right off the top-of-my-head, a possible fix is to keep track of the highest n output so-far already and only output when a larger n is encountered.就在我的脑海中,一个可能的解决方法是跟踪迄今为止的最高n output,并且在遇到更大的n时仅跟踪 output。 (This is probably not the best solution, but I'm feeling too tired and lazy to think about alternatives) (这可能不是最好的解决方案,但我觉得太累了,懒得考虑替代方案)

Don't use a global variable for this ( mutable global state is an anti-pattern! ) - you'll need to pass it as another parameter.不要为此使用全局变量( 可变全局 state 是一种反模式! ) - 您需要将其作为另一个参数传递。


int output_n_and_val( int n, int val, int& biggestN ) {

    if( n > biggestN ) {
        cout << "(" << n << ") = " << val << endl;
        biggestN = n;
    }
    return val;
}

int recursiveFuncImpl( int n, int& biggestN ) {

    if( n == 1 ) {
        return output_n_and_val( n, -1, biggestN );
    }
    else if( n == 2 ) {
        return output_n_and_val( n, -1, biggestN );
    }
    else {
        int val =  2 * ( recursiveFuncImpl( n - 1, biggestN ) + recursiveFuncImpl( n - 2, biggestN ) );
        return output_n_and_val( n, val, biggestN );
    }
}

// Entrypoint function:
int recursiveFunc( int n ) {

    int biggestN = -1;
    return recursiveFuncImpl( n, biggestN );
}

int main()
{
    recursiveFunc( 10 );

    recursiveFunc( 5 );

    return 0;
}

The downside to this approach is that because of the n == 2 case-handling is encountered before n == 1 you'll never get the output (1) = -1 .这种方法的缺点是,由于在n == 1之前遇到了n == 2案例处理,您永远不会得到 output (1) = -1 Fixing that is an exercise left for the reader.解决这个问题是留给读者的练习。

you should use an array to store values that you have computed, for example您应该使用数组来存储您计算的值,例如

const int N = 1000000;
int values[N]; // remember to memset to 0 first

int recursiveFunc(int n) {
    if(n < 0) return 0;
    if(values[n] != 0) return values[n];

    int val;    // value at nth sequence

    if (n == 1) {      // base case 1
        val = -1;
    }
    else if (n == 2) {  // base case 2
        val = -1;
    }
    else {      // recursive case
        val = 2 * (recursiveFunc(n - 1) + recursiveFunc(n - 2));
    }

    return values[n] = val;
}

int main() {
    memset(values, 0, sizeof(values));
    for (int i = 1; i < 10; i++) {

        std::cout << "(" << i << ") = " << recursiveFunc(i) << endl;
    }
    return 0;
}

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

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