2012-03-22 6 views
1

C#で再帰メソッドに問題があります。コンパイルすると、指定されたintの総数の合計sumUpTooが表示されます。つまり、 - 入力10 - 出力55(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0)cで再帰メソッドに問題がある#

私はどこにでも情報を見つけることができないので、誰かが私にそれを通過する方法を教えることができるウェブサイトへのリンクを持っているなら、非常に感謝します。

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); 

    } 
} 

}

+4

はhttp://stackoverflow.com/questions/9831790/having-trouble-withを参照してください。 -recursive-methods-in-c-sharp;) –

+2

実際に*コンパイル*するコードを投稿してください - 現在あなたのメソッドは再帰呼び出しと呼ばれていますが、再帰呼び出しとしています。あなたの 'メイン'で。 –

+0

これは今でも出力されているものを教えてくれます。 –

答えて

8

再帰関数は自己それを呼び出す関数です。関数を終了する基本ケースと、関数が変更されたパラメータで自身を呼び出す再帰的ケースが必要です。あなたの関数は次のようになります。あなたは、関数のロジックに従った場合は10を返します

recursivesum(10); 

あなたがこれを表示されます。

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

だからあなたは、単純な、このようにそれを呼び出し、この機能を使用します+再帰(9)。 recursivesum(9)は9 + recursivesum(8)を返します。これで10 + 9 + recursivesum(8)になりました。

これは、10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 +再帰(1)のポイントに達するまで続きます。今度は関数を再度見ると、recursivesum(1)は自分自身を再び呼び出すことはありません。代わりにxを返すだけです。だから、関数が巻き戻され、あなたが期待する結果を得るでしょう。

再帰に関する最後のメモ。再帰はいくつかのアルゴリズムを実装する素晴らしいエレガントな方法ですが、それは危険です。このサイトはスタックオーバーフローと呼ばれるものではありません。

+0

ツタンカーメンに感謝します。今はもっと意味をなさない。 – Nicholas

+0

更新された機能付きの編集済み投稿、あなたの助けてくれてありがとうございます – Nicholas

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; 
} 
+0

あなたは正しいですが、私は再帰関数で多くの問題を抱えています。 :) – Nicholas

+0

おそらく、初心者が誘導によって証拠に問題があるのと同じ理由でおそらく?私は彼らが精神的な体操の同じ種類を行使すると思います。 –

+0

@GregD:それは本質的に同じものです。 –