メインメニューを開く

差分

フィボナッチ数/C Sharp

587 バイト追加, 2012年12月6日 (木) 04:34
== 記述例 1 ==
本来ならば古風な記述例。一応、[[引数末尾再帰]] にはなっている。<source lang="csharp">using System; class AppMain { static int fib(int x が0以下の場合にエラー処理をしなければならないが省略してある。) { if (x <= 0) { return 0; } else if (x <= 2) { return 1; } else { return fib(x - 1) + fib(x - 2); } }  public static void Main(string[] args) { for (int x = 0; x < 16; x++) { Console.WriteLine("fib({0}) = {1}", x, fib(x) )); } }}</source> == 記述例 2 ==[[ラムダ式]]を使った記述方法。
<source lang="csharp">
using System;
public static void Main(string[] args)
{
for (int x = 0; x < 16; x++) { Console.WriteLine( "fib({0}) = {1}", x, fib(15x) ); }
}
}
匿名利用者