在c#中使用递归方法时遇到问题

时间:2012-03-22 22:59:20

标签: c#

c#中的递归方法有问题。编译时,它应该只显示给定int的所有数字的总sumUpToo,即 - 输入10 - 输出55(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0)

我无法在任何地方找到任何信息,所以如果某人有链接到可以教我如何通过它的网站,将不胜感激。

class Program
{
    static void Main(string[] args)
    {
        public static int sum(int x)
        {
        Console.WriteLine("num");
        x = Console.ReadLine();
        int sum = 0, i = 0;
        for (i = 0; i <= x; i++)

        {
            sum = sum + i;
        }
        Console.WriteLine("{0}", sum);
        }
        public static int recursivesum(int x)
        {
        if (x <= 1)
            Console.WriteLine("{0}", x);
        else
           (x + Recursivesum(x - 1));
        }

编辑*这是调整似乎现在工作正常,如果我没有弄错。感谢所有帮助

class Program
{
    static void Main(string[] args)
    {
        int x;
        Console.Write("Please enter an integer value to sum up to: ");
        x = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("The sum of all numbers up to and including {0} is {1}",x, recursivesum(x));
    }

    public static int sum(int x)
    {
        int sum = 0, i = 0;
        for (i = 0; i <= x; i++)
        {
        sum = sum + i;
        }
        return sum;
    }

    public static int recursivesum(int x)
    {
        if (x <= 1)
            return x;    
        else
            return x + recursivesum(x-1);

    }
}

}

2 个答案:

答案 0 :(得分:10)

初学者经常遇到递归函数问题。严格遵循这种模式,你不太可能出错:

ReturnType RecursiveFunction(ArgumentType argument)
{
    if (the argument is such that the problem can be trivially solved)
        return the trivial solution;
    // The problem could not be trivially solved. 
    // Break it down into one or more smaller problems.
    ArgumentType smallerProblemArgument = whatever;
    ReturnType smallerProblemSolution = RecursiveFunction(smallerProblemArgument);
    // We have solved the smaller problem.
    ReturnType bigProblemSolution = ...
    // ... use the smaller problem solution to solve the bigger problem...
    return bigProblemSolution;
}

所以在你的情况下:

public static int SumOfFirstNIntegers(int n)
{
    if (n <= 0) // trivial case
        return 0;
    // We have a harder problem to solve. Make the problem simpler:
    int smallerProblem = n-1;
    int smallerSolution = SumOfFirstNIntegers(smallerProblem);
    // We have solved the smaller problem. Use it to solve the big problem.
    int bigSolution = smallerSolution + n;
    return bigSolution;
}

答案 1 :(得分:7)

递归函数是一个自我调用的函数。您需要一个将退出函数的基本案例和一个递归的情况,其中函数使用修改的参数调用自身。你的功能应该是这样的:

public static int recursivesum(int x)
{
    if (x <= 1)
        return x;    // this is the base case
    else
        return x + recursivesum(x-1);
}

所以要使用这个函数,你可以这样简单地调用它:

recursivesum(10);

如果您遵循该函数的逻辑,您将看到这将返回10 + recursivesum(9)。 recursivesum(9)将返回9 + recursivesum(8)。所以现在我们有10 + 9 + recursivesum(8)。

这将持续到我们达到10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + recursivesum(1)的程度。现在,如果再次查看该函数,recursivesum(1)不会再次调用自身。相反,它只返回x。所以现在该功能将展开,您将得到您期望的结果。

关于递归的最后一点说明。递归可以是实现某些算法的一种非常优雅的方式,但它有危险。这个站点没有被称为堆栈溢出!