用C/C++語言實現大數(Big Number)相乘
用C/C++語言實現大數(Big Number)相乘
資料來源: https://mp.weixin.qq.com/s/fOF4H2h-idfMiQ2emiN_FQ
線上編譯器:
https://www.tutorialspoint.com/compile_cpp_online.php
https://www.tutorialspoint.com/compile_c_online.php
Code:
#include "stdio.h" #include "string.h" #define char_to_int(a) (int) (a - '0') #define int_to_char(a) (char)(a + '0') int main() { char a[1000]={"1844674407"}; char b[1000]={"3709551615"}; unsigned long long c[1000]={0}; int i = 0, j = 0, k = 0; memset(c,0,sizeof(c)); /*完成图片中的1,2步,结果从数组开头开始存放*/ for (i = 0; i<strlen(a); i++) { k = i; for (j = 0; j<strlen(b); j++){ c[k] += char_to_int(a[i]) * char_to_int(b[j]); k++; } } /*完成图片中的3步,从后面往前面*/ for (i = k-1; i>0; i--) { if (c[i] >= 10) { c[i-1] = c[i]/10 + c[i-1]; c[i] = c[i]%10; } } /*输出*/ for(i=0;i<k;i++) printf("%llu",c[i]);putchar('\n'); return 0; }
$gcc -o main *.c $main 6842914925636017305