下载的样例对但是全WA,是什么问题?

P1957 口算练习题

csq_pig @ 2023-09-02 18:19:54

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
char s[60][30];
int n,f,a,b,c,k,ok,ans;
int main()
{
    scanf("%d",&n); s[0][0]=getchar();
    for (int i=1;i<=n;i++)
        gets(s[i]);
    for (int i=1;i<=n;i++) {
        for (int j=0;j<strlen(s[i]);j++) {
            if (s[i][j]=='a') {
                f=1;continue;
            }
            if (s[i][j]=='b') {
                f=2;continue;}
            if (s[i][j]=='c') {
                f=3;continue;}
            if (s[i][j]==' ' and a!=0) {
                ans+=k;k=0;ok=1;continue;}
            if (s[i][j]==' ') {
                k=0;continue;}
            k++;
            if (ok==0) {
                a*=10; a+=(s[i][j]-'0');
            }
            if (ok==1) {
                b*=10; b+=(s[i][j]-'0');
            }
        }
        ans+=k;
        if (f==1) {
            cout<<a<<"+"<<b<<"="<<a+b<<endl;
            c=a+b;
        }
        if (f==2) {
            cout<<a<<"-"<<b<<"="<<a-b<<endl;
            c=a-b;
            if (c<0) {
                ans++;
                c=-c;
            }
        }
        if (f==3) {
            cout<<a<<"*"<<b<<"="<<a*b<<endl;
            c=a*b; 
        }

        while (c>0) {
            c/=10;
            ans++;
        }
        cout<<ans+2<<endl;
//      cout<<ans<<" "<<a<<" "<<b<<endl; 
        ok=0;
        ans=0;
        k=0;
        a=0;
        b=0;
        c=0;
    }
    return 0;
}

为什么我的答案都是对的,我还下载了样例,也是完全对的,但是测评结果是全WA呢?求大佬救命!!!


by williamwei @ 2023-09-02 18:37:03

不要用gets。最好别轻易用getcharlinux系统下换行符不一样。


by csq_pig @ 2023-09-02 19:49:56

@williamwei 哦哦哦,妙啊,但是不用gets就要彻底改造了> <


|