sicily 1029

大数相加

#include <iostream> #include <cstring> using namespace std; class Data { public: Data() { memset(a, 0, sizeof(a)); size = 0; } Data(int num) { setNum(num); } int a[100]; int size; Data operator =(Data tmp) { memcpy(a, tmp.a, sizeof(tmp.a)); size = tmp.size; return *this; } friend Data operator +(Data x, Data y) { Data tmp; int p = 0; //进位 int i = 0; tmp.size = x.size > y.size ? x.size : y.size; for(; i < tmp.size; i++) { tmp.a[i] = x.a[i] + y.a[i] + p; p = tmp.a[i] / 10; tmp.a[i] %= 10; } if(p != 0) { tmp.a[i] = p; (tmp.size)++; } return tmp; } void print() { for(int i = size - 1; i >= 0; i--) cout << a[i]; cout << endl; } void setNum(int num) { memset(a, 0, sizeof(a)); int i = 1; a[0] = num % 10; num /= 10; for(; num != 0; i++) { a[i] = num % 10; num /= 10; } size = i; } }; Data child[12]; int n, m; Data father; int main() { while(cin >> n >> m && !(n == 0 && m == 0)) { father.setNum(1); memset(child, 0, sizeof(child)); for(int i = 1; i <= m; i++) { for(int j = n + 1; j > 1; j--) child[j] = child[j - 1]; father = father + child[n + 1]; child[1] = father; } for(int i = 1; i <= n; i++) father = father + child[i]; father.print(); } return 0; } 

你可能感兴趣的:(sicily 1029)