1. 程式人生 > >遞歸算法輸出數列的前N個數

遞歸算法輸出數列的前N個數

pan oid string col 遞歸算法 str 算法 () blog

數列1,1,1,3,5,9,17,31,57,105……N大於3時,第N個數為前三個數之和。

1   for (int i = 0; i < 10; i++)
2             {
3                 listint.Add(1);
4             }
5             test3(10);
6             test3();
 1  List<int> listint = new List<int>(); 
 2         int test3(int n)
 3         {
 4             int
result = 1; 5 if (n > 3) 6 { 7 result = test3(n - 1) + test3(n - 2) + test3(n - 3); 8 listint[n-1]=result; 9 } 10 else 11 { 12 result = 1; 13 } 14 15 return
result; 16 } 17 18 void test3() 19 { 20 foreach (int a in listint) 21 { 22 MessageBox.Show(a.ToString()); 23 } 24 }

遞歸算法輸出數列的前N個數