Big Data Division (Large Division)

Source: Internet
Author: User

Title DescriptionDescription Division is the basic operation in the calculation, although the rules are simple, but the number of bits is too many, it will inevitably error. The question now is: given any number of digits (large enough to be a divisor of O (∩_∩) o), given a long long type of divisor, the remainder is retained and the S-bit after the decimal point is preserved. input/output format input/output Input Format:
Three lines: The first line: dividend the second line: divisor the third line: a number S, indicating that the S bit is retained after the decimal point
output Format:
The number of rows representing the result of the calculation (no leading 0 possible) input and Output sample sample Input/output sample Test point # # Input Sample:123456789
123
30 Input Sample: 1003713.731707317073170731707317073170 idea: This problem of large data volume, large data type, if the direct calculation is not advisable, must use high-precision algorithm. here can be divided into several functions to calculate: ① high-precision divided by single-precision function: The topic will be carefully said
Reverse output function: The topic will be carefully said ③ Hand calculation of the process of division, the remainder *10 divided by the divisor, the number after the first decimal point, save to K inside, the remainder is assigned to the remainder-the divisor *k (can not think of it, it is possible to manually simulate the process, in fact, very simple) then a single output can The code is as follows:
1#include <stdio.h>2#include <string.h>3 #defineMaxLength 999994 intX,q;//q is used for backup remainder5 voidOutputintC[])//Reverse output function6 {7     inti;8      for(i=c[0];i>=1; i--)9printf"%d", C[i]);Ten } One /*============================================================================*/ A voidDivCharStra[],intBintC[])//The high precision number A divided by the integer b, the result is saved in C.  - { -     inta[maxlength],i,lena,lenc,j,t; theMemset (A,0,sizeof(a)); -Memset (c,0,sizeof(c)); -     if(b==0)  -     { +c[0]=1; -c[1]=0; +         return ; A     } atLena=strlen (stra); -      for(i=0; i<lena;i++)//storing the string stra positive sequence to array a -a[i+1]=stra[i]-'0';  -x=0; -      for(i=1; i<=lena;i++)//Divide by Phase -     { inC[i]= (x*Ten+a[i])/b; -X= (x*Ten+a[i])%b; to     }     +Lenc=1; -      while(c[lenc]==0&&lenc<lena)//High may have some meaningless 0, need to set the LENC bit theLenc++;  *c[0]=lena-lenc+1; $      for(j=1; j<=c[0];j++)//move the array C whole to the left, that is, the prefix 0 to cover outPanax Notoginseng     { -c[j]=C[lenc]; theLenc++; +     } A      for(i=1, j=c[0];i<j;i++,j--) the     { +t=c[i];c[i]=c[j];c[j]=T; -     } $Q=x;//back up the remainder $Output (c);//Incoming output function - } - /*============================================================================*/ the intMain () - {Wuyi     CharA[maxlength]; the     intB[maxlength]; -     inti,k; Wu     Long LongS,n; - gets (a); Aboutscanf"%i64d",&n); $scanf"%d",&s); - Div (a,n,b); -printf".");  -      for(i=0; i<s;i++)//s bit after decimal point A     { +x=x*Ten; thek=x/N; -printf"%d", k); $x=x-n*K; the     } theprintf"\n%d\n", q);  the     return 0; the}

Big Data Division (Large Division)

Related Article

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.