大数加法
hdu1002
#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 1005;const int MOD = 1e9 + 7;#define MemI(x) memset(x, -1, sizeof(x))#define Mem0(x) memset(x, 0, sizeof(x))#define MemM(x) memset(x, 0x3f, sizeof(x))#define lson l, m, rt <<1#define rson m + 1, r, rt <<1 | 1void Add(char a[MAXN], char b[MAXN], int alen, int blen){ int len = max(alen, blen); int aa[MAXN] = {0}, bb[MAXN] = {0}; for(int i = 0;i aa[i] = a[alen - i - 1] - '0'; for(int i = 0;i bb[i] = b[blen - i - 1] - '0'; int ans[MAXN]; Mem0(ans); for(int i = 0;i ans[i] = aa[i] + bb[i]; for(int i = 0;i { if(ans[i] > 9) { ans[i] -= 10; ans[i + 1]++; } } if(ans[len]) len++; bool flag = false; for(int i = len - 1;i >= 0;--i) { if(ans[i] != 0) flag = true; if(flag) cout < } if(!flag) cout <<0; cout <}int main(){ int T; while(cin >> T) { for(int cas = 1;cas <= T;++cas) { char a[MAXN], b[MAXN]; cin >> a >> b; if(cas != 1) cout < cout <<"Case " < Add(a, b, strlen(a), strlen(b)); } } return 0;}View Code 大数乘法51nod 1027 #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 1005;const int MOD = 1e9 + 7;#define MemI(x) memset(x, -1, sizeof(x))#define Mem0(x) memset(x, 0, sizeof(x))#define MemM(x) memset(x, 0x3f, sizeof(x))#define lson l, m, rt <<1#define rson m + 1, r, rt <<1 | 1char a[MAXN], b[MAXN];int ans[MAXN * MAXN] = {0};int main(){ cin >> a >> b; int alen = strlen(a), blen = strlen(b); for(int i = 0;i > 1;++i) swap(a[i], a[alen - i - 1]); for(int i = 0;i > 1;++i) swap(b[i], b[blen - i - 1]); for(int i = 0;i { for(int j = 0;j { ans[i + j] += (a[i] - '0') * (b[j] - '0'); ans[i + j + 1] += ans[i + j] / 10; ans[i + j] %= 10; } } bool flag = false; for(int i = alen + blen - 1;i >= 0;--i) { if(!flag && ans[i] != 0) flag = true; if(flag) cout < } if(!flag) cout <<0; cout < return 0;}View Code hdu1042 #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 100005;const int MOD = 1e9 + 7;#define MemI(x) memset(x, -1, sizeof(x))#define Mem0(x) memset(x, 0, sizeof(x))#define MemM(x) memset(x, 0x3f, sizeof(x))#define lson l, m, rt <<1#define rson m + 1, r, rt <<1 | 1int main(){ int n, a[MAXN]; while(cin >> n) { Mem0(a); int len = 0, t = 0; a[0] = 1; //万进制 for(int i = 1;i <= n;++i) { for(int j = 0;j <= len;++j) { a[j] = a[j] * i + t; t = a[j] / 10000; a[j] %= 10000; } if(t) { a[++len] = t; t = 0; } } printf("%d", a[len]); for(int i = len - 1;i >= 0;--i) printf("%04d", a[i]); printf("\n"); } return 0;}View Code
大数乘法
51nod 1027
#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 1005;const int MOD = 1e9 + 7;#define MemI(x) memset(x, -1, sizeof(x))#define Mem0(x) memset(x, 0, sizeof(x))#define MemM(x) memset(x, 0x3f, sizeof(x))#define lson l, m, rt <<1#define rson m + 1, r, rt <<1 | 1char a[MAXN], b[MAXN];int ans[MAXN * MAXN] = {0};int main(){ cin >> a >> b; int alen = strlen(a), blen = strlen(b); for(int i = 0;i > 1;++i) swap(a[i], a[alen - i - 1]); for(int i = 0;i > 1;++i) swap(b[i], b[blen - i - 1]); for(int i = 0;i { for(int j = 0;j { ans[i + j] += (a[i] - '0') * (b[j] - '0'); ans[i + j + 1] += ans[i + j] / 10; ans[i + j] %= 10; } } bool flag = false; for(int i = alen + blen - 1;i >= 0;--i) { if(!flag && ans[i] != 0) flag = true; if(flag) cout < } if(!flag) cout <<0; cout < return 0;}View Code hdu1042 #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 100005;const int MOD = 1e9 + 7;#define MemI(x) memset(x, -1, sizeof(x))#define Mem0(x) memset(x, 0, sizeof(x))#define MemM(x) memset(x, 0x3f, sizeof(x))#define lson l, m, rt <<1#define rson m + 1, r, rt <<1 | 1int main(){ int n, a[MAXN]; while(cin >> n) { Mem0(a); int len = 0, t = 0; a[0] = 1; //万进制 for(int i = 1;i <= n;++i) { for(int j = 0;j <= len;++j) { a[j] = a[j] * i + t; t = a[j] / 10000; a[j] %= 10000; } if(t) { a[++len] = t; t = 0; } } printf("%d", a[len]); for(int i = len - 1;i >= 0;--i) printf("%04d", a[i]); printf("\n"); } return 0;}View Code
hdu1042
#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 100005;const int MOD = 1e9 + 7;#define MemI(x) memset(x, -1, sizeof(x))#define Mem0(x) memset(x, 0, sizeof(x))#define MemM(x) memset(x, 0x3f, sizeof(x))#define lson l, m, rt <<1#define rson m + 1, r, rt <<1 | 1int main(){ int n, a[MAXN]; while(cin >> n) { Mem0(a); int len = 0, t = 0; a[0] = 1; //万进制 for(int i = 1;i <= n;++i) { for(int j = 0;j <= len;++j) { a[j] = a[j] * i + t; t = a[j] / 10000; a[j] %= 10000; } if(t) { a[++len] = t; t = 0; } } printf("%d", a[len]); for(int i = len - 1;i >= 0;--i) printf("%04d", a[i]); printf("\n"); } return 0;}View Code