大数加法大数乘法
Posted shuizhidao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数加法大数乘法相关的知识,希望对你有一定的参考价值。
大数加法
hdu1002
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #include <queue> #include <iomanip> #include <stack> 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 | 1 void 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 < alen;++i) aa[i] = a[alen - i - 1] - ‘0‘; for(int i = 0;i < blen;++i) bb[i] = b[blen - i - 1] - ‘0‘; int ans[MAXN]; Mem0(ans); for(int i = 0;i < len;++i) ans[i] = aa[i] + bb[i]; for(int i = 0;i < len;++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 << ans[i]; } if(!flag) cout << 0; cout << endl; } 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 << endl; cout << "Case " << cas << ": " << a << " + " << b << " = "; Add(a, b, strlen(a), strlen(b)); } } return 0; }
大数乘法
51nod 1027
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #include <queue> #include <iomanip> #include <stack> 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 | 1 char 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 < alen >> 1;++i) swap(a[i], a[alen - i - 1]); for(int i = 0;i < blen >> 1;++i) swap(b[i], b[blen - i - 1]); for(int i = 0;i < alen;++i) { for(int j = 0;j < blen;++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 << ans[i]; } if(!flag) cout << 0; cout << endl; return 0; }
hdu1042
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #include <queue> #include <iomanip> #include <stack> 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 | 1 int 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(" "); } return 0; }
以上是关于大数加法大数乘法的主要内容,如果未能解决你的问题,请参考以下文章