- A+B Problem
- 2014-11-26 17:11:15 @
这题真的好难啊……我研究了3天3夜才打出来的奇丑无比的代码……
#include <cstdio>
using namespace std;
char n[2][10000005];int len, i;
inline void swap(char &x, char &y) {
char t = x; x = y; y = t;
}
inline void scan(bool t) {
char sc; while (((sc = getchar()) < '0' || sc > '9') && sc ^ '-');
bool f; int l = 0; if (!(f = sc == '-')) n[t][l++] = sc - '0';
while ((sc = getchar()) >= '0' && sc <= '9') n[t][l++] = sc - '0';
for (i = 0; i < l >> 1; ++i) swap(n[t][i], n[t][l - i - 1]);
if (f) for (i = 0; i < l; ++i) n[t][i] = -n[t][i];
if (l > len) len = l;
}
int main() {
scan(0); scan(1);
for (i = 0; i < len; ++i) n[0][i] += n[1][i];
while (!n[0][len - 1] && len > 1) --len;
if (n[0][len - 1] < 0) for (putchar('-'), i = 0; i < len; ++i) n[0][i] = -n[0][i];
for (i = 0; i < len; ++i) {
while (n[0][i] < 0) n[0][i] += 10, --n[0][i + 1];
while (n[0][i] > 9) n[0][i] -= 10, ++n[0][i + 1];
}
if (n[0][len]) ++len;
while (!n[0][len - 1] && len > 1) --len;
for (i = len - 1; ~i; --i) putchar(n[0][i] + '0');
putchar('\n');
return 0;
}
5 条评论
-
dush LV 4 @ 2015-10-06 21:01:28
Orz
-
2014-11-26 21:00:42@
ORZ---------------------高精 NMB,这就一普通加法
-
2014-11-26 20:08:21@
#include <iostream>
#include <string>
#include <algorithm>
#include <cstddef>using namespace std;
string operator+(string a, string b)
{
if (a.size() < b.size()) {
swap(a, b);
}bool carry = false;
for (size_t i = 0; i < a.size(); ++i) {
string::reverse_iterator ia = a.rbegin() + i;
if (i < b.size()) {
string::reverse_iterator ib = b.rbegin() + i;
*ia += (*ib - '0');
}
if (carry) {
++*ia;
}
if (*ia > '9') {
*ia -= 10;
carry = true;
} else {
carry = false;
}
}if (carry) {
a = "1" + a;
}return a;
}int main()
{
string a, b;
cin >> a >> b;
cout << a + b << endl;
} -
2014-11-26 19:16:07@
OrzOrzOrzOrzOrzOrzOrzOrzOrz
跪神犇的高精度!!! -
2014-11-26 17:43:21@
orz
- 1
信息
- ID
- 1000
- 难度
- 9
- 分类
- (无)
- 标签
- (无)
- 递交数
- 74311
- 已通过
- 28434
- 通过率
- 38%
- 被复制
- 217