Files
python/GESP/Mkx/20250225_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

62 lines
1.4 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
/*
测试用例:
3479957928375817
897259321544245
答案4377217249920062
*/
const int N = 1e5 + 10; // 足够大的静态数组大小
const int WIDTH = 4; // 压位位数
const int BASE = 10000; // 压位基数10^4
int a[N], b[N]; // 静态数组存储
int al, bl; // 有效长度
// 字符串转压位数组(逆序存储)
void str2num(string s, int c[], int &cl) {
cl = 0;
for (int i = s.size() - 1; i >= 0; i -= WIDTH) {
int v = 0;
int st = max(i - WIDTH + 1, 0); // 最后一组可能不够4个长度
for (int j = st; j <= i; j++)
v = v * 10 + (s[j] - '0');
c[cl++] = v;
}
}
// 高精度加法(压位版)
void add(int a[], int &al, int b[], int &bl) {
// 确保a是较长的数组如果不是则交换a和b
// 确保a是较长的数组如果不是则交换a和b
al = max(al, bl);
int t = 0;
for (int i = 0; i < al; i++) {
t += a[i] + b[i];
a[i] = t % BASE;
t /= BASE;
}
// 处理最后的进位
if (t)
a[al++] = t;
}
int main() {
string x, y;
cin >> x >> y;
// 转换输入字符串为压位数组
str2num(x, a, al), str2num(y, b, bl);
// 执行加法
add(a, al, b, bl);
// 输出结果:最高位直接输出,后续位补前导零
for (int i = al - 1; i >= 0; i--)
printf("%d", a[i]); // 其他位保持4位宽度
printf("\n");
return 0;
}