高精度

回来第一件事先复习高精

  • 高精加:
  • 按位相加 依次进位 倒序输出
 1 #include <bits/stdc++.h>
 2 #define rep(i,a,b) for(int i = a;i <= b;i ++)
 3 #define rrep(i,a,b) for(int i = b;i >= a;i --);
 4 using namespace std;
 5 const int M = 1e5 + 1;
 6 string a,b;
 7 int c[700];
 8 void p(){
 9     int l1 = a.size();
10     int l2 = b.size();
11     int l = max(l1,l2);
12     rep(i,0,l - 1){
13         if((l1) != 0) c[i] += a[l1 - 1] - '0',l1 --;
14         if((l2) != 0) c[i] += b[l2 - 1] - '0',l2 --;
15         if(c[i] >= 10) c[i] -= 10,c[i + 1] += 1;
16     }
17     if(c[l]) l ++;
18     for(int i = l - 1;i >= 0;i --) cout << c[i];
19     return;
20 }
21 signed main(){
22     cin >> a;
23     cin >> b;
24     p();
25     return 0;
26 }
  • 高精乘:
  • 按位相乘 每次进位 倒序输出
  • 高乘高 高乘低 皆如此
 1 #include <bits/stdc++.h>
 2 #define rep(i,a,b) for(int i = a;i <= b;i ++)
 3 #define rrep(i,a,b) for(int i = b;i >= a;i --)
 4 using namespace std;
 5 const int M = 1e5 + 1;
 6 string s1,s2;
 7 int c[40000000];  
 8 int a[2001],b[2001];
 9 void p(){
10     int l1 = s1.size();
11     int l2 = s2.size();
12     int l = max(l1,l2);
13     int t = 0;
14     rrep(i,0,l1 - 1) a[++ t] = s1[i] - '0';
15     t = 0;
16     rrep(i,0,l2 - 1) b[++ t] = s2[i] - '0';
17     int cy = 0;
18     rep(i,1,l1){
19         rep(j,1,l2){
20             c[i + j - 1] += a[i] * b[j] + cy;
21             if(c[i + j - 1] >= 10) cy = c[i + j - 1] / 10,c[i + j - 1] %= 10;
22             else cy = 0;
23         }
24         if(cy) c[i + l2] += cy,cy = 0;
25     }
26     int tag = l1 + l2 + 1;
27     while(!c[tag] && tag >0) tag --;
28     rrep(i,1,tag) cout << c[i];
29     return;
30 }
31 signed main(){
32     cin >> s1;
33     cin >> s2;
34     if(s1[0] == '0' || s2[0] == '0'){
35         cout << 0 << endl;
36         return 0;
37     }
38     p();
39     return 0;
40 }

PS:需要纠正一个态度,以前看到高阶的有难度的算法,会觉得暂时用不上就不学了。现在不行,现在看见新东西就得学,否则永远无法真正提升自己。

posted @ 2024-09-21 11:22  DinoCrab  阅读(14)  评论(0)    收藏  举报