杭电1002大整数加法

杭电http://acm.hdu.edu.cn/showproblem.php?pid=1002

 

输入:

   
   
   
   
2 1 2 112233445566778899 998877665544332211


输出:

Case 1:

1 + 2 = 3

Case 2:

 112233445566778899 + 998877665544332211 = 1111111111111111110

#include <cstdlib>
#include <iostream>
#define Max 1002
using namespace std;
char aa[Max],bb[Max];
int a[Max],b[Max],r[Max];

int main(int argc, char *argv[])
{
    int t,i,j;
    int al,bl;
    cin>>t;
    for(int k =1;k<=t;k++){
    //输入数据并倒序放入数组 
    cin>>aa>>bb;
    al = strlen(aa);
    bl = strlen(bb);
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(i = al-1;i>=0;i--){
    a[al-1-i] = aa[i]-'0';
     }
    for(i = bl-1;i>=0;i--){
    b[bl-1-i] = bb[i]-'0';
     }         
     
    memset(r,0,sizeof(r));
    //计算 
    i =0;
    int flag =0;
    int len = al>bl?al:bl; 
    while(i<len){
     if(a[i]+b[i]+flag>9){ 
           r[i] = (a[i]+ b[i]+flag) %10;  
           flag = (a[i]+ b[i]+flag) /10;;        
                      }
      else{
           r[i] = a[i]+ b[i] +flag;
           flag =0;
           } 
        i++;                   
      }
      if(flag){
               r[i++] = flag;
               }
    cout<<"Case "<<k<<":"<<endl;
    cout<<aa<<" + "<<bb<<" = "; 
    for(--i;i>=0;i--){
                   cout<<r[i];
                   }
    cout<<endl;
      if(k!=t) 
     cout<<endl;          
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
/*
2
123 456
120 80
输出格式错误,最后一个case后面不要空行 
*/


你可能感兴趣的:(杭电1002大整数加法)