Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language homework topic is: Please design an algorithm to complete the addition of two super-long positive integers. Who will? Help!
C language homework topic is: Please design an algorithm to complete the addition of two super-long positive integers. Who will? Help!
Obviously, it is highly accurate.

I remember that long integers refer to long, beyond long and long long, and __int64, but obviously that's not what the title means.

I think LZ should be familiar with vertical addition, and high precision is the principle of vertical addition.

I'd better just give you the algorithm, not the code, and write the code myself.

First of all, you can't directly enter scanf, you can enter it as a string-I think LZ you will.

Then create four integer arrays, two of which represent the numbers to be added, one represents the carry and the other represents the result.

Because the sum of two digits will never exceed 18, the last carried array can be saved as bool type.

First, pass the two input strings to the two plastic arrays respectively, and pay attention to starting from the tail, so that the subscripts of the two plastic arrays can store two numbers in the order of 100,000 ... Starting from 0, they are strictly bit-aligned.

So you can add each array bit at a time (note that the array initialization is set to 0, so you can add it to the highest bit).

When adding, judge whether it is greater than 10. If it is greater than 10, it is subtracted from 10, and then the carry bool array of bits with subscript+1 changes from 0 to 1.

Create a new output array.

Finally, traverse the result array again. If there is a carry, it will be+1, and if it continues to carry, it will directly operate the next bit.

Import into the output array, and printf will be ok.