Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to calculate large numbers in java
How to calculate large numbers in java

Whether you use JAVA or not, the method is actually the same. Using an array, for example, a number with a length of one thousand, you can use this int[] number = new int[1000]; The idea of ??adding is, first, two The lengths of the two arrays must be the same. If the number of digits is different, add 0 in front. This is a bit of a waste of memory, but it is more convenient to calculate. Then let them go from 0 to the highest bit, add each bit and save it in the corresponding position. The last step is to start from 0 to determine if there is a number greater than 10. If there is a number, carry it forward (add 1 to the previous one and subtract 10 from the current position). That's it.

If it is subtraction, the idea is actually the same. Subtract each bit first, and then determine whether there is less than 0. If so, borrow 1 from the previous bit (subtract 1 from the previous bit, and add 10 to the current bit). If it is multiplication, , the same as above, but it should be noted that when carrying, more than one digit is carried forward, such as 5*6, and it is necessary to advance 3 digits (the previous digit is increased by 3, and the current digit is subtracted by 30). Division is more troublesome. When it comes to accuracy issues, it is easier to see the actual output required. When directly for looping an array, special attention should be paid to the fact that if there is a carry in the highest bit, this is a more error-prone place. The idea is as above. If it cannot be implemented, please leave a message.