简体   繁体   English

C中的递归问题(数组中奇数的总和)

[英]Recursion question (Sum of odd numbers in array) in C

I have questions about a recursion function.我对递归 function 有疑问。 The program is supposed to compute the sum of n odd integers and the point is each time we find the sum, we must print the following:该程序应该计算 n 个奇数的总和,关键是每次我们找到总和时,我们必须打印以下内容:

For example, If the user gives 5 numbers (5,4,3,2,1), the list will be:例如,如果用户给出 5 个数字 (5,4,3,2,1),则列表将是:

5 4 3 2 1 5 4 3 2 1

Then it will print:然后它将打印:

5[0] (Yes) 4[5] (Yes) 3[5] (Yes) 2[8] (No) 1[8] (No) 5[0](是) 4[5](是) 3[5](是) 2[8](否) 1[8](否)

At first, it prints the number, after that in the [] the sum of the next odd numbers and at the end in parenthesis, a (Yes) or (No) if the number^2 in each node is greater from the sum of its next odd numbers.首先,它打印数字,然后在 [] 中打印下一个奇数的总和,在括号的末尾,如果每个节点中的 number^2 大于它的下一个奇数。 Like in the first one, 5>0 so it's a Yes.就像第一个一样,5>0,所以它是肯定的。

I've written this so far, I can't print it though, is there anything missing?我已经写到这里了,但是我不能打印它,有什么遗漏吗?

int checkSumOfOdds(struct list *ptr) {
    int k=0;

    if (ptr != NULL){
        k = checkSumOfOdds(p->next);
        if((ptr->next)^2 > k){
            printf(" %d [%d] (YES)",ptr->value, k);}
        else{
            printf(" %d [%d] (YES)",ptr->value, k);}

        k += ptr->value;
        return k;
    }

    return 0;
}

According to the problem statement, the values to be displayed are from the first input to the last.根据问题陈述,要显示的值是从第一个输入到最后一个输入。 '5' comes first, then '4'...先是“5”,然后是“4”……

This determines how the recursive function behaves.这决定了递归 function 的行为方式。 Does it call itself first, or does it at the end?它是先调用自己,还是最后调用自己? In your case the head values are to be dealt with first, thus the recursive call is to be done after the number is processed.在您的情况下,首先要处理头值,因此要在处理完数字后进行递归调用。 At the end.在最后。

Then your test if (ptr != NULL) should only affect the recursive call, not the whole body.那么你的测试if (ptr != NULL)应该只影响递归调用,而不是整个身体。

The ^ operator is exclusive OR in C... Probably not what you want (though it is possible to use it, but not like that). ^运算符在 C 中是异或...可能不是您想要的(尽管可以使用它,但不是那样)。 To test if a number is odd, simply check its first bit (with & 1 ), or check if N % 2 is > 0 .要测试一个数字是否为奇数,只需检查其第一位(使用& 1 ),或检查N % 2是否> 0

Since the number is processed and displayed early (because the recursive call is at the end), you can carry the sum with the calls.由于数字被提前处理和显示(因为递归调用在末尾),您可以将总和与调用一起携带。

I'm guessing the sum to be displayed should start with the current number (or, as an exercise, simply delay the summing...).我猜要显示的总和应该从当前数字开始(或者,作为练习,简单地延迟求和......)。

An example of code that should work应该工作的代码示例

int checkSumOfOdds(struct list *ptr, int sum) {
     int isodd = ptr->value & 1;
     if (isodd) sum += ptr->value;
     printf("%d[%d](%s) ", ptr->value, sum, isodd ? "Yes":"No");
     return ptr->next ? checkSumOfOdds(ptr->next, sum) : sum;
}

to be called被称为

 checkSumOfOdds(headOfList, 0);
 printf("\n");

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

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