C language · High-precision Addition

Source: Internet
Author: User

Problem description Enter two integers aAnd b, and outputs the and of the two integers. aAnd bare not more than 100 bits. Algorithm description due to aAnd bare larger, they cannot be stored directly using the standard data types in the language. For this type of problem, arrays are generally used for processing.
Define an array AA[0] for storage aof Bits, A[1] for storage a10 bits, and so on. You can also use an array Bto store b
Calculation C= a+ b, the first place A[0] With B[0] Add, if there is a carry, then the rounding (i.e., the sum of 10 digits) is deposited R, put in and the single digit deposit C[0], i.e. C[0] Equals ( A[0]+ B[0]) %10 And then calculate A[1] With B[1] Add, at this point should also be low into the value of the RAlso add up, namely C[1] It should be A[1], B[1] and RThree number of the and. If there is another carry, the new rounding can still be deposited into RIn, and the single-digit deposit to C[1]. And so on, you can find out Cof all bits.
Finally, the COutput can be. Input format input consists of two lines, the first behavior a non-negative integer a, the second behavior is a non-negative integer b。 Two integers are not more than 100 bits, and the highest bit of two numbers is not 0. The output format outputs a row that indicates a+ bThe value. Sample Input 20100122201001221234567890
2010012220100122 Sample Output 20100122203011233454668012
1 /*2 Analysis:3 4 1. The length of the two number may be different, then the remaining number of the other number must be added after one count is added. 5 6 2. Judge the highest bit, if the highest bit has carry, then also need to add the place. 7 */8#include <stdio.h>9#include <string.h>Ten intMain () One { A     Chara[ $],b[ $];//defining a string array -     intc[ -];  -scanf"%s%s", A, b);//Input string Array the     inti,j,k=0, r=0; -     intLena,lenb; -Lena =strlen (a); -LenB =strlen (b); +      for(i=lena-1, j=lenb-1; i>=0&&j>=0; I--, j--){   -         intP= (a[i]-'0') + (b[j]-'0')+R;  +r=p/Ten;//Rounding Ac[k++]=p%Ten;//The remainder is added to the array at     } -      while(i>=0){//if the number in B is gone, -         intP= (a[i]-'0')+R;  -r=p/Ten;  -c[k++]=p%Ten;  -i--;  in     }   -      while(j>=0){//if the number in a is gone, to         intP= (b[j]-'0')+R;  +r=p/Ten;  -c[k++]=p%Ten;  thej--;  *     }   $     if(r) {//Judging if the highest bit has no roundingPanax Notoginsengc[k++]=R;  -     }   the      for(inti=k-1; i>=0; i--) {//Output Results +printf"%d", C[i]); A     } the     return 0; +}

C language · High-precision Addition

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.