424 - Integer Inquiry
简单的高精度加法,LRJ白书的模板。只是脑残了下,忘记回车了
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn=1000; struct bign { int len,s[maxn],flag;//flag 为表示正负备用 bign() {memset(s,0,sizeof(s));len=1;} bign operator = (const char* num) { len=strlen(num); for(int i=0;i<len;i++) s[i]=num[len-i-1]-'0'; return *this; } bign operator = (int num) { char s[maxn]; sprintf(s,"%d",num); *this=s; return *this; } bign(int num){ *this=num; } bign(const char* num){ *this=num;} string str() const { string res=""; for(int i=0;i<len;i++) res=(char)(s[i]+'0')+res; if(res=="") res="0"; return res; } bign operator + (const bign& b) const { bign c; c.len=0; int max=0; if(len>b.len) max=len; else max=b.len; for(int i=0,g=0;g || i<max;i++) { int x=g; if(i<len) x+=s[i]; if(i<b.len) x+= b.s[i]; c.s[c.len++]=x%10; g=x/10; } return c; } }; istream& operator >> (istream &in, bign& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream &out, const bign& x) { out << x.str(); return out; } int main() { bign sum=0; char input[500]; while(scanf("%s",&input)&&strcmp(input,"0")) { bign plus; plus=input; sum=sum+plus; } cout<<sum<<endl;//要加回车,唯一的坑 (这哪里是坑?明明是你自己的问题嘛!) return 0; }