Fibonacci Numbers
时间限制: 1 Sec 内存限制: 128 MB提交: 13 解决: 2
[ 提交][ 状态][ 讨论版]
题目描述
A Fibonacci sequence is calculated by adding the prevIoUs two members of the sequence,with
the first two members being both 1.
f (1) = 1,f (2) = 1,f (n > 2) = f (n − 1) + f (n − 2)
Your task is to take a number as input,and print that fibonacci number.
输入
100
输出
354224848179261915075
样例输入
100
样例输出
354224848179261915075
提示
No generated fibonacci number in excess of 1000 digits will be in the test data,i.e. f (20) = 6765
和大数加法类似,还要注意数据的变换s1,s2,sum之间的变换,sum=s1+s2.然后字符处理
#include<iostream> #include<cstring> using namespace std; char sum[1200]; int s=0,m=0,n; int main() { cin>>n; string s1,s2; int a[10000],b[10000]; int he,i; s1="1"; s2="1"; for(m=2;m<n;m++) { memset(a,sizeof(a)); memset(b,sizeof(b)); a[0]=s1.length(); for(i=1;i<=a[0];i++) { a[i]=s1[a[0]-i]-'0'; } b[0]=s2.length(); for(i=1;i<=b[0];i++) { b[i]=s2[b[0]-i]-'0'; } he=(a[0]>b[0]?a[0]:b[0]); for(i=1;i<=he;i++) { a[i]=a[i]+b[i]; a[i+1]=a[i+1]+a[i]/10; a[i]=a[i]%10; } he++; while((a[he]==0)&&(he>1)) he--; for(i=he,s=0;i>=1;i--,s++) { sum[s]=a[i]+'0'; } s1=s2; s2=sum; } cout<<s2<<endl; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。