A + B Problem II 高精度



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
 
昨天跟学长谈到学妹问他的一个问题,我了解后就想做做,本以为是个很简单的问题(确实是个很简单的问题)
没想到却发费我挺长时间的,其实原因就是自己以为很简单却没想好思路,就直接开始码代码了,
这是个错误的option,so
以后得先想好思路,在写代码,高级码农就是这样做的
看看我的凌乱的代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int T,t,t1,t2,flag=0,flag1=0,cnt=1,k;
    string s1,s2,s;
    cin>>T;
    k=T;
    while(T--)
    {
        flag=0;flag1=0;
        string s2="";
        cin>>s>>s1;
        t=s.size();
        t1=s1.size();
        t--;
        t1--;
        int    t2=t;
        int    t3=t1;
        while(1)
        {
            if((s[t]+s1[t1]-'0'-'0'+flag1)>9)
            flag=1;
            else
            flag=0;
            if(flag==1)
            {
            s2=char(s[t]+s1[t1]-'0'-10+flag1)+s2;
            flag1=1;
            //cout<<s2<<"yyyyyyyy"<<endl;
            }
            else
            {
            s2=char(s[t]+s1[t1]-'0'+flag1)+s2;
            flag1=0;
            //cout<<s2<<"yyyyyyyy"<<endl;
            }
            t--;
            t1--;
            if(t==-1)
            {
                for(int i=t3-t2-1;i>=0;i--)
                {
                    if(flag==1)
                    {
                        if(s1[i]+flag-'0'>9)
                        {
                        s2=char(s1[i]-10+flag)+s2;
                        flag=1;
                        }    
                        else
                        {
                        s2=char(s1[i]+flag)+s2;
                        flag=0;
                        }
                    }
                    else
                    s2=char(s1[i])+s2;
                }    
                break;
            }
            if(t1==-1)
            {
                for(int i=t2-t3-1;i>=0;i--)
                {
                    if(flag==1)
                    {
                        if(s[i]+flag-'0'>9)
                        {
                        s2=char(s[i]-10+flag)+s2;
                        flag=1;
                        }    
                        else
                        {
                        s2=char(s[i]+flag)+s2;
                        flag=0;
                        }
                    }
                    else
                    s2=char(s[i])+s2;
                }    
                break;
            }
        }
        printf("Case %d:
",cnt);
        cout<<s<<" + "<<s1<<" = ";
        if(flag==1)
        cout<<"1";
        cout<<s2<<endl;
        if(cnt!=k)
        cout<<endl;
        cnt++;
    }
    return 0;
}
如果你够坚强够勇敢,你就能驾驭他们
原文地址:https://www.cnblogs.com/liuzhaojun/p/11290094.html