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

HDU 1250 Hat's Fibonacci (递推、大数加法、string)

时间:2019-08-10 09:56:22      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:斐波那契   java   clu   问题   line   jin   you   mission   font   

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.

 


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)

标签:斐波那契   java   clu   问题   line   jin   you   mission   font   

原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/11330299.html

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