就是两个比较大的数相乘,用数组就可以解决。
不过要注意一下数组存的时候数对应的前后顺序
用<algorithm>里的reverse就行了,,
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 105
int lena,lenb;
char a[N],b[N];
int na[N],nb[N],ans[N];
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
cin>>a>>b;
lena=strlen(a);
lenb=strlen(b);
for (i=0;i<lena;i++)
na[i]=a[i]-'0';
for (i=0;i<lenb;i++)
nb[i]=b[i]-'0';
reverse(na,na+lena);
reverse(nb,nb+lenb);
memset(ans,0,sizeof(ans));
for (i=0;i<lena;i++)
{
for (j=0;j<lenb;j++)
{
ans[i+j]+=nb[j]*na[i];
}
for (j=0;j<N-1;j++)
{
ans[j+1]+=ans[j]/10;
ans[j]%=10;
}
}
for (i=N-1;i>=0;i--)
if (ans[i])
break;
for (;i>=0;i--)
cout<<ans[i];
cout<<endl;
}
Language:
Bull Math
Description
Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros).
FJ asks that you do this yourself; don't use a special library function for the multiplication. Input
* Lines 1..2: Each line contains a single decimal number.
Output
* Line 1: The exact product of the two input lines
Sample Input 11111111111111 1111111111 Sample Output 12345679011110987654321 Source |