戏雷chnos.blog.chinaunix.net
chnos
全部博文(41)
ACM UVA题(38)
2012年(8)
2011年(1)
2009年(32)
止觞
分类:
2009-04-16 20:14:35
/* ******************************************************************************* * * Filename: 10007.cpp * * Description: Use Catalan numbers. (2n)!/(n+1)! * * Version: 0.1 * Created: 4/14/2009 8:22:16 PM * * Author: Ye Xiaofeng , yexfeng # gmail.com * ******************************************************************************* */ #include <iostream> #include <vector> #include <string> using namespace std; class BigInt { private: vector<short> v; void add(BigInt& bint); int at(int i); public: BigInt(int val); BigInt(BigInt& bigInt); BigInt& operator=(BigInt& bint); BigInt& operator=(int val); BigInt& operator+=(BigInt& bint); BigInt& operator+=(int val); BigInt& operator--(); BigInt& operator*=(int val); BigInt& operator*=(BigInt& bint); bool operator!=(BigInt& bint); int len(); string getStr() const; }; int BigInt::len() { return v.size(); } int BigInt::at(int pos) { if (pos < v.size()) { return v[pos]; } else { return -1; } } BigInt::BigInt(BigInt& bigInt) { v = bigInt.v; } BigInt& BigInt::operator--() { for (int i = 0; i < v.size(); i++) { if (i == 0 || (i > 0 && v[i-1] == 9)) { if (v[i] == 0) { v[i] = 9; } else { v[i]--; } } } if (v[v.size()-1] == 0) { v.pop_back(); } return *this; } void BigInt::add(BigInt& bint) { int carryFlg = 0; for (int i = 0; i < v.size() || i < bint.v.size(); i++) { if (i >= v.size()) { v.push_back(bint.v[i]); } else if (i < v.size() && i < bint.v.size()) { v[i] += bint.v[i]; } v[i] += carryFlg; if (v[i] > 9) { v[i] -= 10; carryFlg = 1; } else { carryFlg = 0; } } if (carryFlg == 1) { v.push_back(1); } } BigInt& BigInt::operator+=(BigInt& bint) { add(bint); return *this; } BigInt& BigInt::operator+=(int val) { BigInt tmpInt(val); add(tmpInt); return *this; } BigInt& BigInt::operator=(BigInt& bigInt) { this->v = bigInt.v; return *this; } BigInt& BigInt::operator=(int val) { BigInt tmpInt(val); *this = tmpInt; return *this; } BigInt::BigInt(int val) { do { v.push_back(val%10); val = val / 10; } while (val != 0); } string BigInt::getStr() const { string tmpStr; for (int i = 0; i < v.size(); i++) { char c = v[i] + '0'; tmpStr.push_back(c); } return tmpStr; } BigInt& BigInt::operator*=(int val) { BigInt tmpInt(val); *this *= tmpInt; return *this; } BigInt& BigInt::operator*=(BigInt& bint) { BigInt tmpInt1(*this); int carryVal = 0; *this = 0; for (int i = 0; i < bint.len(); i++) { int x = bint.at(i); BigInt tmpInt2(tmpInt1); for (int j = 0; j < tmpInt2.len(); j++) { tmpInt2.v[j] *= x; tmpInt2.v[j] += carryVal; carryVal = tmpInt2.v[j] / 10; tmpInt2.v[j] = tmpInt2.v[j] % 10; } if (carryVal != 0) { tmpInt2.v.push_back(carryVal); } carryVal = 0; add(tmpInt2); tmpInt1.v.insert(tmpInt1.v.begin(), 0); } return *this; } ostream& operator << (ostream &os, const BigInt& bigInt) { string tmpStr = bigInt.getStr(); for (int i = tmpStr.size()-1; i >= 0; i--) { cout << tmpStr[i]; } return os ; } BigInt operator*(BigInt& bint1, int val) { BigInt tmpInt(bint1); return tmpInt; } int main(int argc, char **argv) { int nodeNum = 0; cin >> nodeNum; while (nodeNum != 0) { if (nodeNum == 1) { cout << 1 << endl; } else { BigInt bint(1); for (int i = nodeNum+2; i <= 2*nodeNum; i++) { bint *= i; } cout << bint << endl; } cin >> nodeNum; } }
上一篇:使用Windbg发现了Windows的一个Bug ?
下一篇:ACM UVA (499)
登录 注册