HDU 1002 A + B Problem II

龍威昊發表於2019-05-10

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 391535 Accepted Submission(s): 75775

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


http://acm.hdu.edu.cn/showpro…


Accepted Code

// Author : Weihao Long
// Created : 2017/12/12

#include "stdio.h"
#include "string.h"

#define LENTH 1000

int main() {
    int t, tt;
    int count = 1;
    scanf("%d", &t);
    tt = t;
    while (t--) {
        char num1[LENTH], num2[LENTH], num3[LENTH];
        scanf("%s", num1, LENTH);
        scanf("%s", num2, LENTH);
        int len1, len2;
        len1 = strlen(num1);
        len2 = strlen(num2);
        int flag = len1 > len2;
        if (flag) {
            int tag = 0;
            while (len2--&&len1--) {
                num3[len1] = num1[len1] + num2[len2] - `0` + tag;
                if (num3[len1] - `0` > 9) {
                    tag = 1;
                    num3[len1] -= 10;
                }
                else
                    tag = 0;
            }
            while (len1--) {
                num3[len1] = num1[len1] + tag;
                if (num3[len1] - `0` > 9) {
                    tag = 1;
                    num3[len1] -= 10;
                }
                else
                    tag = 0;
            }
            num3[strlen(num1)] = ` `;
            if (tag) {
                printf("Case %d:
", count);
                printf("%s + %s = 1%s
", num1, num2, num3);
            }
            else {
                printf("Case %d:
", count);
                printf("%s + %s = %s
", num1, num2, num3);
            }
            if (count != tt)
                printf("
");
            count++;
        }
        else {
            int tag = 0;
            while (len1--&&len2--) {
                num3[len2] = num1[len1] + num2[len2] - `0` + tag;
                if (num3[len2] - `0` > 9) {
                    tag = 1;
                    num3[len2] -= 10;
                }
                else
                    tag = 0;
            }
            while (len2--) {
                num3[len2] = num2[len2] + tag;
                if (num3[len2] - `0` > 9) {
                    tag = 1;
                    num3[len2] -= 10;
                }
                else
                    tag = 0;
            }
            num3[strlen(num2)] = ` `;
            if (tag) {
                printf("Case %d:
", count);
                printf("%s + %s = 1%s
", num1, num2, num3);
            }
            else {
                printf("Case %d:
", count);
                printf("%s + %s = %s
", num1, num2, num3);
            }
            if (count != tt)
                printf("
");
            count++;
        }
    }
    return 0;
}

Notes

這題是對兩個大整數求和,可以用字串來解決。

相關文章