码迷,mamicode.com
首页 > 其他好文 > 详细

斐波拉契数列:1、1、2、3、5、8、13、21……,编写函数,求数列的第n项F(n)(3<=n<=40)。输入n,输出F(n)。

时间:2018-02-26 18:59:09      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:3.2   编写   stdin   3.5   using   函数   pos   void   std   

#include<iostream>
#include<cstdio>
/*1.1.2.3.5.8.13.21.34.55*/
using namespace std;
int fibo(int n){
int ans[1000]={0,1,1};
for(int i=3;i<=n;i++){
ans[i]=ans[i-1]+ans[i-2];
}
return ans[n];
}
void read(){
freopen("fibo.in","r",stdin);
freopen("fibo.out","w",stdout);
}
int main(){
read();
int n;
cin>>n;
cout<<fibo(n);
}

斐波拉契数列:1、1、2、3、5、8、13、21……,编写函数,求数列的第n项F(n)(3<=n<=40)。输入n,输出F(n)。

标签:3.2   编写   stdin   3.5   using   函数   pos   void   std   

原文地址:https://www.cnblogs.com/1129-tangqiyuan/p/8474510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!