代码单独测试通过但提交全部WA求助

P1957 口算练习题

zyabc @ 2022-06-01 22:53:52

代码单独测试通过,下载测试点测试结果也是对的,为什么提交全部WA


#include<bits/stdc++.h>
using namespace std;
string input[51];
string output[51];
int main()
{
    int n;
    cin>>n;
    getchar();

    int i,j;
    int operator1[n][3]={0},result[n],temp;
    int index=0;

    for(i=0;i<n;i++)
    {
        getline(cin,input[i]);
        result[i]=0;
        if(input[i][0]>='a'&&input[i][0]<='c') 
        {operator1[i][0]=input[i][0]-'a'+1;input[i][0]=' ';}
        else operator1[i][0]=operator1[i-1][0];
        index=0;
        for(j=0;j<input[i].size();j++)
        {       
            if(input[i][j]>='0'&&input[i][j]<='9') 
            {
                if(j==0||((j>0)&&(input[i][j-1]==' '))) 
                {   index++;
                    if(index==2) 
                    {
                        if(operator1[i][0]==1) output[i]+='+';
                        if(operator1[i][0]==2) output[i]+='-';
                        if(operator1[i][0]==3) output[i]+='*';
                    }
                }
                operator1[i][index]=operator1[i][index]*10+input[i][j]-'0'; 
                output[i]=output[i]+input[i][j];
            }
            else if(input[i][j]==' ') 
            {

                continue;   
            }
        }
        output[i]=output[i]+'=';
        if(operator1[i][0]==1) result[i]=operator1[i][1]+operator1[i][2];
        if(operator1[i][0]==2) result[i]=operator1[i][1]-operator1[i][2];
        if(operator1[i][0]==3) result[i]=operator1[i][1]*operator1[i][2];
        if(result[i]==0) {output[i]=output[i]+'0';continue;}
        if(result[i]<0) {output[i]=output[i]+'-';}
        temp=abs(result[i]);
        string abc="";
        while(temp>0)
        {
            abc=(char)(temp%10+'0')+abc;
            temp=temp/10;                       
        }
        output[i]=output[i]+abc;

    }
    for(i=0;i<n;i++)
    {
        cout<<output[i]<<endl;
        cout<<output[i].size()<<endl;
    }

    return 0;
}

by caomuyilin @ 2022-06-23 19:37:22

我和你同样的问题,不知为啥

#include <bits/stdc++.h>
using namespace std;
string s;
char c;
int cal(char a, int b, int c)
{
    if (a == '+') return b + c;
    if (a == '-') return b - c;
    if (a == '*') return b * c;
    return b + c;
}
int getlen(int a)
{
    int len = 0, flag = 0;
    if (a < 0)
    {
        flag = 1;
        a = -a;
    }
    while (a)
    {
        a /= 10;
        len++;
    }
    return len + flag;
}
void process(string s)
{
    int a = 0, b = 0, i = 0;
    int len = s.length(), ans=0;
    if (s[0] >= 'a' && s[0] <= 'z')
    {
        c = s[0];
        if (c == 'a') c = '+';
        else if (c == 'b') c = '-';
        else c = '*';
        i = 2;
    }
    while (s[i] != ' ' && i < len) { a = a * 10 + s[i] - '0'; i++;}
    i++;
    while (i < len) { b = b * 10 + s[i] - '0'; i++;}
    cout << a << c << b << "=" << cal(c, a, b) << endl;
    ans = getlen(cal(c, a, b))+getlen(a)+getlen(b)+2;
    cout << ans << endl;
}
int main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(NULL);
    // freopen("1.in", "r", stdin);
    // freopen("test.out", "w", stdout);
    int n;
    cin >> n;
    getline(cin, s);
    for (int i = 1; i <= n; i++)
    {
        getline(cin, s);
        process(s);
    }
    // clear
    s.clear();
    return 0;
}

by pingyyds @ 2022-07-03 09:57:49

我也一样


by pingyyds @ 2022-07-03 09:58:41

不会是题目的测试数据不对吧


by LzxQwQ @ 2022-07-06 16:50:25

虽然我是一个蒟蒻,但我还是好心地提醒一下洛谷洛谷的测试数据末尾会有/n,所以你用getline会多读入一个/n,而你本地的Windows却不会


|