English 中文(简体)
为什么在高峰期后恢复功能会下降?
原标题:Why does recursive function count downwards after peak?
  • 时间:2011-11-04 12:55:07
  •  标签:
  • c
  • recursion

with fear that I may overstep another question of mine (although this is a new problem alltogether) I still ask this question.

我有这一法典:

int blob_count(int y, int x, int gridCopy[][5], int sum){

    //Local vars
    int posX, posY;

    //Find the position 1 behind and 1 above the starting point, and start the loop there
    for(posX = -1;posX <=1; posX++){
        for(posY = -1; posY <= 1; posY++){
            if((y + posY) >= 0 && (x + posX) >= 0){
                if((y + posY) <= 5 && (x + posX) <= 5){
                    if(gridCopy[posY+y][posX+x] == 1){
                        //Set the starting point to 0 (so it wont get calculated again)
                        gridCopy[posY+y][posX+x] = 0;



                        y = posY+y;
                        x = posX+x;

                        sum++;
                        blob_count(y, x, gridCopy, sum);
                    }
                }
            }
        }
    }

    return sum;
}

问题是,每起休养费中,金额是1美元,是错失价值。 通过为每次休养手术提供印刷结果:

sum = 1  
sum = 2  
sum = ...  
sum = n  

然而,通过在休息室外(返回前的权利)印刷出钱,那是巨大的。

sum = n  
sum = ...  
sum = 2  
sum = 1  

return sum; // = 1

这一点显然是错误的,因为我想到的是全部数字,而不是最低数字。 返回是否对错位有价值? 我试图在休养呼吁(在休息期间)之后将其付诸行动。

最佳回答

Okay let s get rid of the extra bits and simplify your problem down to the essentials. You have:

int blob_count(int sum)
{
    sum++;

    if (sum < 10)
        blob_count(sum);

    return sum;
}

If you add printf("sum==%d ", sum) right before the return then it will be called first at the innermost recursion (where sum == 10), then it will return to the next level out where sum == 9, print that, return to sum == 8 and so on.

如果你在回程电话上打到blob_count(sum),那么,在你重新开标之前,你将重印这些数值,以便从sum=0, amount = 1开始。


如果您希望<代码>sum成为您的再次入侵最深的层次,那么你要么通过收益价值回馈:

int blob_count(int sum)
{
    sum++;

    if (sum < 10)
        sum = blob_count(sum);

    return sum;
}

或您可以通过点子通过,以便修改原有变量:

void blob_count(int* sum)
{
    *sum++;

    if (*sum < 10)
        blob_count(sum);

    return;
}

第一个可能是你寻求的解决办法。

问题回答

说了什么。 每个休养电话的现值都经过复印,复制件被转至休养电话。 如果你想改变功能中的物体,你必须将这些物体而不是物体本身当作点。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签