思路:用char[]存储输入,后用int[]逐位计算,根据乘法计算规则错位相加,用数组存储,然后考虑进位,最后倒序输出
代码:
#include<iostream>
#include<cstring>
using namespace std;
char a1[10001], b1[10001];
int a[10001],b[10001],c[10001];
int main()
{
cin >> a1 >> b1;
for (int i = 0; i < strlen(a1); i++)
{
a[i] = a1[strlen(a1)-i-1] - '0';
}
for (int i = 0; i < strlen(b1); i++)
{
b[i] = b1[strlen(b1)-i-1] - '0';
}
//****i+j-1呢,事实上,j是指正常的计算进位(一位一位进行乘法计算),而i就是我们说的错位相加
for (int i = 0; i < strlen(a1); i++)
{
for (int j = 0; j < strlen(b1); j++)
{
c[i + j ] += a[i] * b[j];
}
}
for (int i = 0; i < strlen(a1) + strlen(b1) - 1; i++)
{
if (c[i] > 9)//大于9则需要进位
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
int len = strlen(a1) + strlen(b1);
while (c[len-1] == 0 && len > 1)len--;
for (int i = len-1; i >= 0; i--)cout << c[i];
return 0;
}
总结:
乘法要错位相加;
注意倒序输入,倒序输出
这个代码多次复习
标签:10001,day8,int,len,a1,b1,P1303,Problem,strlen From: https://blog.csdn.net/remaker15/article/details/140107939