求助,样例过不去(关注)

P1303 A*B Problem

CSP400pts @ 2024-09-04 19:51:32

WA code:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<deque>
#include<string.h>
#include<cstring>
#include<string>
#include<map>
#include<set>
using namespace std;
const int N=1e5+5;
string a,b;
int c[N],d[N];
int ans[N];
void print() {
    int len=a.size()+b.size();
    while(a[len]==0&&len>1) len--;
    for(int i=len; i>=1; i--) cout<<ans[i];
}
int main() {
    cin>>a>>b;
    int la=a.size();
    int lb=b.size();
    for(int i=0; i<la; i++) {
        c[la-i]=a[i]-'0';
    }
    for(int i=0; i<lb; i++) {
        d[lb-i]=b[i]-'0';
    }
    int x,last=0;
    for(int i=1; i<=la+1; i++) {
        last=0;
        for(int j=1; j<=lb+1; j++) {
            x=a[i]*b[j];
            x+=last;
            last=x/10;
            ans[i+j-1]+=x%10;
            if(ans[i+j-1]>=10) {
                ans[i+j]++;
                ans[i+j-1]%=10;
            }
        }
    }
    print();
    return 0;
}

by ggginoxwx @ 2024-09-04 21:39:51

//while(a[len]==0&&len>1) len--;
while(ans[len]==0&&len>1) len--;

不知道对不对(


by CSP400pts @ 2024-09-04 21:41:40

@ggginoxwx 不对


by mengfancheng @ 2024-09-07 16:33:03

#include<bits/stdc++.h>
using namespace std;
int ds[1000];
int main() {
    string x,y;
    cin>>x>>y;
    long long a=x.length(),b=y.length(),zxz,zdz;
    if((a==1||b==1)&&(x[0]=='0'||y[0]=='0')){
        cout<<"0";
        return 0;
    }
    zxz=a;
    zdz=b;
    if(b<a){
        zxz=b;
        zdz=a;
    }
    int xx[zdz]={0},yy[zdz]={0},zh[a+b]={0};
    for(int i=a-1;i>=0;i--){
        xx[i]=int(x[a-1-i])-48;
    }
    for(int i=b-1;i>=0;i--){
        yy[i]=int(y[b-1-i])-48;
    }
    for(int i=0;i<a;i++){
        for(int j=0;j<b;j++){
            zh[i+j]+=xx[i]*yy[j];
        }
    }
    for(int i=0;i<a+b-1;i++){
        zh[i+1]+=zh[i]/10;
        zh[i]=zh[i]%10;
    }
    if(zh[a+b-1]!=0){
        cout<<zh[a+b-1];
    }
    for(int i=a+b-2;i>=0;i--){
        cout<<zh[i];
    }
}

|