The addition operation of large number, Hangzhou electric oj-1002

Source: Internet
Author: User

Original title Address: http://acm.hdu.edu.cn/showproblem.php?pid=1002 "problem Description" I have a very simple problem for you. Given integers A and B, your job is to calculate the Sum of A + B. "input" the first line of the input contains an Integ Er T (1<=t<=20) which means the number of test cases. Then T lines follow, each line consists of both positive integers, A and B. Notice that the integers is very large, that M EANs should not process them by using 32-bit integer. Assume the length of each integer would not be exceed 1000. "Output" for each test case, you should output of both lines. The first line was "Case #:", # means the number of the the test case. The second line is the equation "A + b = sum", sum means the result of A + B. Note there is some spaces int the Equati On. Output a blank line between test cases. "Sample Input" 2 1 2 112233445566778899 998877665544332211"Sample Output "Case 1:1 + 2 = 3 Case 2:1.,122,334,455,667,79e,+17 + 998877665544332211 = 1111111111111111110" AC Code "
1#include <stdio.h>2#include <string.h>3 #defineMax (A, b) a > B? A:b4 5 Chara[1024x768];6 Charb[1024x768];7 Charc[1024x768];8 inti;9 Ten voidReverseChar*a) One { A     intAA =strlen (a); -     CharT; -      for(i=0; i<aa/2; i++) the     { -t =A[i]; -A[i] = a[aa-1-i]; -a[aa-1-I.] =T; +     } - } + voidAddChar*a,Char*b,Char*c) A { at         intCC =0, AA = strlen (a), BB =strlen (b); -         intLen =max (AA, BB); -             for(i=0; i<aa; i++) -            { -A[i] = a[i]-'0'; -         } in             for(i=0; i<bb; i++) -         { toB[i] = b[i]-'0'; +         } -          for(i=0; i<len; i++) the         { *C[i] = (A[I]+B[I]+CC)%Ten+'0'; $CC = (A[I]+B[I]+CC)/Ten;Panax Notoginseng         } -            if(cc) c[i++] = cc +'0'; theC[i] =' /'; + } A voidPrintChar*c) the { +      for(i = strlen (c)-1; i>=0; i--) -printf"%c", C[i]);  $printf"\ n"); $ } - Main () - { the     intN, J; -scanf"%d", &n);Wuyi      for(j=1; j<=n; J + +) the     { -Memset (A,0,sizeof(a)); Wumemset (b,0,sizeof(b)); -Memset (c,0,sizeof(c)); Aboutscanf"%s%s", A, b); $printf"Case %d:\n", j); -printf"%s +%s =", A, b); - reverse (a), reverse (b); - Add (A, B, c); A print (c); +           if(J! = N) printf ("\ n"); the     } -      $}

Addition operation of large number, Hangzhou electric oj-1002

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.