HDU 1002 A + B Problem II

Source: Internet
Author: User

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 shoshould 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 shoshould 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

I almost got abused. In this simple question, I wrote an inline function, max, which is different from the function in algorithm.
Only AC is handed in three times. I will give you some data for WA:
0001 1000
0 0
000 0000
9999 1
1 9999
99900 00999
00999 99900
If these data and examples are all used, you should be able to access

LANGUAGE: C ++
CODE:
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <stdlib. h>
 
# Define maxn1005
 
Using namespace std;
 
Char ans [maxn];
Int anslen;
 
Void plus (char s1 [], char s2 [])
{
Int len1 = strlen (s1 );
Int len2 = strlen (s2 );
 
For (int I = 0; I <len1; I ++)
S1 [I]-= '0 ';
For (int I = 0; I <len2; I ++)
S2 [I]-= '0 ';
 
Int mid1 = len1> 1;
Int mid2 = len2> 1;
 
// Printf ("% d \ n", len1, len2, mid1, mid2 );
 
For (int I = 0; I <mid1; I ++)
Swap (s1 [I], s1 [len1-1-i]);
For (int I = 0; I <mid2; I ++)
Swap (s2 [I], s2 [len2-1-i]);
 
Anslen = max (len1, len2 );

// Printf ("\ n anslen = % d \ n", anslen );
 
Int s = 0, sum;
For (int I = 0; I <= anslen; I ++)
{
Sum = (s1 [I] + s2 [I] + s );
Ans [I] = sum % 10;
S = sum/10;
}
}
 
Void printans (char ans [], int anslen)
{
// If (ans [anslen + 1] = 0)
While (anslen> 0 & (ans [anslen] = 0) anslen --;
// Else printf ("% d", ans [anslen + 1]);
If (anslen = 0)
{
Printf ("% d \ n", ans [0]);
Return;
}
For (int I = anslen; I>-1; I --)
{
Printf ("% d", ans [I]);
}
Printf ("\ n ");
}
 
Int main ()
{
Int cas;
Char s1 [maxn], s2 [maxn];
Scanf ("% d", & cas );
For (int I = 1; I <= cas; I ++)
{
Memset (s1, 0, sizeof (s1 ));
Memset (s2, 0, sizeof (s2 ));
Scanf ("% s", s1, s2 );
Printf ("Case % d: \ n", I );
Printf ("% s + % s =", s1, s2 );
Plus (s1, s2 );
Printans (ans, anslen );
If (I! = Cas) printf ("\ n ");
}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.