1. 程式人生 > >杭電acm:1002 A+B Problem 2

杭電acm:1002 A+B Problem 2

A + B Problem II

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

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

#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    scanf("%d", &n);    //測試資料組數
    int s = 1;   //輸出 Case #時使用
    while (s <= n)
    {
        char str1[1000] = {0}, str2[1000
] = {0}; int a[1001] = {0}, b[1001] = {0}; scanf("%s%s", str1, str2); int len1 = strlen(str1), len2 = strlen(str2); //求字串長度 int i, j, k = 0; for (i = len1 - 1; i >= 0; i--) //將字元型轉化為數字 { a[k++] = str1[i] - '0'; } k = 0; for (j = len2 - 1; j >= 0; j--) { b[k++] = str2[j] - '0'; } k = len1 > len2 ? len1 : len2; //求和 for (j = 0; j < k; j++) { a[j] += b[j]; if (a[j] >= 10) { a[j] -= 10; a[j + 1] += 1; } } printf("Case %d:\n", s); //輸出 printf("%s + %s = ", str1, str2); if (a[k] == 0) { for (i = k - 1; i >= 0; i--) { printf("%d", a[i]); } } else { for (i = k; i >= 0; i--) { printf("%d", a[i]); } } printf("\n"); s++; if (s <= n) { printf("\n"); } } return 0; }