HDOJ 1133 Buy the Ticket 简单解题报告

    和前几天做的差不多的题目,1267那道,分析和那道差不多。要注意的是100!,longlong不够,继续使用以前的高精度类。

    题目还有一个比较坑人的地方,没有告诉你m>=n,需要处理m<n的情况,就是直接输出个0。。。下面是笔者AC的代码,仅供参考

#include <iostream>
using namespace std;

const int MAX_LEN=1000;
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(_int64 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;
    }

    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[101]={1,1};
bigNumber s[101][101]={1};

int main()
{
    int i,j,cas=1;
    for(i=2;i<=100;i++)
        t[i]=t[i-1]*bigNumber(i);

    for(i=1;i<=100;i++)
        for(j=0;j<=i;j++)
            if(j==0)
                s[i][0]=s[i-1][0];
            else if(i==j)
                s[i][i]=s[i][i-1];
            else
                s[i][j]=s[i][j-1]+s[i-1][j];
    while(cin>>i>>j &&(i||j))
    {
        cout<<"Test #"<<cas++<<":\n";
        if(i>=j)
            cout<<s[i][j]*t[i]*t[j]<<endl;
        else
            cout<<0<<endl;
    }
}

 

posted @ 2013-02-01 13:11  SF-_-  阅读(193)  评论(0编辑  收藏  举报