HDU 1002

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

string add(string sa,string sb)
{
     if(sa==""&&sb=="")return "0";
    if(sa=="") return sb;
    if(sb=="")return sa;
    string a=sa;
    string b=sb;
    if(a.length()<b.length())
    {
        a=sb;
        b=sa;
    }    
    int length_a=a.length(),length_b=b.length();
    int pos_a=length_a-1;
    int pos_b=length_b-1;
    for(;pos_b>=0;)
    {
        a[pos_a--]+=b[pos_b--]-'0';
    }
    //cout<<a<<endl;
    for(pos_a=length_a-1;pos_a>0;pos_a--)
    {
        if(a[pos_a]>'9')
        {
            a[pos_a]-=10;
            a[pos_a-1]+=1;
        }
    }
    if(a[0]>'9')
    {
        a[0]-=10;
        a='1'+a;
    }
    return a;

}

int main()
{
    string a,b;
    //ifstream cin("example.txt");
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        if(i!=1)
        {
            cout<<endl;
        }
        cin>>a>>b;
        cout<<"Case "<<i<<":"<<endl;
        cout<<a<<" + "<<b<<" = ";
        cout<<add(a,b);
        cout<<endl;
    }
    return 0;
}

1.将a设为比较大的字串时,若使用交换,则会出现RE。原因不明

2.一定要把判断其中一个串是否为空的语句写在函数最前,否则会TLE。

posted @ 2013-06-30 22:58  UmbrellaBeach  阅读(147)  评论(0)    收藏  举报