hdu1002

题目意思:计算两个大整数的和

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


AC代码:

#include<iostream>
#include<string>
using namespace std;
string add(string s1,string s2){//字符串模拟大数加法,
    string s;
    int len1,len2;
    len1=s1.size()-1; len2=s2.size()-1;
    int i=0,flag=0;
    while(len1>-1&&len2>-1){
        int sum=flag+(s1[len1--]-'0')+(s2[len2--]-'0');
        s+=char ((sum)%10+'0');
        flag=sum/10;
    }
    while(len1>-1){
        int sum=flag+(s1[len1--]-'0');
        s+=char ((sum)%10+'0');
        flag=sum/10;
    }
    while(len2>-1){
        int sum=flag+(s2[len2--]-'0');
        s+=char ((sum)%10+'0');
        flag=sum/10;
    }
    if(flag) s+=char ('0'+flag);
    //cout<<s<<endl;
    for(int i=0;i<s.size()/2;i++){
        char c=s[i];
        s[i]=s[s.size()-i-1];
        s[s.size()-i-1]=c;
    }
    return s;
}
int main()
{
    int t,k=0;
    string a,b;
    cin>>t;
    while(t--){
        cin>>a>>b;
        cout<<"Case "<<++k<<":"<<endl;
        cout<<a<<" + "<<b<<" = "<<add(a,b)<<endl;
        if(t) cout<<endl;
    }
    return 0;
}

你可能感兴趣的:(hdu1002)