递归-波那挈数
{
?? ?/// <summary>
?? ?/// 費波那挈數 的摘要說明。
?? ?/// </summary>
?? ?public class 費波那挈數
?? ?{
?? ??? ?public static int 費波那挈數計算(int i)
?? ??? ?{
?? ??? ??? ?if(i<0)
?? ??? ??? ?{
?? ??? ??? ??? ?//Console.Write("此數字沒有費波那挈數!");
?? ??? ??? ??? ?return 0;
?? ??? ??? ?}
?? ??? ??? ?if(i==1||i==2)
?? ??? ??? ?{
?? ??? ??? ??? ?//Console.Write("此數的費波那挈數為:{0}",1);
?? ??? ??? ??? ?return 1;
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?return 費波那挈數計算(i-1)+費波那挈數計算(i-2);
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?public class Client
?? ?{
?? ??? ?[STAThread]
?? ??? ?public static void Main(string[] t)
?? ??? ?{
?? ??? ??? ?int i=Convert.ToInt32(Console.ReadLine());
?? ??? ??? ?Console.Write("{0}的費波那挈數為:{1}",i,費波那挈數.費波那挈數計算(i));
?? ??? ??? ?Console.ReadLine();
?? ??? ?}
?? ?}
}
?? ?/// <summary>
?? ?/// 費波那挈數 的摘要說明。
?? ?/// </summary>
?? ?public class 費波那挈數
?? ?{
?? ??? ?public static int 費波那挈數計算(int i)
?? ??? ?{
?? ??? ??? ?if(i<0)
?? ??? ??? ?{
?? ??? ??? ??? ?//Console.Write("此數字沒有費波那挈數!");
?? ??? ??? ??? ?return 0;
?? ??? ??? ?}
?? ??? ??? ?if(i==1||i==2)
?? ??? ??? ?{
?? ??? ??? ??? ?//Console.Write("此數的費波那挈數為:{0}",1);
?? ??? ??? ??? ?return 1;
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?return 費波那挈數計算(i-1)+費波那挈數計算(i-2);
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?public class Client
?? ?{
?? ??? ?[STAThread]
?? ??? ?public static void Main(string[] t)
?? ??? ?{
?? ??? ??? ?int i=Convert.ToInt32(Console.ReadLine());
?? ??? ??? ?Console.Write("{0}的費波那挈數為:{1}",i,費波那挈數.費波那挈數計算(i));
?? ??? ??? ?Console.ReadLine();
?? ??? ?}
?? ?}
}
轉載于:https://www.cnblogs.com/Tim-Seven/articles/1075505.html
總結
- 上一篇: WinAPI: waveInMessag
- 下一篇: 保险都是骗人的吗?