高精度乘高精度
Description
读入两个高精度数(位数小于255),计算它们的积并输出结果
Input
两个高精度数
Output
这两个数的乘积
Sample Input
9834572345234234
5892348534
Sample Output
57948727940957880596512956
解题思路
用两个字符数组输入,将它们转到两个数组里,然后按各个位数相乘并处理进位就能得出结果。
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
using namespace std;
const int maxn=3010;
char str1[1010],str2[1010];
int a[1010],b[1010],c[maxn];
void input()
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%s%s",str1,str2);//输入两个字符数组
int t=1;
for(int i=strlen(str1)-1;i>=0;i--)
{
a[t]=str1[i]-'0';//转换
t++;
}
t=1;
for(int i=strlen(str2)-1;i>=0;i--)
{
b[t]=str2[i]-'0';//转换
t++;
}
}
void add()
{
int g=0;
for(int i=1;i<=strlen(str1);i++)
for(int j=1;j<=strlen(str2);j++)
{
c[i+j-1]+=a[i]*b[j];//计算
c[i+j]+=c[i+j-1]/10;//处理进位
c[i+j-1]%=10;
}
}
void output()
{
int i=maxn;
while(c[i]==0&&i>1)i--;
for(int j=i;j>=1;j--)
{
printf("%d",c[j]);//输出
}
}
int main()
{
input();
add();
output();
return 0;
}