42 lines
1.1 KiB
C
42 lines
1.1 KiB
C
|
|
#include <stdio.h>
|
||
|
|
#include <math.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
// 递归计算大整数乘积
|
||
|
|
double bigdataride(long x, long y, int n) {
|
||
|
|
x = llabs(x); y = llabs(y);
|
||
|
|
if (n == 1) {
|
||
|
|
return (double)x * y; // 基本情况
|
||
|
|
} else {
|
||
|
|
if (n % 2 == 1) n--; // 处理奇数位
|
||
|
|
long halfPow = (long)pow(10, n / 2);
|
||
|
|
long a = x / halfPow;
|
||
|
|
long b = x % halfPow;
|
||
|
|
long c = y / halfPow;
|
||
|
|
long d = y % halfPow;
|
||
|
|
|
||
|
|
// 递归计算
|
||
|
|
double ac = bigdataride(a, c, n / 2);
|
||
|
|
double bd = bigdataride(b, d, n / 2);
|
||
|
|
long aJb = a + b;
|
||
|
|
long cJd = c + d;
|
||
|
|
double abcd = bigdataride(aJb, cJd, n / 2);
|
||
|
|
// 返回最终结果
|
||
|
|
return ac * pow(10, n) + (abcd - ac - bd) * pow(10, n / 2) + bd;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
long x = 8782346786879887887L;
|
||
|
|
long y = 4524387689998798768L;
|
||
|
|
// 计算数字长度
|
||
|
|
char sx[20];
|
||
|
|
snprintf(sx, sizeof(sx), "%ld", x);
|
||
|
|
int n = strlen(sx);
|
||
|
|
// 计算并输出结果
|
||
|
|
double s = bigdataride(x, y, n);
|
||
|
|
printf("大数相乘的计算结果为:%lf\n", s);
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|