Bull Math
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit
Status
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).
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
The larger simulation process is the multiplication of two large numbers. So we must first simulate the multiplication of large numbers and integers, because it is the multiplication of large numbers and integers, so we must simulate the addition of large numbers first.
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 1e2+5;
string str1,str2;
string sum(string s1,string s2) //大数加法
{
if(s1.length()<s2.length())
{
string temp=s1;
s1=s2;
s2=temp;
}
int i,j;
for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
{
s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));
if(s1[i]-'0'>=10)
{
s1[i]=char((s1[i]-'0')%10+'0');
if(i) s1[i-1]++;
else s1='1'+s1;
}
}
return s1;
}
string Mult(string s,int x) //大数乘以整形数
{
reverse(s.begin(),s.end());
int cmp=0;
for(int i=0;i<s.size();i++)
{
cmp=(s[i]-'0')*x+cmp;
s[i]=(cmp%10+'0');
cmp/=10;
}
while(cmp)
{
s+=(cmp%10+'0');
cmp/=10;
}
reverse(s.begin(),s.end());
return s;
}
string Multfa(string x,string y) //大数乘法
{
string ans;
for(int i=y.size()-1,j=0;i>=0;i--,j++)
{
string tmp=Mult(x,y[i]-'0');
for(int k=0;k<j;k++)
tmp+='0';
ans=sum(ans,tmp);
}
return ans;
}
int main(){
cin>>str1;
cin>>str2;
string gao = Multfa(str1,str2);
cout<<gao<<endl;
return 0;
}