Description
Stripping divided by high-precision
Input
Each set of test data stack 2 rows, each row represents a high precision integer (no more than 100 bits)
Output
Each set of test data output accounted for 2 rows, respectively, the output quotient and the remainder.
Sample Input
140
5
Sample Output
28
0
Ideas:
Division is analog subtraction.
First, the maximum multiplier (represented by 10^k) that can be subtracted from the divisor is calculated, and then minus 10^ (k-1), then the number of (k-2) times,,,, until it is not enough.
#include <cstdio> #include <cstring> #include <iostream> using namespace std;
Char aa[109],bb[109];
int a[109],b[109],ans[109];
int len1,len2;
void Init () {Len1=strlen (AA);
Len2=strlen (BB);
memset (a,0,sizeof a);
memset (b,0,sizeof b);
Memset (ans,0,sizeof ans);
for (int i=len1-1,j=0; i>=0; I--, j + +) a[j]=aa[i]-' 0 ';
for (int i=len2-1,j=0; i>=0; I--, j + +) b[j]=bb[i]-' 0 ';
} void Output () {//cout<< "*************\n";
int Len=strlen (AA);
for (int i=0; i<len; i++)//output before processing carry, due to our results when the corresponding multiple of the divisor, and then add out the ans[i+1]+=ans[i]/10,ans[i]%=10;
int i=len-1;
int flag=0;
while (ans[i]==0) i--;
if (i>=0) flag=1;
for (; i>=0; i--) cout<<ans[i];
if (!flag) cout<< "0";
cout<<endl;
i=len-1;
flag=0;
while (a[i]==0) i--;
if (i>=0) flag=1;
for (; i>=0; i--)//The remainder is stored in the remainder of a cout<<a[i];
if (!flag) cout<< "0"; cout<<endl
cout<< "*************\n";
} int cmp (int *p1,int *p2,int len1,int len2)//Compare the size of two numbers {if (len1<len2) return-1;
else if (len1==len2) {for (int i=len1-1; i>=0; i--) if (P1[i]>p2[i]) return 1;
else if (p1[i]<p2[i]) return-1;
return 0;
} else return 1; } int sub (int *p1,int *p2,int len1,int len2) {//cout<< "-----------------\ n";//for (int i=len1-1; i>=0; i
--) cout<<p1[i];
cout<<endl;
for (int i=len2-1; i>=0; i--) cout<<p2[i];
cout<<endl;
cout<< "-----------------\ n";
if (CMP (P1,P2,LEN1,LEN2) ==-1) return-1;//Returns-1 description not enough minus for (int i=0; i<len1; i++)//analog high-precision subtraction {p1[i]-=p2[i];
if (p1[i]<0) p1[i]+=10,p1[i+1]--;
} for (int i=len1-1; i>=0; i--) if (P1[i]) return i+1;//returns the number of bits left to return 0;
} void Solve () {init ();
Len1=sub (A,B,LEN1,LEN2);
if (len1<0)//not enough minus {cout<< "0" <<endl;
for (int i=strlen (AA)-1; i>=0; i--) cout<<a[i];
cout<<endl;
Return
} ans[0]++;//is first directly reduced, so the result is to add an int k=len1-len2;
if (k<0) {output ();
Return
The else if (k>0)//heuristics the maximum multiplier 10^k {for (int i=len1-1; i>=0; i--)//increases the number of 0 after the divisor and becomes its 10^k times {
if (i>=k) b[i]=b[i-k];
else b[i]=0;
}}//cout<< "k=" <<k<<endl;
len2=len1;//note the length of the dividend at this time has changed.
for (int j=0; j<=k; j + +)//minus 10^ (k-1), (k-2),,,, 10^0 times {int tmp; while ((Tmp=sub (a,b+j,len1,len2-j)) >=0) has been reduced, know not to lose, pay attention to the technique of parameter ...
The second parameter shows that the divisor at this time is the original divisor of 10^ (k-j) times {len1=tmp;
ans[k-j]++;
}} output ();
} int main () {while (CIN>>AA>>BB) {solve ();
} return 0;
}