C++动态数组--用动态数组求斐波那契的前N项

一、代码

 1 ///////////////////
 2 /*动态分配数组
 3   int *p=new int[size];
 4   ....
 5   delete[]p;
 6 */
 7 //////用动态数组求斐波那契的前N项
 8 #include<iostream>
 9 using namespace std;
10 void main()
11 {
12     int n;
13     cout<<"请输入要输出的个数:"<<endl;
14     cin>>n;
15     cout<<""<<n<<"项为:"<<endl;
16     int *p=new int[n];//动态生成数组
17     if (p==0||p<=0)
18     {
19         return;
20     }
21     p[0]=0;
22     p[1]=1;
23     cout<<p[0]<<"\t";
24     cout<<p[1]<<"\t";
25     for (int i=2;i<n;i++)
26     {
27         p[i]=p[i-2]+p[i-1];
28         cout<<p[i]<<"\t";
29     }
30     cout<<endl;
31     delete []p;
32 }

 

二、运行

posted @ 2013-09-25 15:43  不懂编程的程序员  阅读(517)  评论(0编辑  收藏  举报