花生闽南叫土豆吧 关注:4贴子:98
  • 6回复贴,共1

HDU_高精度题

只看楼主收藏回复

Problem C
Time Limit : 2000/2000ms (Java/Other)    Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 29    Accepted Submission(s) : 0
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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+=B
A*=A
A+=A
B+=A
C+=B
D=B
Sample Output
8
9
10
9
1
1
1
1
1
1



IP属地:福建1楼2010-08-24 21:59回复
    竟然TLE了,晕啊。。。。。
    #include<stdio.h>
    #include<string.h>
    int num[10][5000];
    char oper[10];
    int doit();
    int translate(char pre);
    int assignment(int num1,int num2);
    int add(int num1,int num2);
    int mul(int num1,int num2);
    #define Max(a,b) a>b?a:b;
    int main()
    {
         memset(num,0,sizeof(num));
         for(int j=0;j<10;j++)
         {
              num[j][1]=1;
              num[j][0]=1;
         }
         while(scanf("%s",&oper)!=EOF)
         {
             doit();
         }
         for(int i=0;i<10;i++)
         {
             for(int j=num[i][0];j>=1;j--)
             {
                 printf("%d",num[i][j]);
             }
             printf("\n");
         }
         return 0;
    }
    int doit()
    {
         if(oper[1]=='=')
         {
             assignment(translate(oper[0]),translate(oper[2]));
             return 0;
         }
         if(oper[1]=='+')
         {
             add(translate(oper[0]),translate(oper[3]));
             return 0;
         }
         if(oper[1]=='*')
         {
             mul(translate(oper[0]),translate(oper[3]));
             return 0;
         }
         return 0;
    }
    int translate(char pre)
    {
         switch(pre)
         {
         case 'A':return 0;
         case 'B':return 1;
         case 'C':return 2;
         case 'D':return 3;
         case 'E':return 4;
         case 'F':return 5;
         case 'G':return 6;
         case 'H':return 7;
         case 'I':return 8;
         case 'J':return 9;
         }
    }
    int assignment(int num1,int num2)
    {
         for(int i=1;i<=num[num2][0];i++)
         {
             num[num1][i]=num[num2][i];
         }
         num[num1][0]=num[num2][0];
    


    IP属地:福建2楼2010-08-24 22:00
    回复
           return 0;
      }
      int add(int num1,int num2)
      {
           int temp[5000];
           memset(temp,0,sizeof(temp));
           for(int i=1;i<=num[num1][0];i++)
           {
               temp[i]+=num[num1][i];
           }
           for(int j=1;j<=num[num2][0];j++)
           {
               temp[j]+=num[num2][j];
           }
           temp[0]=Max(num[num1][0],num[num2][0]);
           for(int k=1;k<=temp[0];k++)
           {
               temp[k+1]=temp[k+1]+temp[k] / 10;
               temp[k]=temp[k] % 10;
           }
           while(temp[temp[0]+1]>0)
           {
               temp[0]++;
               temp[temp[0]+1]=temp[temp[0]] / 10;
               temp[temp[0]]=temp[temp[0]] % 10;
           }
           for(int l=1;l<=temp[0];l++)
           {
               num[num1][l]=temp[l];
           }
           num[num1][0]=temp[0];
           return 0;
      }
      int mul(int num1,int num2)
      {
           int temp[5000];
           memset(temp,0,sizeof(temp));
           temp[0]=num[num1][0]+num[num2][0]-1;
           for(int i=1;i<=num[num1][0];i++)
           {
               for(int j=1;j<=num[num2][0];j++)
               {
                   temp[i+j-1]=temp[i+j-1]+num[num1][i]*num[num2][j];
               }
           }
           for(int k=1;k<=temp[0];k++)
           {
               temp[k+1]=temp[k+1]+temp[k] / 10;
               temp[k]=temp[k] % 10;
           }
           while(temp[temp[0]+1]>0)
           {
               temp[0]++;
               temp[temp[0]+1]=temp[temp[0]] / 10;
               temp[temp[0]]=temp[temp[0]] % 10;
           }
           for(int l=1;l<=temp[0];l++)
           {
               num[num1][l]=temp[l];
           }
           num[num1][0]=temp[0];
           return 0;
      }
      


      IP属地:福建3楼2010-08-24 22:00
      回复
        开到int64 竟然提示WA 晕死。。。。
        #include<stdio.h>
        #include<string.h>
        unsigned _int64 num[10][2000],count=0;
        char oper[10];
        unsigned _int64 doit();
        unsigned _int64 translate(char pre);
        unsigned _int64 assignment(unsigned _int64 num1,unsigned _int64 num2);
        unsigned _int64 add(unsigned _int64 num1,unsigned _int64 num2);
        unsigned _int64 mul(unsigned _int64 num1,unsigned _int64 num2);
        unsigned _int64 printzero(unsigned _int64 a);
        #define Max(a,b) a>b?a:b;
        unsigned _int64 main()
        {
             memset(num,0,sizeof(num));
             for(unsigned _int64 j=0;j<10;j++)
             {
                  num[j][1]=1;
                  num[j][0]=1;
             }
             while(scanf("%s",&oper)!=EOF)
             {
                 doit();
             }
             for(unsigned _int64 i=0;i<10;i++)
             {
                 for(unsigned _int64 j=num[i][0];j>=1;j--)
                 {
                     if(j!=num[i][0]) printzero(num[i][j]);
                     printf("%d",num[i][j]);
                 }
                 printf("\n");
             }
             return 0;
        }
        unsigned _int64 doit()
        {
             if(oper[1]=='=')
             {
                 assignment(translate(oper[0]),translate(oper[2]));
                 return 0;
             }
             if(oper[1]=='+')
             {
                 add(translate(oper[0]),translate(oper[3]));
                 return 0;
             }
             if(oper[1]=='*')
             {
                 mul(translate(oper[0]),translate(oper[3]));
                 return 0;
             }
             return 0;
        }
        unsigned _int64 translate(char pre)
        {
             switch(pre)
             {
             case 'A':return 0;
             case 'B':return 1;
             case 'C':return 2;
             case 'D':return 3;
             case 'E':return 4;
             case 'F':return 5;
             case 'G':return 6;
             case 'H':return 7;
             case 'I':return 8;
        


        IP属地:福建4楼2010-08-25 00:26
        回复
               case 'J':return 9;
               }
          }
          unsigned _int64 assignment(unsigned _int64 num1,unsigned _int64 num2)
          {
               for(unsigned _int64 i=1;i<=num[num2][0];i++)
               {
                   num[num1][i]=num[num2][i];
               }
               num[num1][0]=num[num2][0];
               return 0;
          }
          unsigned _int64 add(unsigned _int64 num1,unsigned _int64 num2)
          {
               unsigned _int64 temp[2000];
               memset(temp,0,sizeof(temp));
               for(unsigned _int64 i=1;i<=num[num1][0];i++)
               {
                   temp[i]+=num[num1][i];
               }
               for(unsigned _int64 j=1;j<=num[num2][0];j++)
               {
                   temp[j]+=num[num2][j];
               }
               temp[0]=Max(num[num1][0],num[num2][0]);
               for(unsigned _int64 k=1;k<=temp[0];k++)
               {
                   temp[k+1]=temp[k+1]+temp[k] / 1000000000;
                   temp[k]=temp[k] % 1000000000;
               }
               while(temp[temp[0]+1]>0)
               {
                   temp[0]++;
                   temp[temp[0]+1]=temp[temp[0]] / 1000000000;
                   temp[temp[0]]=temp[temp[0]] % 1000000000;
               }
               for(unsigned _int64 l=1;l<=temp[0];l++)
               {
                   num[num1][l]=temp[l];
               }
               num[num1][0]=temp[0];
               return 0;
          }
          unsigned _int64 mul(unsigned _int64 num1,unsigned _int64 num2)
          {
               unsigned _int64 temp[2000];
               memset(temp,0,sizeof(temp));
               temp[0]=num[num1][0]+num[num2][0]-1;
               for(unsigned _int64 i=1;i<=num[num1][0];i++)
               {
                   for(unsigned _int64 j=1;j<=num[num2][0];j++)
                   {
                       temp[i+j-1]=temp[i+j-1]+num[num1][i]*num[num2][j];
                   }
               }
               for(unsigned _int64 k=1;k<=temp[0];k++)
               {
                   temp[k+1]=temp[k+1]+temp[k] / 1000000000;
                   temp[k]=temp[k] % 1000000000;
          


          IP属地:福建5楼2010-08-25 00:26
          回复
                 }
                 while(temp[temp[0]+1]>0)
                 {
                     temp[0]++;
                     temp[temp[0]+1]=temp[temp[0]] / 1000000000;
                     temp[temp[0]]=temp[temp[0]] % 1000000000;
                 }
                 for(unsigned _int64 l=1;l<=temp[0];l++)
                 {
                     num[num1][l]=temp[l];
                 }
                 num[num1][0]=temp[0];
                 return 0;
            }
            unsigned _int64 printzero(unsigned _int64 a)
            {
                 if(a<10)
                 {
                     printf("00000000");
                     return 0;
                 }
                 if(a<100)
                 {
                     printf("0000000");
                     return 0;
                 }
                 if(a<1000)
                 {
                     printf("000000");
                     return 0;
                 }
                 if(a<10000)
                 {
                 printf("00000");
                     return 0;
                 }
                 if(a<100000)
                 {
                     printf("0000");
                     return 0;
                 }
                 if(a<1000000)
                 {
                     printf("000");
                     return 0;
                 }
                 if(a<10000000)
                 {
                     printf("00");
                     return 0;
                 }
                 if(a<100000000)
                 {
                     printf("0");
                     return 0;
                 }
                 return 0;
            }


            IP属地:福建6楼2010-08-25 00:26
            回复
              看的好晕


              7楼2010-10-07 01:10
              回复