样例和下载数据都对上了,可是为什么WA0分?

P1957 口算练习题

worldcat @ 2023-12-09 13:55:00

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,check,findit,a,b,c,anslen;
string s,op;
int main()
{
    cin>>n;
    getline (cin,op);
    for(int nihao=1;nihao<=n;nihao++)
    {
        getline(cin,s);
        if (!(s[0]>='0'&&s[0]<='9'))
        {
            check=s[0]-'a'+1;
            findit=2;
            a=b=0;
            while(s[findit]!=' ')
                a=a*10+(s[findit++]-'0'); 
            findit++;
            while(findit<s.size())
                b=b*10+(s[findit++]-'0');

            cout<<a;
            if (check==1)
            {
                c=a+b;
                cout<<"+";
            }
            else if (check==2)
            {
                c=a-b;
                cout<<"-";
                if (c<0)
                {
                    findit++;
                }
            }
            else if (check==3)
            {
                c=a*b;
                cout<<"*";
            }
            anslen=0;
            cout<<b<<"="<<c<<endl;
            while(c!=0)
            {
                anslen++;
                c/=10;
            }
            cout<<findit-2+1+anslen<<(nihao!=n?"\n":"");
        }
        else
        {
            findit=0;
            a=b=0;
            while(s[findit]!=' ')
                a=a*10+(s[findit++]-'0'); 
            findit++;
            while(findit<s.size())
                b=b*10+(s[findit++]-'0');

            cout<<a;
            if (check==1)
            {
                c=a+b;
                cout<<"+";
            }
            else if (check==2)
            {
                c=a-b;
                cout<<"-";
                if (c<0)
                {
                    findit++;
                }
            }
            else if (check==3)
            {
                c=a*b;
                cout<<"*";
            }
            anslen=0;
            cout<<b<<"="<<c<<endl;
            //cout<<findit<<endl;
            //cout<<anslen<<endl;
            while(c!=0)
            {
                anslen++;
                c/=10;
            }
            cout<<findit+1+anslen<<(nihao!=n?"\n":"");
        }
    }
    return 0;
}

by clx201022 @ 2023-12-09 14:57:44

别用 getline


by N_ty @ 2023-12-10 06:59:48

@worldcat 因为你没过GESP3,第一题让你老眼昏花


by worldcat @ 2023-12-10 08:02:11

@yejingzhi 6


by worldcat @ 2023-12-10 08:04:38

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,check,a,b,c,alen,blen,anslen,conpetation;
string s,op,an;
int main()
{
    cin>>n;
    getline(cin,op);
    for(int nihao=1;nihao<=n;nihao++)
    {

        conpetation=a=b=c=0;
        cin>>an;
        if (an[0]=='a')
        {
            check=1;
        }
        else if (an[0]=='b')
        {
            check=2;
        }
        else if (an[0]=='c')
        {
            check=3;
        }
        else
        {
            for(int i=0;i<an.size();i++)
            {
                a=a*10+(an[i]-'0');
            }
            //cout<<123<<endl;
            cin>>b;
            cout<<a;
            if (check==1)
            {
                c=a+b;
                cout<<"+";
            }
            else if (check==2)
            {
                c=a-b;
                cout<<"-";
                if (c<0)
                {
                    conpetation++;
                }
            }
            else if (check==3)
            {
                c=a*b;
                cout<<"*";
            }
            cout<<b<<"="<<c<<endl;
            anslen=0;
            blen=0;
            alen=0;
            while(c!=0)
            {
                anslen++;
                c/=10;
            }
            while(a!=0)
            {
                alen++;
                a/=10;
            }
            while(b!=0)
            {
                blen++;
                b/=10;
            }
            cout<<alen+blen+anslen+conpetation+2<<(nihao!=n?"\n":"");
            continue;
        }
        //cout<<123<<endl;
        cin>>a>>b;
        //cout<<check<<endl;
        cout<<a;
        if (check==1)
        {
            c=a+b;
            cout<<"+";
        }
        else if (check==2)
        {
            c=a-b;
            cout<<"-";
            if (c<0)
            {
                conpetation++;
            }
        }
        else if (check==3)
        {
            c=a*b;
            cout<<"*";
        }
        cout<<b<<"="<<c<<endl;
        anslen=0;
        blen=0;
        alen=0;
        if (!c) anslen=1;
        while(c!=0)
        {
            anslen++;
            c/=10;
        }
        if (!a) alen=1;
        while(a!=0)
        {
            alen++;
            a/=10;
        }
        if (!b) blen=1;
        while(b!=0)
        {
            blen++;
            b/=10;
        }
        cout<<alen+blen+anslen+conpetation+2<<(nihao!=n?"\n":"");
    }   
    return 0;
}

by worldcat @ 2023-12-10 08:05:13

@clx201022 感谢,现在换思路了


|