#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语言程序设计教程(第三版)课后习题6.11 (C语言代码)for循环浏览:1178 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:641 |
C语言程序设计教程(第三版)课后习题1.6 (C++代码)浏览:909 |
简单的a+b (C语言代码)浏览:600 |
C语言训练-求1+2!+3!+...+N!的和 (C语言代码)万恶的long long浏览:906 |
C语言程序设计教程(第三版)课后习题6.8 (C++代码)浏览:614 |
数对 (C语言代码)浏览:762 |
愚蠢的摄影师 (C++代码)浏览:980 |
蛇行矩阵 (C语言代码)浏览:559 |
判定字符位置 (C语言代码)浏览:849 |