"Algorithm: 1" high-precision Division

Source: Internet
Author: User

High-precision Division, I use high-precision subtraction to simulate

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#include<string>using namespaceStd;inlineintRead () {intx=0, f=1;CharCh=GetChar ();  for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') f=-1;  for(; isdigit (ch); Ch=getchar ()) x=x*Ten+ch-'0'; returnx*F;}Const intmaxn=10000;intA[MAXN],B[MAXN],C[MAXN];void inch(intA[])//This function is used to input the input string into an int array, the angle is 0 for the length. and stored in reverse. {    strings; CIN>>s; a[0]=s.length ();  for(intI=1; i<=a[0];i++) a[i]=s[a[0]-i]-'0'; return ;}voidPrintintA[])//Output{    if(!a[0]) puts ("0"); Else     {         for(inti=a[0];i>0; i--) printf ("%d", A[i]); Puts (""); }    return ;}intCompareintA[],intB[])//The comparison function, if a<b, returns 1 if a>b, returns 1 if a=b, returns 0{    if(a[0] > b[0] )return 1; if(a[0] < b[0] )return-1;  for(inti=a[0];i>0; i--)    {        if(A[i] > B[i])return 1; Else if(A[i] < b[i])return-1; }    return 0;}voidSubintA[],intB[])//High Precision Subtraction{    intflag=Compare (A, b); if(!flag) {a[0]=0;return ;} if(Flag = =1 )     {         for(intI=1; i<=a[0];i++)        {            if(A[i] <B[i]) {A[i+1]--; A[i]+=Ten; } A[i]-=B[i]; }         while(a[0] >0&&!a[a[0]]) a[0]--; }    return ;}voidnumcpy (intP[],intQ[],intDet//copy p array to q array starting from Det{     for(intI=1; i<=p[0];i++) q[i+det-1]=P[i]; q[0]=p[0]+det-1; return ;}voidDivintA[],intB[],intc[]) {    intTMP[MAXN]; c[0]=a[0]-b[0]+1;  for(inti=c[0];i>0; i--) {memset (TMP,0,sizeof(TMP));        numcpy (B,tmp,i);  while(Compare (A,TMP) >=0) {c[i]++;sub (a,tmp);}//using subtraction to simulate Division    }     while(c[0] >0&&!c[c[0]]) c[0]--; return ;}intMain () {inch(a),inch(b), Div (a,b,c), print (c), print (a);

"Algorithm: 1" high-precision Division

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.