High-precision division, as with the addition and subtraction, we have to start with the hand calculation. Give an example, for example, 524134 divided by 123. The result is 4261.

The first 4 of the source is we put 524 and 123 aligned, and then the cycle of subtraction, 4 cycles, the remaining 32, 32134 of the first three bits 321 continue and 123 alignment, the cycle subtraction 2 times, and 75, 7534 of the first three bits 753 and 123 align, Cycle subtraction 6 times, remaining 15, 154 and 123 are aligned, can only be reduced 1 times, so the result is 4 2 6 1.

Programmed the above process

1. Deposit A, b two number in the Char array 0 subscript indicates the highest bit

2. Compare the pre-LenB and B-alignment of a to size

3. If the previous lenb of a in the 2 comparison result is large, then the cyclic subtraction is performed until it is smaller than B and the number of cycles is denoted as s[0] to indicate the highest position of the final result.

4. If the previous LenB bit of a in the 2 comparison result is small, do nothing.

5. The whole B is backward to one, and the highest position of a (the highest bit may be 0 for the time being), the subscript iteration +1 of S is the calculation of the next digit.

6. Continuously compared until the tail of B is aligned with the tail of a, the last LenB bit of A is also a cyclic subtraction arithmetic, so the result is obtained. Terminating the program

The above mentioned LenB are the length of the first B, and later due to the displacement will lead to increased

The process is as follows

a:524134/

b:123/

4-time subtraction of loops

a:032134/

b:0123/-----Loop dislocation Front complement 0

2-time subtraction of loops

a:007534/

b:00123/

6-time subtraction of loops

a:000154/

b:000123/

1-time subtraction of loops

The source code is as follows:

#include <iostream>#include<cstring>#include<cstdio>#defineMAX 10001using namespacestd;Chara[max]={'0'},b[max]={'0'};intLena=0, lenb=0;intres[max]={0};BOOLIsgreater () {returnSTRNCMP (A,B,LENB) >=0;//Note that the LENB will increase because of the shift, so don't worry.}//subtract the current highest bit of a and the highest bit of BvoidSub () {//first of all to find the highest bit of a and B alignment, because in the while loop has been the size of the judgment, when the highest position refers to the true highest bit intI, begin =0; for(i =0; a[i]=='0'; ++i) Begin=i; //at this point, begin and I are the highest-level subscripts that are already aligned with B. for(; i < LenB; + +)i) a[i]= A[i]-B[i] +'0';//Note that if you use-=, it should be a[i]-= b[i]-' 0 '//start borrow to borrow negative complement from right to left for(inti = lenb-1; i >0; I.)if(a[i]<'0') {A[i]+=Ten; A[i-1]--; }} intMainintargcChar Const*argv[]) {cin>>A>>B; LenA=strlen (A); LenB=strlen (B); //If A is less than B direct output 0 ends. if(Lena<lenb or (LENA==LENB and!)Isgreater ())) {cout<<0<<endl;return 0; } intRes_i =0;//Tag res array being processed subscript 0 is the highest bit while(1){//Break when last aligned//begin cyclic subtraction of the first lenb bits of A and B while(Isgreater ()) {sub (); ++Res[res_i]; } ++res_i; //if the last remainder is less than the number of digits of B, then cot is 0 no problem if(LENA==LENB)//must be checked immediately after the subtraction cycle, because the LENB changed after the shift, and it's not necessary. Break; //Move right to B note LenB to increase by 1 intJ; for(j = lenb++; J >0; --j) B[j]=b[j-1]; B[J]='0'; } BOOLFlag =true;//indicates that there will be 0 output for(inti =0; i < res_i; ++i) {if(Flag and res[i]!=0) Flag=false;//already encountered the first number that is not 0 indicates that there are 0 outputs if(res[i]==0){ if(!flag) cout<<0; }Elsecout<<Res[i]; } cout<<Endl; return 0;}

View Code

"Algorithmic Learning Notes" 34. High-Precision Division SJTU OJ 1026/1016