AcWing algorithm base 1.4

High Precision

Most is these four, there are two high-precision template addition, high-precision subtraction, multiplication precision low accuracy, precision divided by low accuracy, probably usually use (because I'm not very good vector, it is also an array He wrote, 23333)

Artificial hand calculations and arithmetic precision similar process simulation artificial hand is calculated, bit multiplication, as mentioned below, the analog operation is substantially

Ado, on the template

Precision adder

 1 // C = A + B, A >= 0, B >= 0
 2 vector<int> add(vector<int> &A, vector<int> &B)
 3 {
 4     if (A.size() < B.size()) return add(B, A);
 5     
 6     vector<int> C;
 7     int t = 0;
 8     for (int i = 0; i < A.size(); i ++ )
 9     {
10         t += A[i];
11         if (i < B.size()) t += B[i];
12         C.push_back(t % 10);
13         t /= 10;
14     }
15     
16     if (t) C.push_back(t);
17     return C;
18 }

 

High-precision subtraction

 1 // C = A - B, 满足A >= B, A >= 0, B >= 0
 2 vector<int> sub(vector<int> &A, vector<int> &B)
 3 {
 4     vector<int> C;
 5     for (int i = 0, t = 0; i < A.size(); i ++ )
 6     {
 7         t = A[i] - t;
 8         if (i < B.size()) t -= B[i];
 9         C.push_back((t + 10) % 10);
10         if (t < 0) t = 1;
11         else t = 0;
12     }
13 
14     while (C.size() > 1 && C.back() == 0) C.pop_back();
15     return C;
16 }

In the case where the subtraction precision A <B output of a next sentence we Laid - like, the ninth line template is more clever step, because we reduce a range of t is (-10,10) t may be a positive number may also be negative for positive numbers we can be directly used for the previous negative to enter a 1, equivalent to an increase of 10 t, here written as (t + 10)% 10 will be able to both cases write together

 

Low precision by precision

 1 // C = A * b, A >= 0, b > 0
 2 vector<int> mul(vector<int> &A, int b)
 3 {
 4     vector<int> C;
 5     int t = 0;
 6     for (int i = 0; i < A.size() || t; i ++ )
 7     {
 8         if (i < A.size()) t += A[i] * b;
 9         C.push_back(t % 10);
10         t /= 10;
11     }
12     
13     return C;
14 }

Multiplication and simulation of us considered a little difference, for example, 156 * 23 count the first time directly take 6 * 23, * 23 + 5 then let's carry, and so on

 

Dividing the low accuracy precision

 1 // A / b = C ... r, A >= 0, b > 0
 2 vector<int> div(vector<int> &A, int b, int &r)
 3 {
 4     vector<int> C;
 5     r = 0;
 6     for (int i = A.size() - 1; i >= 0; i -- )
 7     {
 8         r = r * 10 + A[i];
 9         C.push_back(r / b);
10         r %= b;
11     }
12     reverse(C.begin(), C.end());
13     while (C.size() > 1 && C.back() == 0) C.pop_back();
14     return C;
15 }

 

The above is the template vector, for I will not use vector with an array wrote, uh uh uh uh ....

Precision adder

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring> 
 4 
 5 using namespace std;
 6 int add(int a[], int b[]);
 7 const int N = 100000 + 10;
 8 char x1[N], x2[N];
 9 int a[N], b[N], c[N];
10 
11 int main()
12 {
13     scanf("%s%s",x1,x2);
14     int len1 = strlen(x1), len2 = strlen(x2);
15     for(int i = len1 - 1, j = 0; i >= 0; i --, j ++)
16         a[j] = x1[i] - '0';
17     for(int i = len2 - 1, j = 0; i >= 0; i --, j ++)
18         b[j] = x2[i] - '0';
19       
20     int id = add(a, b);
21     for(int i = id - 1; i >= 0; i --)
22         printf("%d",c[i]);
23     return 0;  
24     //add(a,b);
25     
26 }
27 
28 int add(int a[], int b[])
29 {
30     int mx = 0, id = 0;
31     if(strlen(x1) >= strlen(x2))  mx = strlen(x1);
32     else  mx = strlen(x2);
33     int t = 0;
34     for(int i = 0; i < mx; i ++)
35     {
36         c[id++] = (a[i] + b[i] + t) % 10;
37         t = (a[i] + b[i] + t) / 10;
38     }
39     if(t)  c[id++] = t;
40     return id;
41 }

 

High-precision subtraction

. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include <CString> 
 . 4  
. 5  the using  namespace STD;
 . 6  int Mun ( int A [], int B []);
 . 7  BOOL CMP ( int A [], int B []); // for comparing a, b who is large, the output is determined positive or negative
 . 8  const  int N = 100000 + 10 ;
 . 9  char X [N], Y [N];
 10  int A [N], B [N], C [N];
 . 11  
12 is  int main ()
 13 is  {
14     scanf("%s%s",x,y);
15     int lena = strlen(x), lenb = strlen(y);
16     for(int i = lena - 1, j = 0; i >= 0; i --, j ++ )    a[j] = x[i] - '0';
17     for(int i = lenb - 1, j = 0; i >= 0; i --, j ++ )    b[j] = y[i] - '0';
18     
19     if(cmp(a, b))  cout << "-";
20     int temp = mun(a, b);
21     for(int i = temp - 1; i >= 0; i -- )
22         printf("%d",c[i]);
23     return 0;
24 }
25 
26 int mun(int a[], int b[])
27 {
28     if(cmp(a, b))  return (mun(b, a));
29     
30     int t = 0, mx = max(strlen(x), strlen(y));
31     for(int i = 0; i < mx; i ++ )
32     {
33         t = a[i] - b[i] + t;
34         c[i] = (t + 10) % 10;
35         if(t < 0)  t = -1;
36         else t = 0;
37     }
38     
39     int id = mx;
40     for(int i = mx - 1; i > 0; i --) // remove leading zeros
 41 is      {
 42 is          IF (c [I] == 0 ) ID - ;
 43 is          the else   BREAK ;
 44 is      }
 45      return ID; ID // c is the length of the array
 46 is  }
 47  
48  BOOL CMP ( int A [], int B [])
 49  {
 50      int MX = max (strlen (X), strlen (Y));
 51 is      for ( int I = MX - . 1 ; I> = 0 ; I - )
 52 is      {
 53 is         if(a[i] > b[i])  return 0;
54         if(a[i] < b[i])  return 1;
55     }
56     return 0;
57 }

 

Precision multiplication

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 
 6 using namespace std;
 7 int mul(int a[], int b);
 8 const int maxa = 100000 + 10;
 9 char x[maxa];
10 int a[maxa], c[maxa], b;
11 
12 int main()
13 {
14     scanf("%s%d",x,&b);
15     int lena = strlen(x);
16     for(int i = lena - 1, j = 0; i >= 0; i --, j ++ )  a[j] = x[i] - '0';
17     
18     int id = mul(a, b);
19     for(int i = id - 1; i >= 0; i -- )
20         cout << c[i];
21     return 0;
22 }
23 
24 int mul(int a[], int b)
25 {
26     int t = 0, id = 0, lena = strlen(x);
27     for(int i = 0; i < lena; i ++ )
28     {
29         t = a[i] * b + t;
30         c[id++] = t % 10;
31         t = t / 10;
32     }
33     if(t)  c[id++] = t;
34     return id;
35 }

 

Guess you like

Origin www.cnblogs.com/chuyds/p/10972974.html