Big integer addition

Source: Internet
Author: User

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


The first solution is to store multiple integers. obviously, no variable of the C/C ++ inherent type can save it. the most intuitive idea is to use a string to save it. A string is essentially a character array. then perform operations from the low position to the high position in sequence. The addition result is greater than 9, that is, carry.
Complete code:

/*************************************** * **/* Hangdian acm1002: A + B Problem II/* 2010/10/1/* VC 6.0 by 52 coder/* persever2009@gmail.com ******************** * *********************/# include <iostream> using namespace STD; string sum_calc (string S1, string S2) {int length; // compare S1, S2 length if (s1.length ()> = s2.length () {length = s1.length (); string TMP (s1.length ()-s2.length (), '0'); S2 = TMP + S2;} else {length = s2.len Struct (); string TMP (s2.length ()-s1.length (), '0'); S1 = TMP + S1;} string sum (length, '0 '); // carry flag int carry = 0; For (int K = length-1; k! =-1; -- k) {// the ASCII code corresponding to the character '0' is 48 sum [k] = (S1 [k] + S2 [k] + carry-96) % 10 + 48; carry = (S1 [k] + S2 [k] + carry-96> 9);} // after the highest bit carries, the carry processing if (carry) sum = '1' + sum; return sum;} void main () {int t; // The number of test cases; CIN> T; // use the dynamic array when not knowing the dimension before compiling; string * A = new string [T]; string * B = new string [T]; string * sum = new string [T]; for (INT I = 0; I! = T; ++ I) {CIN> A [I]> B [I]; sum [I] = sum_calc (A [I], B [I]) ;}for (Int J = 0; J! = T; ++ J) {cout <"case" <j + 1 <": "<Endl <A [J] <" + "<B [J] <" = "<sum [J]; if (I! = T-1) cout <Endl ;}

Article from: I love programmers http://www.52coder.net/archives/1055.html copyright all. This site in addition to the source, are the author of the original articles, can be freely cited, but please indicate the source.

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.