#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
int a[401], b[401], c[802], x = 0, h = 0;
string n1, n2;
cin >> n1;
cin >> n2;
int n = n1.length();
int m = n2.length();
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
for (int i = 1; i <= n1.length(); i++)
a[i] = n1[n - i ] - '0';
for (int i = 1; i <= n2.length(); i++)
b[i] = n2[m - i] - '0';
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
c[i + j - 1] = a[i] * b[j] + c[i + j - 1] + x;
if (c[i + j - 1] >= 10) {
x = c[i + j - 1] / 10;
c[i + j - 1] %= 10;
}
}
if (a[n] * b[m] + x < 10)
h = n + m;
else
h = n + m - 1;
for (int i = h; i >= 1; i--)
cout << c[i];
system("pause");
return 0;
}
高精度乘法,这个算法有什么问题?为什么输出是错的?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- threenewbee 2019-12-01 12:13关注
#include <iostream> #include <string> #include <cstring> #include <cstdlib> using namespace std; int main() { int a[401], b[401], c[802], x = 0, h = 0; string n1, n2; cin >> n1; cin >> n2; int n = n1.length(); int m = n2.length(); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); for (int i = 1; i <= (int)n1.length(); i++) a[i] = n1[n - i] - '0'; //改 for (int i = 1; i <= (int)n2.length(); i++) b[i] = n2[m - i] - '0'; //改 for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { c[i + j - 1] = a[i] * b[j] + c[i + j - 1] + x; if (c[i + j - 1] >= 10) { x = c[i + j - 1] / 10; c[i + j - 1] %= 10; } } if (a[n] * b[m] + x < 10) h = n + m; else h = n + m - 1; //修正多余的前导0的问题 int z; for (z = 800; c[z] == '0' || c[z] == 0; z--); if (z == -1) cout << '0'; for (int i = z; i >= 1; i--) cout << c[i]; system("pause"); return 0; }
解决 无用评论 打赏 举报
悬赏问题
- ¥30 这是哪个作者做的宝宝起名网站
- ¥60 版本过低apk如何修改可以兼容新的安卓系统
- ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
- ¥50 有数据,怎么建立模型求影响全要素生产率的因素
- ¥50 有数据,怎么用matlab求全要素生产率
- ¥15 TI的insta-spin例程
- ¥15 完成下列问题完成下列问题
- ¥15 C#算法问题, 不知道怎么处理这个数据的转换
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!