递归算法输出数列的前N个数

数列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         }

 

posted @ 2017-06-29 15:27  dyfisgod  阅读(561)  评论(0编辑  收藏  举报