80分 help!!!!

P1303 A*B Problem

Jason_yinkai @ 2021-07-30 14:59:58

#include<iostream>
#include<cstring>
using namespace std;
char s1[2005],s2[2005];
int a[2005],b[2005],c[4010];
int main(){
    cin>>s1>>s2;
    int la = strlen(s1);
    int lb = strlen(s2);
    for(int i = 0;i < la;i++) a[la - i] = s1[i] - '0';
    for(int i = 0;i < lb;i++) b[lb - i] = s2[i] - '0';
    int lc = la + lb;
    for(int i = 1;i <= la;i++){
        for(int j = 1;j <= lb;j++){
            c[i + j - 1] += a[i] * b[j];
            c[i + j] += c[i + j - 1] / 10;
            c[i + j - 1] = c[i + j - 1] % 10;
        }
    }
    if(c[lc] == 0 and lc > 0) lc--;
    for(int i = lc;i > 0;i--){
        cout<<c[i];
    }
}

by bbbzzx @ 2021-07-30 15:13:34

#include<bits/stdc++.h>
#define maxn 5010
using namespace std;
int a[maxn],b[maxn],c[maxn];
int main(){
    string A,B;
    cin>>A>>B;
    int lena=A.length(),lenb=B.length();
    for(int i=lena-1;i>=0;i--) a[lena-i]=A[i]-'0';
    for(int i=lenb-1;i>=0;i--) b[lenb-i]=B[i]-'0';
    for(int i=1;i<=lena;i++) for(int j=1;j<=lenb;j++) c[i+j-1]+=a[i]*b[j];
    int len=lena+lenb;
    for(int i=1;i<=len;i++){c[i+1]+=c[i]/10; c[i]%=10;}
    for(;!c[len];) len--;
    for(int i=max(1,len);i>=1;i--) cout<<c[i];  
}

by shAdomOvO @ 2021-07-30 15:24:52

if(c[lc] == 0 and lc > 0) lc--;

if 改成 while lc>0 改成 lc>1

while(!c[lc]&&lc>1)lc--;

by Jason_yinkai @ 2021-07-30 15:35:10

@Immortalbelie 谢谢大佬


by static_cast @ 2021-08-02 22:42:58

cin>>a1>>b1;
int lena=strlen(a1);
int lenb=strlen(b1);
for(i=1;i<=lena;i++)a[i]=a1[lena-i]-'0';
for(i=1;i<=lenb;i++)b[i]=b1[lenb-i]-'0';
for(i=1;i<=lenb;i++)
for(j=1;j<=lena;j++)
c[i+j-1]+=a[j]*b[i];
for(i=1;i<lena+lenb;i++)
if(c[i]>9)
{
    c[i+1]+=c[i]/10;
    c[i]%=10;
}
len=lena+lenb;
while(c[len]==0&&len>1)len--;
for(i=len;i>=1;i--)cout<<c[i];
return 0;

这样的~~


|