"Linear scan + high precision" Large integer divided by small integer to find remainder, decimal division

Source: Internet
Author: User
Tags assert
divide large integers by small integers to find remainder

The large integer divided by the small integer, the method for finding the remainder of the
inline int div (int a[], int n, int m) {//m=7 For example
  int ys = 0;
  for (int i = 0; i<n; i++) {
   ys*=10, ys+=a[i]; 
   ys%=m;
  }
  return ys;
}
Verify

void Testdiv () {
  int b[5] = {7, 9, 8, 0, 1};
  Assert (Div (b, 5, 1) ==0);
  Assert (Div (b, 5, 2) ==1);
  Assert (Div (b, 5, 3) ==1);
  Assert (Div (b, 5, 7) ==1);
}

Large integer Decimal Division

{int as[], int e, int k} represent a decimal. E represents the first decimal place subscript. K denotes the last subscript (including decimals).


Many for loops. A typical linear scan.

#include <iostream> #include <stdio.h> #include <string.h> #define MAXN using namespace std; 
  char* XJ (char *buf) {return buf;} void divide (int a, int b) {int AS[MAXN], CS[MAXN];
  int d=a/b;
  memset (as, 0, sizeof (AS));
  
  memset (CS, 0, sizeof (CS));
  Loading a= "as int i=maxn-1, j=0; 
  while (a>0) {as[j++]=a%10; a/=10;
  } j--;
    for (; i>=0&&j>=0; i--, j--) {as[i]=as[j];
  as[j]=0;
  }//as/b = "cs, note CS.E = AS.E, there is an extra 0 in front of CS.
  int e1= I, E2;
  Long long int c=0, t;
    for (i=maxn-1; i>=0; i--) {c*=10;
    t = c+as[i];
    cs[i]=t/b;
    C=t%b;
  if (i<=e1+1&&c==0) break;
  } if (i<0) i=0;
  
  int k = i;  
  CS = "Buf char buf[maxn+2];
  for (i=maxn-1; i>e1+1; i--)//Skip Excess 0 if (cs[i]!=0) break;
  for (j=0; i>e1; I--, j + +) {//Record integer part buf[j]= ' 0 ' +cs[i];  } if (i>=k) buf[j++]= '. ';
  Whether there is a small number of parts e2=j; for (i=e1; i>=k && j<maxn+1; I--, j + +) {//Decimal part//cout<<i<< ":" <<cs[i]<<endl; 
  buf[j]= ' 0 ' +cs[i];

  } buf[j]=0;
  Print BUF cout<<d;
    if (e2<j) {cout<< ".";
  cout<< (BUF+E2);
} cout<<endl;
  } int main () {int a=2, b=3;
  cout<< "input a B" <<endl;
    while (~SCANF ("%d%d", &a, &b)) {cout<<a<< "/" <<b<< ":" <<endl;
  Divide (A, b);
} return 0;
 }


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.