A + B Problem II

Source: Internet
Author: User
Problem descriptioni have a very simple problem for you. given two integers A and B, your job is to calculate the sum of A + B. inputthe 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. outputfor 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 input21 2112233445566778899 998877665544332211 sample outputcase + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
1 # include <stdio. h> 2 # include <string. H & gt; 3 # define n 1001 4 5 Int main () {6 int t; 7 char a [n]; 8 char B [N]; 9 int sum [N]; 10 char temp [N]; 11 int a_length; 12 INT B _length; 13 int I; 14 int length; // length difference 15 int first; 16 int second; 17 int flag; 18 int flag2; 19 int time; 20 21 scanf ("% d", & T); 22 time = 1; // time indicates the number of input data groups 23 24 while (t --) {25 flag = 0; 26 flag2 = 0; 27 scanf ("% S % s",, b); 28 29 printf ("Case % d: \ n", time); 30 time ++; 31 printf ("% S + % s =", a, B ); 32 33 a_length = strlen (a); 34 B _length = strlen (B); 35 36 IF (a_length <B _length) {// ensure that the length of a is greater than or equal to the length of B 37 strcpy (temp, a); 38 strcpy (a, B); 39 strcpy (B, temp ); 40} 41 42 a_length = strlen (a); 43 B _length = strlen (B); 44 length = A_length-B_length; 45 for (I = B _length-1; I> = 0; I --) {// move array B back 46 B [I + Length] = B [I]; 47} 48 49 for (I = 0; I <length; I ++) // Add 'before array B' 0 '50 B [I] = '0'; 51 52 B [a_length] = '\ 0'; // array B add end symbol 53 54 for (I = A_length-1; i> = 0; I --) {55 First = A [I]-'0'; 56 second = B [I]-'0 '; 57 sum [I] = (first + second) % 10; 58 59 if (I> 0) 60 a [I-1] = A [I-1]-'0' + (first + second)/10 + '0 '; 61} 62 63 first = A [0]-'0'; 64 second = B [0]-'0'; 65 66 flag = (first + second)/10; 67 68 if (FLAG) 69 printf ("% d", flag); 70 71 for (I = 0; I <a_length; I ++) {72 If (FLAG) {73 flag2 = 1; 74 printf ("% d", Sum [I]); 75} 76 77 else {78 If (sum [I]! = 0) {79 flag2 = 1; 80 printf ("% d", sum [I]); 81} 82 83 else {84 If (flag2 = 1) 85 printf ("% d", sum [I]); 86} 87} 88 89} 90 91 If (flag2 = 0) 92 printf ("0 "); 93 94 printf ("\ n"); 95 96 If (T! = 0) 97 printf ("\ n"); 98} 99 100 return 0; 101}

 

 

A + B Problem II

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.