Large numbers [addition, subtraction, multiplication and division]

Addition: Input the character array, and then convert it and store it in the integer array in reverse order. During the addition process, pay attention to the carry variable to judge whether the carry is still needed at the end, and pay attention to the special judgment of 0+0.

Subtraction: Similar to addition, you can change the carry variable to a borrow variable, but you need to use the larger number to subtract the smaller number.

Multiplication: Multiplication should be the easiest. The same character array is input, and then stored in the integer array in reverse order. Using dislocation multiplication and addition, we can get: sum[i+j]=a[i]*b[j ].

 Division of large numbers is the most difficult of the four arithmetic operations. Different from the general simulation, the division operation is not imitated by manual division, but realized by the subtraction operation. The basic idea is to repeat the division to see how many divisors can be subtracted from the dividend, and the quotient is the number. It is obviously too slow to reduce one by one. It is necessary to judge how many integers (divisors) to the nth power of 10 can be reduced at most at one time.

Take 7546 divided by 23 as an example:

    First subtract 100 times of 23 from 7546, that is, subtract 2300, you can subtract 3 times, and the remaining 646, at this time, the quotient is 300 (300=100*3);

    Then subtract 10 times of 23 from 646, that is, subtract 230, you can subtract 2 times, leaving 186, and the quotient at this time is 320 (320=300+10*2);

    Then subtract 23 from 186, you can subtract 8 times, leaving 2, and the quotient at this time is 328 (328=320+1*8);

    Since the result of dividing 2 by 23 is less than 1, and we don't need to count the decimal places, we don't need to keep counting.

[Drawing from https://www.cnblogs.com/wuqianling/p/5387099.html ]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848599&siteId=291194637