开发者

find the components

all. I'm making a easy function that can find the components of an integer

for example, if I input 4 6 4 3 2 2 1 1 ,

means i have to find the components of the number '4' (1st) from

the followed 6 (2nd) elements 4,3,2,2,1,1

so the expected output is like 4 3+1 2+2 2+1+1

and when you can't find any of the componets, print NONE.

My function is like :

int Addition(int command[], int target, int length ){
/* command : the input line
   length : the amount of elements need to be checked */ 

    int i = 0, j = 1, k = 0;
    int sum = command[0];
    int content[length];

    content[0] = command[0];

    while(j <= length){
        if(sum == target){
            for(i = 0; i < k; ++i){
                printf("%d+", content[i]);
            }
            printf("%d\n", content[i]);
            sum = command[0];
            k = 0;
            ++counter;
        }
        sum += comma开发者_StackOverflow社区nd[j];

        if(sum > target){
            sum -= command[j];
        }else{
            ++k;
            content[k] = command[j];
        }
        ++j;
    }
    if( length == 0 && counter == 0 )
        printf("NONE\n");
    else{
        return Addition(command+1, target, length-1);
    }
}

my problem is that i don't know how to set up the condition

to print NONE when there's no components ,and I can't avoid

printing the same answer. My output is like :

Sums of 4: 4 4 4 4 4 4 4 3+1 3+1 2+2 2+1+1 2+1+1 NONE

sorry for the long description, but can someone give me some hint to my problems ??

thank you!


Split the program into two phases. The first phase only finds solutions and doesn't print them; instead it stashes them in some way (I can think of at least three). The second phase removes duplicates and then prints the de-duped list, or NONE if there were none.


Since this is a recursive function, you should be placing a test to make sure that length != 0 right at the start of the function so that you don't cycle through an entire loop, accessing memory that shouldn't be accessed (because at that point command is pointing to the end of the memory segment, and you would have done operations like assigning the value of sum from the value of what command is pointing to).

Secondly, I don't see anywhere why your function needs to have a return type of int. It seems that void will work perfectly well, as you're not returning anything from the function (i.e., if you're at the end you simply are printing "NONE" and returning without any return value, so there is no need for the return-type to be int ... it should be void).

So, since you're returning nothing (i.e, void), the first line in your function before you do anything else should look something like:

if (length == 0)
{
    printf("NONE\n");
    return;
}

That way you catch where you need to stop the recursive calls. Then the last line of your function can be (you can omit the "return" before the recursive function call since you're returning void):

Addition(command+1, target, length-1); 

Finally, I believe you're printing all these repeat values because you're incrementing through your command array incorrectly (it seems overly complex). You should simply need a single for-loop or while-loop where you match the first variable in command against the rest of the values and check if their sums equal the target.

I've made a working copy of your function over at ideone, so you can check out what I'm talking about, especially the simplification of the loop.

Hope this helps,

Jason

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜