HDU 1002. 高精度加法
原题链接
简单
作者:
史一帆
,
2021-04-08 21:07:19
,
所有人可见
,
阅读 214
C++ 代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;
int T;
vector<int> A, B;
string a, b;
vector<int> add(vector<int>& A, vector<int>& B)
{
if (A.size() < B.size()) return add(B, A);
vector<int> C;
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(t);
return C;
}
int main()
{
cin >> T;
for (int i = 1; i <= T; i++)
{
cin >> a >> b;
printf("Case %d:\n", i);
A.clear(), B.clear();
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');
vector<int> res = add(A, B);
cout << a << " + " << b << " = ";
for (int i = res.size() - 1; i >= 0; i--) cout << res[i];
if (i != T) printf("\n\n");
else printf("\n");
}
return 0;
}