HDU 1250 Hat's Fibonacci (递推大数加法string)
Posted dyhaohaoxuexi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1250 Hat's Fibonacci (递推大数加法string)相关的知识,希望对你有一定的参考价值。
Hat‘s Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14776 Accepted Submission(s): 4923
Problem Description
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.
Input
Each line will contain an integers. Process to end of file.
Output
For each case, output the result in a line.
Sample Input
100
Sample Output
4203968145672990846840663646 Note: No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
问题大意与分析
就是一个变种的斐波那契,在处理大数相加的时候我用了string 被bug绊了好久...明天好好学学string
#include<bits/stdc++.h> using namespace std; int n,i; string bigadd(string a,string b) { int jin=0,i; char ai,bi; string anss=a; int lena=a.size(); int lenb=b.size(); int lenmax=max(lena,lenb); int p=lena-1; int q=lenb-1; for(i=lenmax-1;i>=0;i--) { if(p<0) ai=‘0‘; else ai=a[p]; if(q<0) bi=‘0‘; else bi=b[q]; anss[i]=((ai-‘0‘+bi-‘0‘+jin)%10)+‘0‘; jin=(ai-‘0‘+bi-‘0‘+jin)/10; p--; q--; } if(jin) { char x=jin+‘0‘; anss=x+anss; } return anss; } /* int main() { while(scanf("%d",&n)!=EOF) { string a="1"; string b="1"; string c="1"; string d="1"; for(i=5;i<=n;i++) { string temp=d; d=bigadd(bigadd(a,b),bigadd(c,d)); a=b; b=c; c=temp; } cout<<a <<b <<c <<d<<endl; } } */ int main(){ string a[8008]; //我之前用了4个string 出错了 好像是和长度有关 a[1]="1"; a[2]="1"; a[3]="1"; a[4]="1"; for(i=5;i<8008;++i) a[i]=bigadd(bigadd(bigadd(a[i-1],a[i-2]),a[i-3]),a[i-4]); while(scanf("%d",&n)!=EOF) { cout<<a[n]<<endl; } return 0; }
以上是关于HDU 1250 Hat's Fibonacci (递推大数加法string)的主要内容,如果未能解决你的问题,请参考以下文章
HDU 1250 Hat's Fibonacci (递推大数加法string)
(二维数组 亿进制 或 滚动数组) Hat's Fibonacci hdu1250
HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)
HDU1250 Hat‘s Fibonacci (大数相加)(Java题解)