代码
/******************************************
/*    杭电ACM1002:A + B Problem II
/*        2010/10/1 
/*    VS2010 Win7
/*    zhuiqiuzhuoyue@hotmail.com
*****************************************
*/

#include 
<iostream>
#include 
<string>
using namespace std;

string Sum_calc(string s1,string s2)
{
    
int length;
    
    
if (s1.length()>=s2.length())
    {
        length
=s1.length();
        
string tmp(s1.length()-s2.length(),'0');
        s2
=tmp+s2;
    }
    
else
    {
        length
=s2.length();
        
string tmp(s2.length()-s1.length(),'0');
        s1
=tmp+s1;
    }
    
string sum(length,'0');
    
int carry = 0;    //carry
    for (int k=length-1;k!=-1;--k)
    {        
        sum[k]
=(s1[k]+s2[k]+carry-96)%10+48;
        carry
=(s1[k]+s2[k]+carry-96>9);
    }
    
if (carry)
        sum
='1'+sum;
    
    
return sum;

}

void main()
{
    
int T;    //The number of test cases;
    cin>>T;    
    
string *= new string[T];    //use the dynamic array when not knowing the dimension before compiling;
    string *= new string[T];
    
string *Sum = new string[T];
    
for (int i=0; i!=T; ++i)
    {
        cin
>>A[i]>>B[i];
        Sum[i] 
= Sum_calc(A[i], B[i]);
    }
    
for (i=0; i!=T; ++i){
        cout
<<"Case "<<i+1<<":\n"
        
<<A[i]<<" + "<<B[i]<<" = "<<Sum[i]<<endl;
        
if (i<T-1)
            cout
<<endl;
    }

    delete [] A;
    delete [] B;
    delete [] Sum;
}

 

 

posted on 2010-10-09 23:28  子桥  阅读(2044)  评论(0编辑  收藏  举报