大数加法、乘法实现的简单版本
有时候在外需要用大数..比如面试之类,对效率要求不高,需要的是容易记住的实现,这里有一份:
#define big string #define le length() big addBig(big x, big y) { if (x.le < y.le) swap(x, y); // x长而y短 for (int i = x.le - 1, j = y.le - 1; i >= 0; --i, --j) { if (j >= 0) x[i] += y[j] - '0'; if (x[i] > '9') { x[i] -= 10; if (i) x[i - 1]++; else x = '1' + x; } } return x; } big mulInt(big s, int x) { reverse(s.begin(), s.end()); int cmp = 0; for (int i = 0; i < (int)s.le; ++i) { cmp += (s[i] - '0') * x; s[i] = cmp % 10 + '0'; cmp /= 10; } while (cmp) { s += cmp % 10 + '0'; cmp /= 10; } reverse(s.begin(), s.end()); return s; } big mulBig(big x, big y) { big ans; for (int i = y.le - 1, j = 0; i >= 0; --i, ++j) { big tmp = mulInt(x, y[i] - '0'); for (int k = 0; k < j; ++k) tmp += '0'; ans = addBig(ans, tmp); } return ans; }