C++输出斐波那契数列的几种方法
2013-06-24 08:14:45      个评论

[cpp]
#include<iostream>
using namespace std;
int main(){
int f1=0,f2=1,t,n=1;
cout<<"数列第1个："<<f1<<endl;
cout<<"数列第2个："<<f2<<endl;
for(n=3;n<=20;n++){
t=f2;
f2=f1+f2;
f1=t;
cout<<"数列第"<<n<<"个："<<f2<<endl;
}
cout<<endl;
return 0;

#include<iostream>
using namespace std;
int main(){
int f1=0,f2=1,t,n=1;
cout<<"数列第1个："<<f1<<endl;
cout<<"数列第2个："<<f2<<endl;
for(n=3;n<=20;n++){
t=f2;
f2=f1+f2;
f1=t;
cout<<"数列第"<<n<<"个："<<f2<<endl;
}
cout<<endl;
return 0;
}

[cpp]
#include<iostream>
using namespace std;
int main(){
int f1=0,f2=1,t,n=1;
cout<<"数列第一项："<<f1<<endl;
cout<<"数列第二项："<<f2<<endl;
for(n=2;n<10;n++){
f1=f1+f2;
cout<<"数列第"<<(2*n-1)<<"项："<<f1<<endl;
f2=f1+f2;
cout<<"数列第"<<(2*n)<<"项："<<f2<<endl;
}
cout<<endl;
return 0;

#include<iostream>
using namespace std;
int main(){
int f1=0,f2=1,t,n=1;
cout<<"数列第一项："<<f1<<endl;
cout<<"数列第二项："<<f2<<endl;
for(n=2;n<10;n++){
f1=f1+f2;
cout<<"数列第"<<(2*n-1)<<"项："<<f1<<endl;
f2=f1+f2;
cout<<"数列第"<<(2*n)<<"项："<<f2<<endl;
}
cout<<endl;
return 0;
}