给出2个大整数A,B,计算A*B的结果。
Input
第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0)
Output
输出A*B
Input示例
123456
234567
Output示例
28958703552
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int judge(char* a) 6 { 7 int i; 8 for (i = 0; i < strlen(a); ++i) { 9 if (a[i] != ‘0‘) 10 return 0; 11 } 12 return 1; 13 } 14 15 void multiplicative(char* a, char* b) 16 { 17 int lena = strlen(a), lenb = strlen(b); 18 int len = lena + lenb; 19 int i, j; 20 int a1[1005]={0}, b1[10005]={0}, r[10005]={0}; 21 22 for (i = 0; i < lena; ++i) 23 a1[i] = a[lena-i-1] - ‘0‘; 24 for (i = 0; i < lenb; ++i) 25 b1[i] = b[lenb-i-1] - ‘0‘; 26 for (i = 0; i < lena; ++i) { 27 for (j = 0; j < lenb; ++j) { 28 r[i+j] += a1[i] * b1[j]; 29 } 30 } 31 for (i = 0; i < len; ++i) { 32 r[i+1] += r[i] / 10; 33 r[i] = r[i] % 10; 34 } 35 while (!r[len-1]) 36 len--; 37 for (i = 0; i < len; ++i) 38 a[i] = r[len-i-1] + ‘0‘; 39 if (judge(a)) 40 strcpy(a, "0"); 41 printf("%s", a); 42 } 43 44 int main () 45 { 46 char A[10005], B[10005]; 47 int fa, fb; 48 scanf("%s%s", A, B); 49 multiplicative(A, B); 50 return 0; 51 }
时间: 2024-12-29 06:42:51