#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" #include "iomanip" using namespace std; typedef long long ll; const int maxn = 100001; char a[maxn], b[maxn]; ll aa[maxn], bb[maxn], c[maxn]; #define p 9 //压9位高精 #define m 1000000000 //10的p次方 int main() { scanf("%s%s", a, b); int len1 = strlen(a); int len2 = strlen(b); int w, ta = -1, tb = -1; reverse(a, a + len1); //逆序字符串 for (int i = 0; i < len1; i++, w = w * 10) //将字符串转换为高精度数组 { if (i%p == 0) //每p位进一 { w = 1; ta++; } aa[ta] += w * (a[i] - '0'); } reverse(b, b + len2); for (int i = 0; i < len2; i++, w = w * 10) //同上 { if (i%p == 0) { w = 1; tb++; } bb[tb] += w * (b[i] - '0'); } for (int i = 0; i <= ta; i++) //两数相乘 { for (int j = 0; j <= tb; j++) { c[i + j] += aa[i] * bb[j]; c[i + j + 1] += c[i + j] / m; c[i + j] %= m; } } int t = ta + tb + 2; while (!c[t]) t--; //找数组长度 for (int i = t; i >= 0; i--) { if (t == i) cout << c[i]; else { cout << setw(p) << setfill('0') << c[i]; //补0 } } printf("\n"); return 0; }
注意:高精压位最多只能压到 9 位,所以并不是最快的算法
0.0分
0 人评分
【绝对值排序】 (C++代码)浏览:670 |
C语言考试练习题_排列 (C语言代码)浏览:719 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:526 |
母牛的故事 (C语言代码)浏览:1427 |
关于C语言变量位置的问题浏览:272 |
简单的a+b (C语言代码)浏览:414 |
简单的a+b (C语言代码)浏览:478 |
1124题解浏览:591 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:593 |
输入输出格式练习 (C语言代码)浏览:842 |