Question 1471: A + B without carry

Source: Internet
Author: User

Question Description: Xiao Ming always tends to ignore the carry when he does decimal addition with paper and pencer. for example, 15 + 17, Xiao Ming will answer 22, because he ignores the carry from the single digits.5 + 7 = 12, and the digit 1 is the carry. enter The input will consist of a series of pairs of integers a and B (both less than 1000000000), separated by a space, one pair of integers per line. output For each pair of input Integers a and B you shoshould output the correct answer of the sum of a and B, a space character and Xiao Ming's answer of the sum of a and B in one line, and with one line of output for each line in input. if Xiao Ming's answer begins with zero, don't output unnecessary zero. sample input 15 161 99931 71 sample output 31 211000 990102 2 prompt [+] *** prompt hidden, click [+] at the top of the page to display the *** Source: July 22, 2013, Zhejiang University, retest and simulation question [cpp]/******************** ******* * Date: * Author: SJF0115 * question: Question 1471: A + B without carry * Source: http://acmclub.com/problem.php? Id = 1471 * result: AC * Source: re-test computer simulation questions of Zhejiang University in 2013 * conclusion: * *********************************/# include <stdio. h> # include <string. h> # include <stdlib. h> int main () {int a, B, len1, len2, index, I, j; char str1 [11], str2 [11], c [11]; // freopen ("C: \ Users \ SJF \ Desktop \ acm.txt", "r", stdin); while (scanf ("% s ", str1, str2 )! = EOF) {len1 = strlen (str1); len2 = strlen (str2); index = 0; // James's calculation process for (I = len1-1, j = len2-1; i> = 0 & j> = 0; I --, j --) {int sum = str1 [I]-'0' + str2 [j]-'0 '; // ignore carry if (sum> 9) {sum-= 10;} c [index ++] = sum + '0';} while (I> = 0) {c [index ++] = str1 [I]; I --;} while (j> = 0) {c [index ++] = str2 [j]; j --;} // remove the leading 0 index = index-1; while (c [index] = '0' & index> 0) {index --;} // correct answer: printf ("% D ", atoi (str1) + atoi (str2); // James's answer for (I = index; I> = 0; I --) {printf ("% c", c [I]);} printf ("\ n");} return 0 ;} /********************************** Date: * Author: SJF0115 * question: Question 1471: A + B without carry * Source: http://acmclub.com/problem.php? Id = 1471 * result: AC * Source: re-test computer simulation questions of Zhejiang University in 2013 * conclusion: * *********************************/# include <stdio. h> # include <string. h> # include <stdlib. h> int main () {int a, B, len1, len2, index, I, j; char str1 [11], str2 [11], c [11]; // freopen ("C: \ Users \ SJF \ Desktop \ acm.txt", "r", stdin); while (scanf ("% s ", str1, str2 )! = EOF) {len1 = strlen (str1); len2 = strlen (str2); index = 0; // James's calculation process for (I = len1-1, j = len2-1; i> = 0 & j> = 0; I --, j --) {int sum = str1 [I]-'0' + str2 [j]-'0 '; // ignore carry if (sum> 9) {sum-= 10;} c [index ++] = sum + '0';} while (I> = 0) {c [index ++] = str1 [I]; I --;} while (j> = 0) {c [index ++] = str2 [j]; j --;} // remove the leading 0 index = index-1; while (c [index] = '0' & index> 0) {index --;} // correct answer: printf ("% d", atoi (str1) + atoi (str2) ); // James's answer for (I = index; I> = 0; I --) {printf ("% c", c [I]);} printf ("\ n");} return 0 ;} [cpp]/********************************* Date: * Author: SJF0115 * question: Question 1471: A + B without carry * Source: http://acmclub.com/problem.php? Id = 1471 * result: AC * Source: re-test computer simulation questions of Zhejiang University in 2013 * conclusion: * *********************************/# include <stdio. h> # include <string. h> # include <stdlib. h> int main () {int a, B, index; int c [12]; // freopen ("C: \ Users \ SJF \ Desktop \ acm.txt "," r ", stdin); while (scanf (" % d ", & a, & B )! = EOF) {// correct answer printf ("% d", a + B); // if (a = 0 & B = 0) {printf ("% d", a + B);} index = 0; while (a | B) {int sum = a % 10 + B % 10; if (sum> 9) {sum-= 10;} c [index ++] = sum; a/= 10; B/= 10 ;} // remove the leading 0 index = index-1; while (c [index] = 0 & index> 0) {index --;} // output answer for (int I = index; I> = 0; I --) {printf ("% d", c [I]);} printf ("\ n");} return 0 ;} /********************************** Date: * Author: SJF0115 * question: Question 1471: A + B without carry * Source: http://acmclub.com/problem.php? Id = 1471 * result: AC * Source: re-test computer simulation questions of Zhejiang University in 2013 * conclusion: * *********************************/# include <stdio. h> # include <string. h> # include <stdlib. h> int main () {int a, B, index; int c [12]; // freopen ("C: \ Users \ SJF \ Desktop \ acm.txt "," r ", stdin); while (scanf (" % d ", & a, & B )! = EOF) {// correct answer printf ("% d", a + B); // if (a = 0 & B = 0) {printf ("% d", a + B);} index = 0; while (a | B) {int sum = a % 10 + B % 10; if (sum> 9) {sum-= 10;} c [index ++] = sum; a/= 10; B/= 10 ;} // remove the leading 0 index = index-1; while (c [index] = 0 & index> 0) {index --;} // output answer for (int I = index; I> = 0; I --) {printf ("% d", c [I]);} printf ("\ n");} return 0;} in the second method, wrong forgets the case that a = 0 B = 0 several times ......... this method is very interesting. I didn't think of it ......... [Cpp] # include <stdio. h> int a, B; void run () {int c, k; c = a + B; printf ("% d", c ); // This program is used by the normal sum to reverse the uncarried sum (or directly adding by bit ). K = 1; while (k <= a | k <= B) {if (a/k % 10 + B/k % 10> 9) c-= k * 10; // The carry on a certain bit is ignored, which is equivalent to reducing the sum by ten times of the weight of this bit, and the bit carry is ignored, the sum is 10 less. K * = 10;} printf ("% d \ n", c);} int main () {while (scanf ("% d", &, & B )! = EOF) run (); return 0 ;}# include <stdio. h> int a, B; void run () {int c, k; c = a + B; printf ("% d", c ); // This program is used by the normal sum to reverse the uncarried sum (or directly adding by bit ). K = 1; while (k <= a | k <= B) {if (a/k % 10 + B/k % 10> 9) c-= k * 10; // The carry on a certain bit is ignored, which is equivalent to reducing the sum by ten times of the weight of this bit, and the bit carry is ignored, the sum is 10 less. K * = 10;} printf ("% d \ n", c);} int main () {while (scanf ("% d", &, & B )! = EOF) run (); 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.