hdu Calculator 3546

来源:互联网 发布:微信抢红包软件安卓版 编辑:程序博客网 时间:2024/06/05 02:24

Calculator

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1400    Accepted Submission(s): 348


Problem Description
Golden Adobe is the most advanced computer for scientific computing in the world. Unfortunately, it broke down. Your task is to write a super computing software to take its place. The software contains 10 registers named from A to J. There are three operations on the registers:
1) Assignment: X=Y
2) Addition: X+=Y
3) Multiplication: X*=Y
Notice that X and Y are two registers, and they may be the same. 
Initially, all the registers are stored by integer 1. Your program should operate several operations and output the final result for 10 registers. You may assume that the length of each decimal number stored in the register is no longer than 5000.
 

Input
The input contains only one test case including several lines.
Each line contains an operation to be calculated.
The number of operations will no more than 300000.
 

Output
The output should contains exactly 10 lines, each line contains an integer denoting the decimal number in the register. See sample test case for further details.
 

Sample Input
A+=BA*=AA+=AB+=AC+=BD=B
 

Sample Output
89109111111
 

Author
HyperHexagon
 

Source

Calculator



看了题解=_=,有个”=“号的操作,如A=B,则之前像A+=B,A-=C,会出现冗余操作,去掉这些操作就可以了。如果不优化,高精度要写的非常好才能过,可我的太丑了。。,不管是即时进位还是最后进位都TLE了

不会用JAVA,只好用c++了

#include<cstdio>#include<iostream>#include<cstring>using namespace std;char A[10][100000];int ans[100000];char oper[300010][20];bool fun[300010],need[10];int bigint(char *s1,char *s2,char op)//s1,s2都是从高位到低位存的{    int ints1[100000]={0};    int ints2[100000]={0};    memset(ans,0,sizeof(ans));    int lenth1=strlen(s1);    int lenth2=strlen(s2);    int lenth;    for(int i=0;i<lenth1;i++)    {        ints1[i]=s1[lenth1-1-i]-'0';       // cout<<ints1[i];    }    //cout<<endl;    for(int i=0;i<lenth2;i++)    {        ints2[i]=s2[lenth2-1-i]-'0';       // cout<<ints2[i];    }    //cout<<endl;    if(op=='*')    {        lenth=lenth1+lenth2;        for(int i=0;i<lenth1;i++)            for(int j=0;j<lenth2;j++)            {                ans[i+j]+=ints1[i]*ints2[j];                /*                int k=0;                while(ans[i+j+k]>=10)                {                    ans[i+j+k+1]+=ans[i+j+k]/10;                    ans[i+j+k]=ans[i+j+k]%10;                    k=k+1;                }                */            }        for(int i=0;i<lenth;i++)            if(ans[i]>=10)            {                ans[i+1]+=ans[i]/10;                ans[i]=ans[i]%10;            }        if(ans[lenth-1]==0)            lenth-=1;    }    else    {        if(op=='+')        {            lenth=max(lenth1,lenth2);            for(int i=0;i<lenth1||i<lenth2;i++)            {                ans[i]+=ints1[i]+ints2[i];                /*                int k=0;                while(ans[i+k]>=10)                {                    ans[i+k+1]+=ans[i+k]/10;                    ans[i+k]=ans[i+k]%10;                    k=k+1;                }                */            }        for(int i=0;i<=lenth;i++)            if(ans[i]>=10)            {                ans[i+1]+=ans[i]/10;                ans[i]=ans[i]%10;            }            if(ans[lenth]!=0)                lenth+=1;        }    }    //cout<<lenth<<endl;    return lenth;}void innit(){    for(int i=0;i<10;i++)    {        strcpy(A[i],"1");        need[i]=true;    }}int main(){    innit();    char s[20];    int k=0;    while(scanf("%s",oper[k++])!=EOF)        ;    for(int i=k-1;i>=0;i--)    {        fun[i]=need[oper[i][0]-'A'];        if(oper[i][1]=='=')        {            need[oper[i][0]-'A']=false;            need[oper[i][2]-'A']=true;        }    }    for(int i=0;i<k;i++)    {        if(fun[i])        {            int lenth;            if(oper[i][1]!='=')            {            lenth=bigint(A[oper[i][0]-'A'],A[oper[i][3]-'A'],oper[i][1]);            for(int j=0;j<lenth;j++)                A[oper[i][0]-'A'][j]=ans[lenth-j-1]+'0';            A[oper[i][0]-'A'][lenth]='\0';            }            else            {                strcpy(A[oper[i][0]-'A'],A[oper[i][2]-'A']);            }        }    }    for(int i=0;i<10;i++)    {        cout<<A[i]<<endl;    }    return 0;}


0 0
原创粉丝点击