HDU-1041 Computer Transformation 大数
这题打表找下规律就可以了,定义一个变量来表示增量,那么这个变量的格律就是 add = (add ± 1) * 2
手写了大数的类,幸好只有-1这个值,这个类是没定义减法运算的。
代码如下:
#include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long int Int64; Int64 rec[65]; int N; struct BigInteger { char x[1005]; BigInteger () { memset(x, 0, sizeof (x)); } BigInteger operator + (BigInteger b); BigInteger operator * (int m); void print(); }ret[1005], Add; BigInteger BigInteger :: operator + (BigInteger b) { BigInteger ans; int lena = 0, lenb = 0, len; for (int i = 1000; i >= 0; --i) { if (x[i] && !lena) lena = i; if (b.x[i] && !lenb) lenb = i; if (lena && lenb) break; } len = max(lena, lenb); for (int i = 0; i <= len; ++i) { ans.x[i] += x[i] + b.x[i]; if (ans.x[i] >= 10) { ans.x[i+1] += ans.x[i] / 10; ans.x[i] %= 10; } } return ans; } BigInteger BigInteger :: operator * (int m) { BigInteger ans; int len = 0; for (int i = 1000; i >= 0; --i) { if (x[i] && !len) len = i; if (len) break; } for (int i = 0; i <= len; ++i) { ans.x[i] += x[i] * m; if (ans.x[i] >= 10) { ans.x[i+1] += ans.x[i] / 10; ans.x[i] %= 10; } } return ans; } void BigInteger :: print() { int len = 0; for (int i = 1000; i >= 0; --i) { if (x[i] && !len) len = i; if (len) break; } for (int i = len; i >= 0; --i) { printf("%d", x[i]); } puts(""); } BigInteger valueof(Int64 obj) { BigInteger ans; int i = 0; while (obj) { ans.x[i] = obj % 10; obj /= 10; ++i; } return ans; } int main() { ret[1] = valueof(0), ret[2] = ret[3] = valueof(1); Add = valueof(0); for (int i = 4; i <= 1000; ++i) { Add = (Add + valueof(i & 1 ? -1 : 1)) * 2; ret[i] = ret[i-1] + Add; } while (scanf("%d", &N) == 1) { ret[N].print(); } return 0; }