0%

C++中的高精度运算

c++中的几种高精度运算

A+B problem

P1601 A+B Problem(高精) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<int> add(vector<int> &A , vector<int> &B){
vector<int> c;
if(A.size()<B.size()) return add(B,A);

int t=0; //进位
for(int i=0;i<A.size();i++){
t+=A[i];
if(i<B.size()) t+=B[i];
c.push_back(t%10);
t/=10;
}
if(t) c.push_back(1);
return c;

}

int main(){
string a,b;cin>>a>>b;
vector<int> A,B;

for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');
for(int i=b.size()-1;i>=0;i--) B.push_back(b[i]-'0');

auto c = add(A,B);

for(int i=c.size()-1;i>=0;i--){
printf("%d",c[i]);
}

return 0;
}

A-B problem

P2142 高精度减法 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
using namespace std;

//判断是否有A >= B
bool cmp(vector<int> &A,vector<int> &B){
if(A.size()!=B.size()) return A.size() > B.size();
for(int i=A.size()-1;i>=0;i++){
if(A[i]!=B[i])
return A[i]>B[i];
return true;
}
}

vector<int> sub(vector<int> &A, vector<int> &B){
vector<int> c;
int t=0; //进位
for(int i=0;i<A.size();i++){
t = A[i] + t;
if(i<B.size()) t-=B[i];
c.push_back((t+10)%10);
if(t<0) t=-1;
else t=0;
}
//除去前导0
while(c.size()>1&&c.back()==0) c.pop_back();
return c;

}

int main(){
string a,b; cin>>a>>b;

vector<int> A,B;
for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');
for(int i=b.size()-1;i>=0;i--) B.push_back(b[i]-'0');
auto c = sub(A,B);
if(cmp(A,B)) {
auto c = sub(A,B);
for(int i=c.size()-1;i>=0;i--) printf("%d",c[i]);
}
else{
printf("-");
auto c = sub(B,A);
for(int i=c.size()-1;i>=0;i--) printf("%d",c[i]);
}


return 0;

}

A*B problem

P1303 A*B Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

  • 高精度×高精度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
using namespace std;
vector<int> mul(vector<int> &A, vector<int> &B) {
if (A.size() < B.size()) return mul(B, A);
vector<int> c;
c.assign(A.size() + B.size(), 0);
int t = 0; //进位
for (int i = 0; i < B.size(); i++) {
for (int j = 0; j < A.size() || t; j++) {
c[i + j] += A[j] * B[i] + t;
t = c[i + j] / 10;
c[i + j] %= 10;
}
}
while (c.size() > 1 && c.back() == 0) c.pop_back();
return c;
}


int main() {
string a, b;
cin >> a >> b;
vector<int> A, B;
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
auto c = mul(A, B);
for (int i = c.size() - 1; i >= 0; i--) printf("%d", c[i]);
return 0;
}

高精度×长整数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>

using namespace std;

vector<int> mul(vector<int> &A, long long b) {
vector<int> c;
long long t = 0; //进位
for (int i = 0; i < A.size() || t; i++) {
if (i < A.size()) t += A[i] * b;
c.push_back(t % 10);
t /= 10;
}
while (c.size() > 1 && c.back() == 0) c.pop_back();
return c;

}
int main() {
string a;
cin >> a;
long long b;
cin >> b;
vector<int> A;
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
auto c = mul(A, b);
for (int i = c.size() - 1; i >= 0; i--) printf("%d", c[i]);
return 0;

}

A/B problem

P1480 A/B Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


vector<int> div(vector<int> &A, long long b, long long &r) {
vector<int> c;
r = 0;//余数
for (int i = A.size() - 1; i >= 0; i--) {
r = r * 10 + A[i];
c.push_back(r / b);
r %= b;
}
reverse(c.begin(), c.end());
while (c.size() > 1 && c.back() == 0) c.pop_back();
return c;
}
int main() {
string a;
cin >> a;
long long b;
cin >> b;
long long r = 0;
vector<int> A;
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
auto c = div(A, b, r);
for (int i = c.size() - 1; i >= 0; i--) printf("%d", c[i]);

return 0;

}

欢迎关注我的其它发布渠道