High-precision thought tutorial (pressure position) and code implementation

Source: Internet
Author: User
Tags add numbers arithmetic array length

High precision This thing is really a long time did not write, after many years, from Pascal to C + +, the code style has also changed

Re-edited (after Pan guidance.) I think it's much better than I wrote before.


Thought to simply write a small tutorial, I hope beginners can have some harvest ~ ~


I. High-precision arithmetic thought

High precision this thing in fact who will, on the elementary school mathematics class to know high-precision how to do, is according to the person's operation method, one operation.

1. Addition

That is, from the beginning of two digits added, if there is a carry, add to 10, and then calculate 10-digit addition, ...

2. Subtraction

It is also subtracted from the first two digits, if the number is less than 0, then add 10, and subtract 10 bits from the number one, ...

3. Multiplication

The first number multiplied by the second digit, writes down, the right is aligned with the single digit, then multiplies with the second Number 10 digits, the right and the 10-bit alignment, ...

Think carefully, you will find in fact the first number from the right number I, may be set to 2 times the second number from the right number of the J-bit, may be set to 8, is equivalent to 2*10^ (i-1) *8*10^ (j-1)

This product contributes to the answer from the right number of the first i+j-1 bit, that is, 16*10^ (i+j-2), to carry 1 to the right from the number of the first i+j, leaving 6.

4. Division

Division is actually an extension of subtraction, for example, 24723 divided by 123.

From a high position, according to human practice, a figure that takes out a dividend

First 2, the judgment is less than 123, less than the answer this one will be 0

Take another one, 24, or less, the answer to this position 0

Take another one, 247, this time is bigger than 123, then see can reduce how many 123, found minus 2 times later than 123 smaller, then the answer is the 3rd place is 2, minus the remaining number is 1

Repeat that process, take a bit, 12, less than 123, answer this position 0

Take another one, 123, not less than 123, see can reduce how many, find can reduce one, then the answer 5th is 1

The remaining number is 0, and the number of the divisor is also taken out, and the operation ends.

The answer is 00201, sorted out is 201.


Second, storage mode

Practice down for convenience, the lowest bit exists in the first place of the array, the highest bit of the last one, is the easiest to write the program, the reason is very simple, because every time from the lowest bit start, and multiplication is obviously more convenient.

I generally use a struct to represent a high-precision number, which has an array of a, a[0] for the length of the number, a[1] is a bit, a[2 is 10 digits, and so on.


three. High precision of pressure position

The essence is the same, except that it is stored in a different way from the way it is exported.

Like number 103.

Using 10-bit high precision storage a[0]=3,a[1]=3,a[2]=0,a[3]=1

Using the high precision of the hundred to save A[0]=2,a[1]=3,a[2]=1

Note that the output time a[1] can not only output a 3, but to output 03.


four. Code implementation

#include <cstdio> #include <cstring> #include <algorithm> usingnamespacestd; constintpower=1;   //the number of bits per operation is 10, defined here to facilitate program implementation constintbase=10;   // 10 power of the second party. To press the position, just change the power and base can, such as pressure million high precision, then power=4,base=10000 constintmaxl=1001;  //array length. CHARA[MAXL],B[MAXL]; Structnum {  inta[MAXL];   num () {memset (a,0,sizeof (a));}            //initialization   num (char*s)                         //Initializes a string to a high-precision number   {    memset (a,0,sizeof (a));      intlen=strlen (s);     a[0]= (len+power-1)/power;           Length of  //number     for (inti=0,t=0,w;i<len;w*=10,++i)           {&NBSP;&NBSP;&NBSP;&NBsp;  if (i%power==0) {w=1,++t;}       a[t]+=w* (s[i]-' 0 '); &NBSP;&NBSP;&NBSP;&NBSP}     //Initialize the array, simulate it yourself, it should be easy to understand the ~   }   voidadd ( INTK) {if (k| | A[0]) a[++a[0]]=k;   //adds a number to the end and uses   voidre () {reverse (a+1,a+a[0]+1) for division.          //the number in turn, the division will use the   voidprint ()                         //Print this high-precision number   {    printf ("%d", a[a[0]);         //first print the highest bit, in order to pressure bit or the high-precision number of 0 consider     for (inti=a[0]-1;i>0;--i)      printf ("%0*d", Power,a[i]);      //Here "%0*d", power means that power must be output, not enough to make up the front with 0      printf ("\ n");   }}p,q,ans; booloperator< (constnum&p,constnum&q)        //judgment is less than relationship, useful when dividing {  if (p.a[0]<q.a[0]) returntrue;   if (p.a[0]>q.a[0)) Returnfalse;   for (inti=p.a[0];i>0;--i)   {    if (P.a[i]!=q.a[i]) returnp.a[i]< Q.a[i];   }   returnfalse; numoperator+ (constnum&p,constnum&q)        //addition, do not say it, the simulation, it is easy to understand {   numc;   c.a[0]=max (P.a[0],q.a[0]);   for (inti=1;i<=c.a[0];++i)   {    c.a[i]+=p.a[i]+q.a[i];      c.a[i+1]+=c.a[i]/base;     c.a[i]%=base; &NBSP;&NBSP}   if (C.a[c.a[0]+1]) ++c.a[0];   returnc; numoperator-(constnum&p,constnum&q)        //subtraction, also needless to say, the simulation, very easy to understand {   numc=p;   for (inti=1;i<=c.a[0];++i)   {    c.a[i]-=q.a[i];      if (c.a[i]<0) {c.a[i]+=base;--c.a[i+1];}   }   while (C.a[0]>0&&!c.a[c.a[0]])--c.a[0];        // My habit is that if the number is 0, then his length is also 0, which makes it easier to compare sizes and add numbers at the end.   returnc; numoperator* (CONSTNUM&AMP;P,CONSTNUM&AMP;Q)          //multiplication, or simulate again. In fact, high precision is simulated artificial arithmetic. {  numc;   c.a[0]=p.a[0]+q.a[0]-1;   for (inti=1;i<=p.a[0];++i)   for ( INTJ=1;J&LT;=Q.A[0];++J)   {    c.a[i+j-1]+=p.a[i]*q.a[j];      c.a[i+j]+=c.a[i+j-1]/base;     c.a[i+j-1]%=base; &NBSP;&NBSP}   if (C.a[c.a[0]+1]) ++c.a[0];   returnc; numoperator/(CONSTNUM&AMP;P,CONSTNUM&AMP;Q)        //division, here I explain a little bit about {   numx,y;   for (inti=p.a[0];i>=1;--i)            // Starting from the highest bit   {    y.add (P.a[i]);      Add the number to the end (lowest), at which point the high is in front, the low in the rear     y.re ();           //the number in turn, into a unified storage mode: Low in front, high in the rear     while (! Y&LT;Q))     //is greater than or equal to the divisor, if less than, in fact, the answer on that bit is the original "0"       y=y-q,+ +x.a[i]; //see can subtract several divisor, reduce several times, the answer on that bit adds several times.     y.re ();           //the number in turn, preparing for the next add number   }   x.a[0]=p.a[0];   while (X.a[0]>0&&!x.a[x.a[0]])--x.a[0];   returnx; } intmain () {  scanf ("%s", a); &NBSP;&NBSP;SCANF ("%s", b);   reverse (A,a+strlen (a));    Reverse (B,b+strlen (b));   p=num (a), q=num (b);   ans=p+q;   ans.print ();   ans=p-q;   ans.print ();   ans=p*q;   ans.print ();   ans=p/q;   ans.print (); }

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.