hzc0829 @ 2024-06-26 20:56:57
代码(WA on #4,#5):
#include<bits/stdc++.h>
using namespace std;
string BigPlus(string a,string b){
while(a[0]=='0'&&a.size()>1){
a.erase(0,1);
}
while(b[0]=='0'&&a.size()>1){
b.erase(0,1);
}
int maxlen=max(a.size(),b.size())-1;
if(a.size()-1==maxlen){
while(b.size()-1<maxlen){
b='0'+b;
}
}else{
while(a.size()-1<maxlen){
a='0'+a;
}
}
bool f=0;
string c="";
for(int i=maxlen;i>=0;i--){
int temp=a[i]+b[i]-96;
if(f){
temp++;
}
if(temp>=10){
f=1;
temp-=10;
}else{
f=0;
}
char num=(char)(temp+48);
c=num+c;
if(f==1&&i==0){
c="1"+c;
}
}
while(c[0]=='0'&&a.size()>1){
c.erase(0,1);
}
return c;
}
string a,b,c="0";
int main(){
cin>>a>>b;
if(a=="0"||b=="0"){
cout<<0;
return 0;
}
while(a[0]=='0'&&a.size()>1){
a.erase(0,1);
}
while(b[0]=='0'&&a.size()>1){
b.erase(0,1);
}
int cnt=-1;
for(int i=b.size()-1;i>=0;i--){
cnt++;
string temp="";
int nowb=b[i]-48;
int carry=0;
for(int i=a.size()-1;i>=0;i--){
int nowa=a[i]-48;
int nowans=nowb*nowa;
if(carry){
nowans+=carry;
}
if(nowans>10){
carry=nowans/10;
nowans%=10;
}else{
carry=0;
}
char num=(char)(nowans+48);
temp=num+temp;
if(i==0&&carry){
temp=(char)(carry+48)+temp;
}
}
int tempcnt=cnt;
while(tempcnt--){
temp=temp+"0";
}
c=BigPlus(c,temp);
}
while(c[0]=='0'&&a.size()>1){
c.erase(0,1);
}
cout<<c;
return 0;
}
by ___define___ @ 2024-06-26 21:16:58
不如python
by 123huchenghao @ 2024-06-30 10:53:12
#include<stdio.h>
#include<string.h>
#define maxn 23333
struct BigNum{
int s[maxn],len;
BigNum(){len=1;memset(s,0,sizeof(s));}
};
char ai[maxn],bi[maxn];
BigNum a,b,c;
void ride(BigNum*,BigNum*,BigNum*);
int main(){
scanf(" %s %s",ai,bi);
a.len=strlen(ai);
b.len=strlen(bi);
for(int i=a.len-1;i>=0;i--) a.s[a.len-i]=ai[i]-48;
for(int i=b.len-1;i>=0;i--) b.s[b.len-i]=bi[i]-48;
ride(&a,&b,&c);
for(int i=c.len;i>0;i--) printf("%d",c.s[i]);
return 0;
}
void ride(BigNum *x,BigNum *y,BigNum *z){
z->len=x->len+y->len;
for(int i=1;i<=x->len;i++)
for(int j=1;j<=y->len;j++){
z->s[i+j-1]+=x->s[i]*y->s[j];
z->s[i+j]+=z->s[i+j-1]/10;
z->s[i+j-1]%=10;
}
while(z->len>1&&!z->s[z->len]) z->len--;
}