Problem
逐位同时输入两个 $M$ 位的数,算出它们的和。多组数据。
什么是逐位同时输入呢?就是一起输入两个数的第 $i$ 位。要全部输入之后把两个数拼起来。
Solution
逐位读入到两个字符串中,然后加起来即可。
数据范围大,需要高精度。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
#define elif else if
#define il inline
namespace BI
{
struct Int
{
vector<int> d;//d[i] -> d[i] * 10^i
Int()
{
d.clear();
d.push_back(0);
}
template<typename T> Int(T x)
{
d.clear();
if (x == 0)
d.push_back(0);
while (x > 0)
d.push_back(x % 10), x /= 10;
}
int operator[](int x)
{
return d[x];
}
int size()
{
return d.size();
}
};
istream& operator>>(istream& in, Int &x)
{
string s;
in >> s;
x.d.assign(s.size(), 0);
for (int i = 0, j = s.size() - 1; i < s.size(); i++, j--)
x.d[j] = s[i] - '0';
return in;
}
ostream& operator<<(ostream& out, const Int &x)
{
for (int i = x.d.size() - 1; i >= 0; i--)
out << x.d[i];
return out;
}
bool operator<(Int x, Int y)
{
if (x.size() > y.size())//x比y长(比y大)
return false;
if (x.size() < y.size())//x比y短(比y小)
return true;
for (int i = x.size() - 1; i >= 0; i--)
if (x[i] < y[i])
return true;
else if (x[i] > y[i])
return false;
return false;
}
bool operator>(Int x, Int y)
{
return y < x;
}
bool operator==(Int x, Int y)
{
return !(x < y) && !(x > y);
}
bool operator<=(Int x, Int y)
{
return x == y || x < y;
}
bool operator>=(Int x, Int y)
{
return x == y || x > y;
}
Int operator+(Int x, Int y)
{
Int ans;
ans.d.assign(max(x.size(), y.size()) + 1, 0);
for (int i = 0; i < x.size(); i++)
ans.d[i] += x[i];
for (int i = 0; i < y.size(); i++)
ans.d[i] += y[i];
for (int i = 0; i < ans.size() - 1; i++)
ans.d[i + 1] += ans[i] / 10, ans.d[i] %= 10;
while (ans[ans.size() - 1] == 0 && ans.size() > 1)
ans.d.pop_back();
return ans;
}
Int operator+=(Int &x, Int y)
{
return x = x + y;
}
Int operator-(Int x, Int y)
{
if (x < y)
return y - x;
Int ans;
ans.d.assign(x.size(), 0);
for (int i = 0; i < x.size(); i++)
ans.d[i] += x[i];
for (int i = 0; i < y.size(); i++)
ans.d[i] -= y[i];
for (int i = 0; i < ans.size() - 1; i++)
if (ans[i] < 0)
ans.d[i] += 10, ans.d[i + 1]--;
while (ans[ans.size() - 1] == 0 && ans.size() > 1)
ans.d.pop_back();
return ans;
}
Int operator-=(Int &x, Int y)
{
return x = x - y;
}
Int operator*(Int x, Int y)
{
Int ans;
ans.d.assign(x.size() + y.size(), 0);
for (int i = 0; i < x.size(); i++)
for (int j = 0; j < y.size(); j++)
ans.d[i + j] += x[i] * y[j];
for (int i = 0; i < ans.size() - 1; i++)
ans.d[i + 1] += ans[i] / 10, ans.d[i] %= 10;
if (ans[ans.size() - 1] == 0)
ans.d.pop_back();
return ans;
}
Int operator*=(Int &x, Int y)
{
return x = x * y;
}
Int operator/(Int x, int y)
{
Int ans = x;
for (int i = ans.size() - 1; i >= 1; i--)
ans.d[i - 1] += (ans[i] % y) * 10, ans.d[i] /= y;
ans.d[0] /= y;
while (ans[ans.size() - 1] == 0 && ans.size() > 1)
ans.d.pop_back();
return ans;
}
Int operator/(Int x, Int y)
{
Int l = Int(0), r = x + Int(1);
while (l + Int(1) < r)
{
Int mid = (l + r) / 2;
if (mid * y <= x)
l = mid;
else
r = mid;
}
return l;
}
Int operator/=(Int x, Int y)
{
return x = x / y;
}
Int operator%(Int x, Int y)
{
return x - (x / y * y);
}
Int operator%=(Int &x, Int y)
{
return x = x % y;
}
Int s2I(string s)
{
Int x;
x.d.assign(s.size(), 0);
for (int i = 0, j = s.size() - 1; i < s.size(); i++, j--)
x.d[j] = s[i] - '0';
return x;
}
}
using namespace BI;
int t, s;
string as, bs;
char c;
Int ai, bi;
int main()
{
cin >> t;
while (t--)
{
cin >> s;
as = bs = "";
while (s--)
{
cin >> c;
as += c;
cin >> c;
bs += c;
}
ai = s2I(as);
bi = s2I(bs);
cout << ai + bi << endl;
if (t > 0)
cout << endl;
}
return 0;
}