高精度加法模板,包装结构体


高精度加法模板,包装结构体

原创 已于 2024-04-19 19:05:30 修改 · 粉丝可见 · 418 阅读 · 9 · 5 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 GEO检测 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/135950322

本题链接: 用户登录

题目:

样例:

cobol<br/>123 456<br/>
579

模板结构体:

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
 
struct BigInt
{
string values;
inline BigInt Add(const BigInt &ta,const BigInt &tb)
{
BigInt ans;
ans.values = "";
string sa = ta.values;
string sb = tb.values;
int alen = sa.size();
int blen = sb.size();
vector<int>a,b,c;
for(int i = alen - 1;~i;--i) a.emplace_back(sa[i] - '0');
for(int i = blen - 1;~i;--i) b.emplace_back(sb[i] - '0');
int t = 0;
for(int i = 0;i < alen || i < blen;++i)
{
if(i < alen) t += a[i];
if(i < blen) t += b[i];
c.emplace_back(t % 10);
t /= 10;
}
while(t > 0)
{
c.emplace_back(t % 10);
t /= 10;
}
int clen = c.size();
for(int i = clen - 1;~i;--i) ans.values += char(c[i] + '0');
return ans;
}
friend inline istream& operator>>(istream&cin,BigInt& t)
{
cin >> t.values;
return cin;
}
friend inline ostream& operator<<(ostream&cout,BigInt t)
{
cout << t.values;
return cout;
}
inline BigInt operator+(BigInt t)
{
return Add(*this,t);
}
};

思路:

大整数,高精度,运算原理跟我们小学进行的笔算原理一样的,模拟一遍即可。

代码详解如下:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstring>
#include <vector>
#define endl '\n'
#define int long long
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

struct BigInt
{
string values;
inline BigInt Add(const BigInt &ta,const BigInt &tb)
{
BigInt ans; // 建立返回值
ans.values = ""; // 返回值初始化

string sa = ta.values; // 取出整数 a 的值
string sb = tb.values; // 取出整数 b 的值

int alen = sa.size(); // 获取整数 a 的位数
int blen = sb.size(); // 获取整数 b 的位数

vector<int>a,b,c; // 定义存储计算过程的数组

// 将字符串反向转化为数组方便运算。
// 由于string是由高位到低位的存储,所以我们反向,方便运算个位等低位的运算
for(int i = alen - 1;~i;--i) a.emplace_back(sa[i] - '0');
for(int i = blen - 1;~i;--i) b.emplace_back(sb[i] - '0');

int t = 0; // 临时变量 t 存储同一位数上的累加结果
// 开始累加过程
for(int i = 0;i < alen || i < blen;++i)
{
// 如果对应的位数存在,那么开始累加
if(i < alen) t += a[i];
if(i < blen) t += b[i];

// 取出当前位的累加结果
c.emplace_back(t % 10);

// 除掉当前位,若可以进位则进位
t /= 10;
}
// 如果计算结果最后还有进位,我们就开始进位
while(t > 0)
{
// 取出当前位的累加结果
c.emplace_back(t % 10);

// 除掉当前位,若可以进位则进位
t /= 10;
}


int clen = c.size(); // 获取最终计算结果位数
// 颠倒重新由高位存储低位
for(int i = clen - 1;~i;--i) ans.values += char(c[i] + '0');
return ans; // 返回结果
}

// 重载输入
friend inline istream& operator>>(istream&cin,BigInt& t)
{
cin >> t.values;
return cin;
}
// 重载输出
friend inline ostream& operator<<(ostream&cout,BigInt t)
{
cout << t.values;
return cout;
}
// 重载累加
inline BigInt operator+(BigInt t)
{
return Add(*this,t);
}
};
inline void solve()
{
BigInt a,b;
cin >> a >> b;
cout << (a + b) << endl;
}
signed main()
{
IOS;
int ___t = 1;
// cin >> ___t;
while(___t--)
{
solve();
}

return 0;
}

最后提交:


觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭

微信二维码

wechat pay

支付宝二维码

ali pay

高精度加法模板,包装结构体
http://blog.angindem.cn/2024/04/19/Angindem-CSDN博客/132_132/
作者
Angindem
发布于
2024年4月19日
许可协议