c++最后两个点WA???

P1303 A*B Problem

ZhouJiangyue @ 2022-07-27 15:07:53

#include<bits/stdc++.h>
using namespace std;

int main(){
    string a,b;
    cin>>a>>b;
    if(a=="0"||b=="0"){
        cout<<0;
        return 0;
    }
    int len=a.size()+b.size()+1,c[len]={0};
    if(a.size()<b.size())swap(a,b);

    for(int i=1;i<=b.size();i++){
        for(int j=1;j<=a.size();j++){
            c[i+j]+=(b[b.size()-i]-'0')*(a[a.size()-j]-'0');

        }
    }

    for(int i=2;i<len;i++){
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    if(c[len]==0)len--;
    for(int i=len;i>=2;i--)cout<<c[i];
    return 0;
}

第四个点答案和我输出的结果一样,为什么是错的???


by ERICYIN @ 2022-07-27 15:12:53

哪题?


by ZhouJiangyue @ 2022-07-27 15:16:10

@ERICYIN P1303


by ERICYIN @ 2022-07-27 15:21:52

让我康康


by ERICYIN @ 2022-07-27 15:24:32

你在本地测试过吗?


by ZhouJiangyue @ 2022-07-27 15:24:43

@ERICYIN 栓Q


by ZhouJiangyue @ 2022-07-27 15:25:09

@ERICYIN 测过


by ERICYIN @ 2022-07-27 15:42:40

用高精: AC代码

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int a[10000], b[10000], c[20000];
int main()
{ 
 string n, m; 
 cin >> n >> m;

 int len_n, len_m, max_len, i, j, w, s, k;

 len_n = n.size();
 len_m = m.size();
 for (i=0; i<=len_n - 1; i++)
 a[i] = n[len_n-i-1]-48;

 for (i=0; i<=len_m - 1; i++)
 b[i] = m[len_m-i-1]-48;

 for (i=0; i<=len_n-1; i++) //a:"321"
 for (j=0; j<=len_m-1; j++) //b:"54"
 {
 w = i+j;
 s = a[i]*b[j];
c[w] += s%10;
 c[w+1]+= c[w]/10+s/10;
 c[w]=c[w]%10;
 } 

 k = len_n+len_m;
 while (c[k]==0 && k>0) k--;

 for (i=k; i>=0; i--) cout << c[i];
 return 0;
}

by 御坂10029号 @ 2022-07-27 15:43:11

@ZhouJiangyue 数组开小了


by 御坂10029号 @ 2022-07-27 15:43:33

@ZhouJiangyue 本地能过,上了洛谷ide不行


by 御坂10029号 @ 2022-07-27 15:44:00

@ZhouJiangyue 最后有进位,所以数组要开大一点


| 下一页