hdu1002大数相加

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 368505    Accepted Submission(s): 71806


Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
 
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 
Sample Input
2 1 2 112233445566778899 998877665544332211
 
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
 
 
解毒了终于,我好累,,
#include <iostream>
#include<string> 
using namespace std; 
int main(int argc, char** argv) {
string str1,str2;
   int t;cin>>t;
   int cont=0;
   while(t--)
   { 
   int p;
       int a[1005]={0};
       if(cont>0)
       cout<<endl;
       cont++;
        cout<<"Case"<<" "<<cont<<":"<<endl;
      cin>>str1>>str2;
    int len1=str1.size() ;
     int len2=str2.size() ;
     int i,j,k;
     j=len2-1;
     for( i=len1-1,k=0;i>=0;i--)
      {     
            p=str1[i]-2*'0'+str2[j];
             if(p+a[k]>=10)
             {
                 a[k]+=(p-10);
                 a[k+1]+=1;
             }
             else
           a[k]+=p;
         
             k++;
             j--;
            
             if(j<0)
             break;
      } 
       int g;
      
       if(i<j)
       {
           cout<<str1<<" "<<"+"<<" "<<str2<<" "<<"="<<" ";
           for(int h=len2-len1-1;h>=0;h--)
           {
               
               if(a[k]+(str2[h]-'0')>=10)
               {
              
                a[k]+=(str2[h]-'0');
                a[k]-=10;
                a[k+1]++; 
           }
           else
          a[k]+=(str2[h]-'0');
     
               k++;
       } 
           for(g=k;g>=0;g--)
          {
              if(a[g]!=0)
              break;
              }
    
            if(g==-1)
            cout<<"0";
            else
            for(int i=g;i>=0;i--)
            cout<<a[i];
            cout<<endl;
        }
       else
      {
           cout<<str1<<" "<<"+"<<" "<<str2<<" "<<"="<<" ";
           for(int h=len1-len2-1;h>=0;h--)
           {
               
               if(a[k]+(str1[h]-'0')>=10)
               {
              
                a[k]+=(str1[h]-'0');
                a[k]-=10;
                a[k+1]++; 
           }
           else
          a[k]+=(str1[h]-'0');
               k++;
            } 
           for(g=k;g>=0;g--)
          {
              if(a[g]!=0)
              break;
              }
    
            if(g==-1)
            cout<<"0";
             else
            for(int i=g;i>=0;i--)
            cout<<a[i];
            cout<<endl;
       }
}
    return 0;
}
原文地址:https://www.cnblogs.com/xiechenxi/p/7300833.html