1542a @ 2024-07-23 16:38:24
#include<iostream>
using namespace std;
int la,lb,lc,a[1010],b[1010],c[1010];
string s1,s2;
int main() {
cin>>s1>>s2;
la = s1.length();
lb = s2.length();
for (int i=0; i<la; i++) a[i]=s1[la-i-1]-48;
for (int i=0; i<lb; i++) b[i]=s2[lb-i-1]-48;
for (int i=0; i<la; i++)
for (int j=0; j<lb; j++) {
c[i+j] += a[i] * b[j] ;
c[i+j+1] += c[i+j] / 10;
c[i+j] = c[i+j] % 10;
}
lc = la + lb;
while (lc>=1 && c[lc]==0) lc--;
for (int i=lc; i>=0; i--) cout<<c[i];
cout<<endl;
return 0;
}
by haimingbei @ 2024-07-23 16:45:06
@1542a 参考阔已一下(AC,求关注) 思路大致,可能是哪出问题了?
/*
3、P1303 A*B Problem
*/
#include<bits/stdc++.h>
using namespace std;
char s1[2005],s2[2005];
int a1[2005],a2[2005],a3[4010];
int main(){
cin>>s1>>s2;
int lena=strlen(s1),lenb=strlen(s2),len=lena+lenb;
for(int i=1;i<=lena;i++)a1[i]=s1[lena-i]-'0';
for(int i=1;i<=lenb;i++)a2[i]=s2[lenb-i]-'0';
if(a1[1]==0||a2[1]==0){
cout<<0;
return 0;
}
for(int i=1;i<=lena;i++){
for(int j=1;j<=lenb;j++){
a3[i+j-1]+=a1[i]*a2[j];
}
}
for(int i=1;i<=len;i++){
a3[i+1]+=a3[i]/10;
a3[i]%=10;
}
while(a3[len]==0)len--;
for(int i=len;i>=1;i--)cout<<a3[i];
return 0;
}
by 1542a @ 2024-07-23 17:49:03
@haimingbei 感谢,数组开小了