zoukankan      html  css  js  c++  java
  • 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.

     


    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;       
    }

     

  • 相关阅读:
    免费试用Windows Azure云平台(无须提供信用卡)
    如何下载Ubuntu命令对应的源码
    Unix编程艺术——优化、工具、重用、可移植性、文档
    Choice of Xen Toolstacks
    [转]数据驱动编程之表驱动法
    获取Centos命令对应的源码
    Unix编程艺术——配置
    [转]vim ctags使用方法
    format and indent xml
    python得到本地网卡的IP
  • 原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/11330299.html
Copyright © 2011-2022 走看看