Waterloo-Basic Exercise 29-High precision addition-java code

Source: Internet
Author: User

Problem description
Enter two integers a and b to output the and of the two integers. Neither A nor B is more than 100 digits.
Algorithm Description
Because A and B are large, they cannot be stored directly using the standard data types in the language. For this type of problem, the array is generally used for processing. The
define an array a,a[0] is used to store a single digit, a[1] is used to store 10 bits of a, and so on. You can also use an array of B to store B. When the
computes C = A + B, the a[0] is added first and b[0], and if there is a carry, the carrying (that is, the sum of 10 digits) is deposited in R, and the Single-digit number is deposited in the c[0], that is, c[0 is equal to (a[0]+b[0])%10. Then calculate the a[1] and b[1] together, and then add up the low value R Also, that is, c[1] should be the sum of the a[1, B[1, and R three numbers. If there is a carry generation, you can still save the new carrying into R, and the bit to c[1]. And so on, you can find all the bits in C. The
finally outputs C. The
Input format
input includes two lines, the first behavior a non-negative integer A, and the second behavior a nonnegative integer b. Two integers are no more than 100 digits, and the highest bit of two is not 0. The
output format
Outputs a row that represents the value of a + B.
Sample Input
20100122201001221234567890
2010012220100122
Sample output
20100122203011233454668012

Package basic exercises;
Import Java.util.Scanner; /** * @author Xu Juan * @email 547139255@qq.com * @detail/public class Basic_29 {public static void main (Strin
        G[] args) {Scanner cin=new Scanner (system.in);
        Int[] A=new int[101];
        Int[] B=new int[101];
        Int[] Result=new int[101];
        for (int i = 0; I <101; i++) {a[i]=b[i]=result[i]=0;
        } String Line1=cin.nextline ();

        String Line2=cin.nextline ();  
        for (int i = 0; i < line1.length (); i++) a[i]= (int) Line1.charat (line1.length () -1-i)-48;

        for (int i = 0; i < line2.length (); i++) b[i]= (int) Line2.charat (line2.length () -1-i)-48;

        int max=line1.length () >line2.length () line1.length (): Line2.length ();
        int jinwei=0;
        int r=0;
            for (int i = 0; i < i++) {result[i]=a[i]+b[i]+r;
            R=RESULT[I]/10;
        result[i]%=10; } intflag=100;
                for (int i = i>-1; i--) {if (result[i]!=0) {flag=i;
            Break
        for (int i = flag I >-1; i--) {System.out.print (result[i));

    }system.out.println (); }
}

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.