题目链接:51nod 1027大数乘法
直接模板了。
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int N = 1001; 5 const int DLEN = 4; 6 const int mod = 10000; 7 int alen, blen; 8 int ans_len; 9 char a1[N], b1[N]; 10 int a[600], b[600]; 11 int ans[600]; 12 13 void BigInt(const char s[], int (&c)[600], int &len){ 14 memset(c, 0, sizeof(c)); 15 int L = strlen(s); 16 len = L / DLEN; 17 if(L%DLEN) len++; 18 int cnt = 0; 19 for(int i = L-1; i >= 0 ; i -= DLEN){ 20 int k = i - DLEN + 1; 21 if(k < 0) k = 0; 22 int t = 0; 23 for(int j = k; j <= i; ++j) 24 t = t * 10 + s[j] - '0'; 25 c[cnt++] = t; 26 } 27 } 28 void multi(){ 29 int i, j; 30 for(i = 0; i < alen; ++i){ 31 int up = 0; 32 for(j = 0; j < blen; ++j){ 33 int t = a[i] * b[j] + ans[i+j] + up; 34 up = t / mod; 35 ans[i+j] = t % mod; 36 } 37 if(up != 0) 38 ans[i+j] = up; 39 } 40 ans_len = alen + blen; 41 while(ans[ans_len-1] == 0 && ans_len > 1) 42 ans_len--; 43 44 printf("%d", ans[ans_len-1]); 45 for(i = ans_len-2; i >= 0; i--) 46 printf("%04d", ans[i]); 47 puts(""); 48 } 49 int main(){ 50 scanf("%s%s", a1, b1); 51 BigInt(a1, a, alen); 52 BigInt(b1, b, blen); 53 multi(); 54 return 0; 55 }