首先将参与计算的数组反转,然后计算每一位的乘积,最后再进位。
1 #include<stdio.h> 2 #include<memory.h> 3 #define N 8 4 #define M 5 5 void carry_bit(int *x); 6 void reverse(int *x,int t); 7 8 int main() 9 { 10 int a[N] = { 4,3,2,1,9,4,6,8 }; 11 int b[M] = { 8,9,6,5,6}; 12 int c[M+N]; 13 memset(c,0,sizeof(int)*(M+N)); 14 reverse(a, sizeof(a)/sizeof(int)); 15 reverse(b, sizeof(b)/sizeof(int)); 16 17 for (int i = 0; i < N; i++) 18 for (int j = 0; j < M; j++) 19 { 20 c[i + j] += a[i] * b[j]; 21 } 22 23 carry_bit(c); 24 for (int i = M + N - 1; i >= 0; i--) 25 printf("%d", c[i]); 26 printf("\n"); 27 } 28 void carry_bit(int *x) 29 { 30 while (*x++ > 10) 31 { 32 int temp = *(x - 1); 33 *(x - 1) = temp % 10; 34 *x += temp / 10; 35 } 36 } 37 void reverse(int *x,int t) 38 { 39 for (int i = 0; i < t/2; i++) 40 { 41 int temp; 42 temp = *(x+i); 43 *(x+i) = *(x +(t-i-1)); 44 *(x +(t-i-1)) = temp; 45 } 46 }