您的位置:首页 > 产品设计 > UI/UE

Uva424 - Integer Inquiry

2013-10-28 12:16 295 查看
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 10005;

struct bign {
int len, s[maxn];
bign() { len = 0; memset(s, 0, sizeof(s)); s[0] = 0; }

bign operator = (const char* str) {
len = strlen(str);
for(int i = 1; i <= len; ++i) s[i] = str[len - i] - '0';
return *this;
}

bign operator = (const int num) {
char str[maxn];
sprintf(str, "%d", num);
*this = str;
return *this;
}

bign (int num) { *this = num; }
bign (const char* num) { *this = num; }

string str() const {
string res = "";
for(int i = len; i > 0; --i) res += (char)(s[i] + '0');
if(res == "") res = "0";
return res;
}

bign operator + (const bign& b) const {
bign x;
int k=0;
for(int i=1;i<=max(len,b.len)+1;i++){
k=s[i]+b.s[i]+k;
x.s[++x.len]=k%10;
k/=10;
}
if(x.s[x.len]==0) x.len--;
return x;
}

bign operator += (const bign& b) {
*this = *this + b;
return *this;
}
bool operator < (const bign& b) const {
if(len != b.len) return len < b.len;
for(int i = len - 1; i >= 0; --i) if(s[i] != b.s[i]) return s[i] < b.s[i];
return false;
}
bool operator != (const bign& b) const { return b < *this || *this < b; }
};

istream& operator >> (istream &in, bign& x) {
string s;
in >> s;
x = s.c_str();
return in;
}

ostream& operator << (ostream &out, bign& x) {
out << x.str();
return out;
}

int main() {
bign ans, num;
while(cin >> num && num != 0) ans += num;
cout << ans << endl;
return 0;
}

/***
Sample Input

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output

370370367037037036703703703670
***/


bign高精度,求几个大数的和。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: