1307: [Example 1.3] High precision multiplication
Time limit: 1000 ms Memory limit: 65536 KB
Commits: 20666 Passes: 7103
【Title description】
Enter two high-precision positive integers M and N (M and N are both less than 100 digits). Find the product of these two high-precision numbers.
【Enter】
Enter two high-precision positive integers M and N.
【Output】
Find the product of these two high-precision numbers.
【Input example】
36
3
[Sample output]
108
【source】
Explanation: The key is to find the law, find the relationship between the storage position of the new array and the position of the two multipliers
Note: carry
#include<bits/stdc++.h>
using namespace std;
int a[205],b[205],c[500],la,lb,lc=1;
string sa,sb;
void pa(){
for(int i=1;i<=la;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
void pb(){
for(int i=1;i<=lb;i++){
cout<<b[i]<<" ";
}
cout<<endl;
}
void pc(){
for(int i=1;i<=lc;i++){
cout<<c[i]<<" ";
}
cout<<endl;
}
int main(){
freopen("cpp.in","r",stdin);
freopen("cpp.out","w",stdout);
cin>>sa;
cin>>sb;
la = sa.size();
lb = sb.size();
// 倒序从1开始排
for(int i=la-1;i>=0;i--){
a[la-i] = sa[i]-'0';
}
// pa();
for(int i=lb-1;i>=0;i--){
b[lb-i] = sb[i]-'0';
}
// pb();
// 找规律,发现i*j位,是放到新数组的 i*j-1
// 注意:放完后,要进位,用 / 和 % 符合,将下一位 加
for(int i=1;i<=la;i++){
for(int j=1;j<=lb;j++){
lc = i+j-1;
c[lc] += a[i]*b[j];
c[lc+1] += c[lc]/10;
c[lc] = c[lc]%10;
}
}
//设置c的位数(应该是la+lb,不过好似感觉要多一位比较靠谱,反正都有消位)
// 如果没有设置,lc延续之前的 lc=i+j-1; 比真正结果少一位
lc = la+lb+1;
// pc();
while(c[lc]==0){
lc--;
}
for(int i=lc;i>=1;i--){
cout<<c[i];
}
return 0;
}