AcWing 1. A + B
原题链接
简单
用类实现高精度加法
C++ 代码
#include <iostream>
using namespace std;
class BIGINT {
public:
int num[10000],length;
BIGINT();
friend istream & operator>>(istream &,BIGINT &);
friend ostream & operator<<(ostream &,BIGINT &);
BIGINT operator+(BIGINT);
};
BIGINT::BIGINT() {
for(int i = 0;i < 10000;i ++) num[i] = 0;
length = 0;
}
istream& operator>>(istream &in,BIGINT &b) {
string s;
in >> s;
for(int i = s.length() - 1;i >= 0;i --)
b.num[s.length() - 1 - i] = s[i] - '0';
b.length = s.length();
return in;
}
ostream& operator<<(ostream &out,BIGINT &b) {
for(int i = b.length - 1;i >= 0;i --) out << b.num[i];
return out;
}
BIGINT BIGINT::operator+(BIGINT b) {
int x = max(b.length,length),add = 0;
BIGINT c;
for(int i = 0;i <= x;i ++) {
c.num[i] = b.num[i] + num[i] + add;
if(c.num[i] > 9) add = 1,c.num[i] -= 10;else add = 0;
}
if(c.num[x] != 0) c.length = x + 1;else c.length = x;
return c;
}
int main() {
BIGINT a,b,c;
cin >> a >> b;
c = a + b;
cout << c;
return 0;
}