SUM中的递归函数

时间:2017-03-04 16:34:37

标签: c recursion

我尝试在C中递归递归函数,计算从x到max(包括)的数字之和。例如,sum(4,7)将计算4 + 5 + 6 + 7并返回值22.函数代码a必须是递归的,因此不允许使用任何传统的循环结构。

我有这个,我认为它应该有效,但我不完全确定为什么它不是

#include <stdio.h>

int main()
{
int sum (x, max);
   int total, y, x, max;
   if (x<max){
       y=x+1;
       total = x+sum(y,max);
       return total;
    return x;
   }


return 0;
}

感谢您提前提供任何帮助!

3 个答案:

答案 0 :(得分:1)

以下是一种可能的解决方案:

#include <stdio.h>

int sum_in_range(int a, int b){
    if(a != b){
        return sum_in_range(a+1,b)+a;
    }
    else{
        return b;
    }
}

int main(void) {
    // your code goes here
    printf("%d",sum_in_range(2,4));
    return 0;
}

答案 1 :(得分:1)

    #include<stdio.h>
#include<stdlib.h>
#include<string.h>

int sum(int s,int max)
{
    if(s==max)
    {
        return s;
    }
    else
    {       
        return(s+sum(s+1,max));
    }
}
int main()
{
    int r,s,max;
    printf("\n enter s and max");
    scanf("%d%d",&s,&max);
    r=sum(s,max);
    printf("%d",r);
}

答案 2 :(得分:0)

我发现你的代码有些错误。我不是专业人士,但这就是我的想法

我只是编辑你的代码。删除,添加并重新排列了一些东西* /

/*First, let's look at your code*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int sum(x, max);//I think what you want to do here is declare a function but instead declaring, you define it here because you added semicolon (;) 
    int total, x, y, max;

    if(x < max)
    {
        y = x + 1;
        total = x + sum(y, max); //you don't have a function declaration for "sum"

        return total;

        return x; //this will never return since you already "return the total before this"
    }

    return 0;
}


//////////////////////////////////////////////////////////////

/*And I think this is what you want to do*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 4, max = 6; //We declare this here for the "sum" function. This way "sum" function can recognize these variables
    int total = x; //be sure to set the total to x.

    //you can make a void function for this instead of "int". but either way, it can do the job.
    void sum(int y) //no need to pass a "max" because "max" is already recognized by the "sum" function since we declare the variables at the top of "main" function
    {
        if(x < max)//don't make it x <= max. Because of the argument "total = total + (x + 1)" on this function. If you do, the total will exceed.
        {
            //You can see here why we set the value of "total" to x.
            total = total + (x + 1);//And also, you can see why we didn't make the argument in if() statement like this: if(x <= max).
            x++;//increment "x" every loop

            //call the function again and pass the total until x == max.
            sum(total);
        }
    }

    //pass the x
    sum(x);

    //check the answer
    printf("The total is %d\n\n", total);

    return 0;
}

//////////////////////////////////////////////////////////////

/*It looks messy with comments*/
/*Here's the code looks like without a comment.It's pretty short code if you look remove the comments.. LOL..*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 4, max = 6;
    int total = x;

    void sum(int y) 
    {
        if(x < max)
        {
            total = total + (x + 1);
            x++;

            sum(total);
        }
    }

    sum(x);

    //check the answer
    printf("The total is %d\n\n", total);

    return 0;
}