面试题---两个大整数相乘
#include <stdio.h> #include <string.h> #include <stdlib.h> void reverse(char *value) { int len = strlen(value); char *p = value; char *q = value + len - 1; while(p < q) { *p ^= *q; *q ^= *p; *p ^= *q; p++; q--; } } void mult(char *num1,char *num2) { int len1 = strlen(num1); int len2 = strlen(num2); int len3 = len1 + len2; char *result = (char *)malloc(len3 + 1); memset(result,'0',len3 + 1); int i = 0; for(i = 0;i < len3 - 1;i++) { int m = 0; int res = 0; for(m = 0;m <= i && m < len1;m++) { int n = 0; for(n = 0;n <= i && n < len2;n++) { if(m + n == i) { int value1 = num1[m] - '0'; int value2 = num2[n] - '0'; res = res + value1 * value2; } } } res = res + result[i] - '0'; result[i] = res % 10 + '0'; result[i + 1] = res / 10 + '0'; } reverse(result); printf("结果为:%s\n",result); } int main() { printf("输入第一个数:\n"); char num1[20]; scanf("%s",num1); printf("输入第二个数:\n"); char num2[20]; scanf("%s",num2); reverse(num1); reverse(num2); printf("逆序后为:%s,%s\n",num1,num2); mult(num1,num2); return 0; }