Problem 1002: Big Decimal Addition

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 None.gif #include  < stdio.h >
 2 None.gif
 3 None.gif int  main()
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5InBlock.gif    int num, i, k;    //num: The number of lines, i & j: loop counter
 6InBlock.gif    int lengtha = -1, lengthb = -1, lengthl, lengths;    //The length of summand, the length of addend, the longer one, the shorter one
 7InBlock.gif    char a[1000], b[1000];    //summand, addend
 8InBlock.gif    char* c = new char[1000];    //result
 9InBlock.gif    char temp;
10InBlock.gif    int rlt = 0, adrlt = 0;    //result of a sigle digit, to carry to the next digit
11InBlock.gif
12InBlock.gif    scanf("%d"&num);    //input the first line, the number of input lines
13InBlock.gif    getchar();
14InBlock.gif
15InBlock.gif    for(k = 0; k < num; k++)
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//* initialization */
18InBlock.gif        lengtha = 0;
19InBlock.gif        lengthb = 0;
20InBlock.gif        for (i = 0; i < 1000; i++)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            a[i] = '0';
23InBlock.gif            b[i] = '0';
24InBlock.gif            c[i] = '0';
25ExpandedSubBlockEnd.gif        }

26InBlock.gif
27ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//* get the input value */
28InBlock.gif        for(i = 0; (temp = getchar()) != ' ' && i < 1000; i++)    //summand until space
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            a[i] = temp;
31InBlock.gif            lengtha++;
32ExpandedSubBlockEnd.gif        }

33InBlock.gif        for(i = 0; (temp = getchar()) != '\n' && i < 1000; i++)    //addend until '\n'
34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
35InBlock.gif            b[i] = temp;
36InBlock.gif            lengthb++;
37ExpandedSubBlockEnd.gif        }

38InBlock.gif
39ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//* print the result format */
40InBlock.gif        printf("Case %d:\n", k + 1);
41InBlock.gif        for (i = 0; i < lengtha; i++)
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43InBlock.gif            printf("%c", a[i]);
44ExpandedSubBlockEnd.gif        }

45InBlock.gif        printf(" + ");
46InBlock.gif        for (i = 0; i < lengthb; i++)
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48InBlock.gif            printf("%c", b[i]);
49ExpandedSubBlockEnd.gif        }

50InBlock.gif        printf(" = ");
51InBlock.gif
52ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//* find the longer one between summand and addend */
53InBlock.gif        if(lengtha > lengthb)
54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
55InBlock.gif            lengths = lengthb;
56InBlock.gif            lengthl = lengtha;
57InBlock.gif            c = a;
58ExpandedSubBlockEnd.gif        }

59InBlock.gif        else
60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
61InBlock.gif            lengths = lengtha;
62InBlock.gif            lengthl = lengthb;
63InBlock.gif            c = b;
64ExpandedSubBlockEnd.gif        }

65InBlock.gif
66ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//* add process */
67InBlock.gif        for(i = 0; i <= lengths; i++)
68ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
69InBlock.gif            rlt = 0;
70InBlock.gif            rlt = (a[lengtha - i] - '0'+ (b[lengthb - i] - '0'+ adrlt;
71InBlock.gif            if(rlt > 9)
72ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
73InBlock.gif                rlt = rlt - 10;    //if any result of digit needs to carry to the next digit
74InBlock.gif                adrlt = 1;
75ExpandedSubBlockEnd.gif            }

76InBlock.gif            else
77InBlock.gif                adrlt = 0;
78InBlock.gif            c[lengthl - i] = rlt + '0';
79ExpandedSubBlockEnd.gif        }

80ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//* carrying for the highest digit of the shorter one */
81InBlock.gif        if(adrlt == 1)
82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
83InBlock.gif            c[lengthl - lengths - 1= (c[lengthl - lengths - 1- '0'+ 1 + '0';
84ExpandedSubBlockEnd.gif        }

85InBlock.gif
86ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//* print the result */
87InBlock.gif        for (i = 0; i < lengthl; i++)
88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
89InBlock.gif            printf("%c", c[i]);
90ExpandedSubBlockEnd.gif        }

91InBlock.gif        if(k != num - 1)
92InBlock.gif            printf("\n\n");
93InBlock.gif        else
94InBlock.gif            printf("\n");
95ExpandedSubBlockEnd.gif    }

96ExpandedBlockEnd.gif}

Summary:
Solving this problem makes me absolutely overjoyed. You konw. for me, a newbie, to figure out such a nut witout any referance. Ok, let's get to business.

Besides the puzzling logic, the input-output format is still the key point in this problem. How to read and store the big decimal in a character array by digit? How to make the reading-terminal-condition?

 Another gist is that how to make the additon when summand and addend don't have the same numbers of digits. So take the lengths of them respectively into account is very important.

When taking the addition, begin the whole process from the lowest digit, so you're suppose to iterate the arrays in a reverse order. At this time, the longer length and shorter one will be used for the for condition.

About the convertion of data types:
Int and char are actually of the same data type which is represented by a integer(emu as well). So when you use char a = (int) b, or int a = (char) b; a actually gets the ASCII value of b, not b itselt. That is to say will get 49 for (int)'1'.
To make '2' to 2,
int a = 1;
char b;
b = '0' + a;
or
char a = '1';
int b;
b = a - '0';

你可能感兴趣的:([随笔分类]ACM,Problems)