HDOJ 1250 Hat's Fibonacci 简单解题报告

    看起来超级简单的题,除了那2005位数。。。正好这几天都在做高精度乘法和加法的题,自己写了一个类。

    下面是我的AC代码:直接加的话内存会爆,所以我又特地写了一个函数,呵呵。。。7038的原因是strlen(t[7037])==2005.

#include <iostream>

using namespace std;



const int MAX_LEN=2010;

int temp[MAX_LEN];



class bigNumber

{

public:

    bigNumber()

    {

        memset(number,0,sizeof(number));

        number[0]='0';

    }

    bigNumber(char *str)

    {

        memcpy(number,str,strlen(str)+1);

    }

    bigNumber(int a)

    {

        int i=0;

        memset(number,0,sizeof(number));

        if(a==0)

            number[0]='0';

        else

        {

            while(a)

            {

                number[i++]=a%10+'0';

                a/=10;

            }

            _strrev(number);

        }

    }

    ~bigNumber()

    {

    }



    char* getNumber()

    {

        return number;

    }



    friend ostream& operator<<(ostream& output, bigNumber& t)

    {

        output<<t.getNumber();

        return output;

    }



    bigNumber& operator+(bigNumber& t)

    {

        bigNumber* a=strlen(this->getNumber())>strlen(t.getNumber())?this:&t;

        bigNumber* b=strlen(this->getNumber())>strlen(t.getNumber())?&t:this;

        bigNumber* c=new bigNumber();

        char *x=a->getNumber();

        char *y=b->getNumber();

        char *z=c->getNumber();

        int lenx=strlen(x);

        int leny=strlen(y);

        int i;

        int flag=0;

        for(i=0;i<leny;i++)

        {

            z[i]=(x[lenx-1-i]-'0')+(y[leny-1-i]-'0')+flag;

            flag=z[i]>9;

            z[i]=z[i]%10+'0';

        }

        for(;i<lenx;i++)

        {

            z[i]=(x[lenx-1-i]-'0')+flag;

            flag=z[i]>9;

            z[i]=z[i]%10+'0';

        }

        if(flag)

            z[i]='1';

        _strrev(z);

        return *c;

    }



    void addFourNumber(bigNumber& a,bigNumber& b,bigNumber& c,bigNumber& d)

    {

        int i,len,lenx,leny,lenp,lenq;

        int flag=0;



        char *x=a.getNumber();

        char *y=b.getNumber();

        char *p=c.getNumber();

        char *q=d.getNumber();

        char *z=this->getNumber();

        

        lenx=strlen(x);

        leny=strlen(y);

        lenp=strlen(p);

        lenq=strlen(q);

        len=lenx>leny?lenx:leny;

        len=lenp>len?lenp:len;

        len=lenq>len?lenq:len;



        for(i=0;i<len;i++)

        {

            z[i]=(i<lenx?x[lenx-1-i]-'0':0)+(i<leny?y[leny-1-i]-'0':0)+(i<lenq?q[lenq-1-i]-'0':0)+(i<lenp?p[lenp-1-i]-'0':0)+flag;

            flag=z[i]/10;

            z[i]=z[i]%10+'0';

        }



        if(flag)

            z[i]=flag+'0';

        _strrev(z);

    }



    bigNumber& operator*(bigNumber& t)

    {

        bigNumber* c=new bigNumber();

        char *x=this->getNumber();

        char *y=t.getNumber();

        char *z=c->getNumber();

        int lenx=strlen(x);

        int leny=strlen(y);

        int i,j;

        memset(temp,0,sizeof(temp));

        for(i=0;i<lenx;i++)

            for(j=0;j<leny;j++)

                temp[i+j]+=(x[lenx-1-i]-'0')*(y[leny-1-j]-'0');

        for(i=0;(i<lenx+leny-1) || temp[i]>9;i++)

        {

            if(temp[i]>9)

                temp[i+1]+=temp[i]/10;

            z[i]=temp[i]%10+'0';

        }

        z[i]=temp[i]+'0';

        while(z[i]=='0')

            z[i--]=0;

        _strrev(z);

        return *c;

    }



    bigNumber& operator=(bigNumber& t)

    {

        memcpy(number,t.getNumber(),strlen(t.getNumber())+1);

        return *this;

    }



    bigNumber& operator=(char *str)

    {

        memcpy(number,str,strlen(str)+1);

        return *this;

    }

private:

    char number[MAX_LEN];

};



bigNumber t[7038]={1,1,1,1,1};



int main()

{

    int i;

    for(i=5;i<7038;i++)

        t[i].addFourNumber(t[i-1],t[i-2],t[i-3],t[i-4]);

    while(cin>>i)

        cout<<t[i]<<endl;

}

 

你可能感兴趣的:(fibonacci)