【大数加法】

WUXING

TimeLimit: 1 Second MemoryLimit: 32 Megabyte

Totalsubmit: 419 Accepted: 85

Description

There is a fashion that getting a total wuxing between Lolita and lolihunter.so,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

 

 

用数组来保存大数,比较基础的模拟把。注意处理好每位的进位问题,还有就是第一位是否进位即首位的处理方法问题。

 

 

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <memory.h>

using namespace std;

void func();

void output();

int tmp;

int num;

int flag;

int l1,l2;

const int maxn=1002;

char s1[maxn],s2[maxn];

int s[maxn];

int att;

int cas;

int cnt;

int main()

{

    //freopen("in.txt","r",stdin);

    num=1;

    cin >> cas;

    while(cas--)

    {

        memset(s,0,sizeof(s));

        att=0;

        flag=0;

        scanf("%s%s",s1,s2);

        l1=strlen(s1);

        l2=strlen(s2);

        if(l1<l2)

        {

            att=1;

            int t=l1;

            l1=l2;

            l2=t;

        }

        cnt=l1;

        l1--,l2--;

        for(int i=0;i<cnt;i++,l1--,l2--)

        {

            if(att==0)

            {



                if(l2>=0)

                s[l1]+=(s1[l1]-'0')+(s2[l2]-'0');

                else s[l1]+=(s1[l1]-'0');

            }

            else

            {

                 if(l2>=0)

                s[l1]+=(s2[l1]-'0')+(s1[l2]-'0');

                else s[l1]+=(s2[l1]-'0');

            }

            func();

        }

        output();

    }

    return 0;

}



void func()

{

    if(s[l1]>=10)

    {

        if(l1==0)

        {

            flag=s[l1]/10;

            s[l1]=s[l1]%10;

        }

        else

        {

            s[l1-1]++;

            s[l1]=s[l1]%10;

        }

    }

}



void output()

{

    printf("Case %d:\n",num++);

    printf("%s + %s = ",s1,s2);

    if(flag!=0)cout << flag;

    for(int i=0;i<cnt;i++)

    {

        cout<< s[i];

    }

    cout << endl;

    if(cas!=0)

    cout << endl;

}

 

你可能感兴趣的:(【大数加法】)