hdoj 1002 A+B

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

注意点:
1:注意进位
2:注意两数相加时加完后长度要凑足,不然会缺了长度较长的那个数的最高的几位。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<list>
#include<iterator>
#include<stack>
#include <queue>
#include <cstdio>
#include<algorithm>
using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
#define e 2.718281828459
#define INF 0x7fffffff
#pragma warning(disable:4996)
#define sf scanf
#define pf printf
#define sf2d(x,y) scanf("%d %d",&(x),&(y))
#define sfd(x) scanf("%d",&x)
#define sff(p) scanf("%lf",&p)
#define pfd(x) printf("%d\n",x)
#define mset(x,b) memset((x),b,sizeof(x))
const double pi = acos(-1.0);

int main(void) {
    int a[1001];
    int b[1001];
    int c[1001];
    int n;
    sfd(n);
    int Case = 1;
    while (n--) {
        int i=1, j=1;
        char temp[1001];

        scanf("%s", temp);
        int len = strlen(temp);
        for (int p = 0; p < len; p++) {
            a[i++] = temp[p] - '0';
        }
        scanf("%s", temp);
        len = strlen(temp);
        for (int p = 0; p < len; p++) {
            b[j++] = temp[p] - '0';
        }
        j--, i--;
        mset(c, 0);

        int k = i > j ? i : j;
        int lena = i;
        int lenb = j;
        len = k;
        while (i&&j) {
            c[k] += a[i] + b[j];
            if (c[k] >= 10) {
                c[k - 1] += c[k] / 10;
                c[k] %= 10;
            }
            k--;
            j--;
            i--;
        }

        while(i) {
            c[k] += a[i];
            if (c[k] >= 10) {
                c[k - 1] += c[k] / 10;
            }
            k--;
            i--;
        }

        while (j) {
            c[k] += b[j];
            if (c[k] >= 10) {
                c[k - 1] += c[k] / 10;
            }
            k--;
            j--;
        }


        printf("Case %d:\n", Case++);
        for (int i = 1; i <= lena; i++)
            printf("%d", a[i]);
        printf(" + ");
        for (int i = 1; i <= lenb; i++)
            printf("%d", b[i]);
        printf(" = ");

        for (int i = 1; i <= len; i++) {
            if (c[0]&&i==1)
                printf("%d", c[0]);
            printf("%d", c[i]);
        }
        printf("\n");
        if (n != 0)
            printf("\n");


    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jiruqianlong123/article/details/81837877