没想到用这种模拟竖式的算法,膜大佬
#include<iostream>
#include<string.h>
using namespace std;
char str[10];
int n, dot;
int res[999999], a[999999], b[999999];
int len, lena, lenb;
void mul()
{
int i, j;
memset(res, 0, sizeof(res));
for (i=1; i<=lena; i++)
{
for (j=1; j<=lenb; j++)
{
res[i+j-1] += a[i] * b[j];
if (res[i+j-1]>9)
{
res[i+j] += res[i+j-1] / 10;
res[i+j-1] %= 10;
}
}
}
lena = lena + lenb;
for (i=1; i<=lena; i++) a[i] = res[i];
}
int main()
{
int i, j, up, down;
while (cin>>str>>n&&n!=0&&str!=0)
{
dot = -1;
for (i=5, j=1; i>=0; i--)
{
if (str[i]!='.') a[j] = b[j++] = str[i] - '0';
else dot = i;
}
if (dot==-1) lena = lenb = 6;
else lena = lenb = 5;
for (i=1; i<n; i++) mul();
if (dot==-1)
{
for (i=lena; i>=1; i--)
{
cout<<a[i];
}
cout<<endl;
}
else
{
dot = 5 - dot;
dot *= n;
for (i=1; i<=lena; i++)
{
if (a[i]!=0)
{
down = i;
break;
}
}
for (j=lena; j>=1; j--)
{
if (a[j]!=0)
{
up = j;
break;
}
}
i = up;
if (up<dot) i = dot;
j = down;
if (j>dot) j = dot + 1;
for (; i>=j; i--)
{
if (i==dot)
{
cout<<".";
}
cout<<a[i];
}
cout<<endl;
}
}
return 0;
}
本文介绍了一种使用模拟竖式算法实现的大数乘法程序。通过逐位相乘并进位的方式,该算法能够处理任意大小的整数乘法运算。文章提供了完整的C++代码实现,展示了如何通过数组来存储大数,并进行乘法运算。
2246

被折叠的 条评论
为什么被折叠?



