模拟与高精度 P1601 A+B Problem(高精)
题目
https://www.luogu.com.cn/problem/P1601
题目分析
不用大数结构体,直接用string的话要注意补零
代码
#include<iostream> #include<string> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> using namespace std; vector<int>p; void add(string a, string b) { int t = 0; for (int i = 0; i < a.length() && i < b.length(); i++) { p.push_back((t + a[i] - '0' + b[i] - '0')%10); t = (t + a[i] - '0' + b[i] - '0')/10; } if (t != 0) { p.push_back(t % 10); } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); string a, b; cin >>a >> b; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int alen = a.length(), blen = b.length(); if (alen> blen) for (int i = 0; i < alen-blen; i++)b = b + '0'; if (alen<blen) for (int i = 0; i <blen-alen; i++)a = a + '0'; add(a, b); for (int i = p.size() - 1; i >= 0; i--) cout << p[i]; }