全WA求佬救救 样例能过 WA的点本地跑也能过

P1957 口算练习题

wenyuzhao @ 2024-11-18 22:47:37

#include <bits/stdc++.h>
using namespace std;
int weishu(int n)
{
    if(n==0)    return 1;
    int s;
    while(n!=0)
    {
        s++;
        n=n/10;
    }
    return s;
}
int main()
{
    int t;
    cin>>t;
    getchar();
    // cout<<t<<endl;
    char lastone;
    while(t--)
    {

        string s1;
        getline(cin,s1);
        //getchar();
        // cout<<s1<<endl<<endl;
        int len=s1.length();
        // for(int i=0;i<len;i++)
        //     cout<<s1[i];
        // cout<<endl<<endl;
        int numa=0,numb=0,ans=0;
        int sum1=0,sum2=0,sum3=0;
        int sum=0;
        int flag1;//记录是否有运算符
        if(s1[0]=='a'||s1[0]=='b'||s1[0]=='c')  //查找是否具有运算符
        {
            flag1=1;
            int j;
            for(j=2;j<len;j++)
            {
                if(s1[j]!=' ')
                {
                    numa=numa*10+(s1[j]-'0');
                    sum1++;
                }
                else
                    break;

            }
            //cout<<"j="<<j<<endl;
            for(int k=j+1;k<=len-1;k++)
                numb=numb*10+(s1[k]-'0');
            sum2=len-j;
        }
        else
        {
            flag1=0;
            int j;
            for(j=0;j<len;j++)
            {
                if(s1[j]!=' ')
                {
                    numa=numa*10+(s1[j]-'0');
                    sum1++;
                }
                else
                    break;
            }
            for(int k=j+1;k<=len-1;k++)
                numb=numb*10+(s1[k]-'0');
            sum2=len-j;
        }
        if(s1[0]=='a'||(lastone=='a'&&flag1==0))
        {
            lastone='a';
            ans=numa+numb;
            cout<<numa<<"+"<<numb<<'='<<ans<<endl;
            sum3=weishu(ans)-1;
        }
        else if(s1[0]=='b'||(lastone=='b'&&flag1==0))
        {
            lastone='b';
            ans=numa-numb;
            cout<<numa<<"-"<<numb<<'='<<ans<<endl;
            sum3=weishu(ans);
        }
        else if(s1[0]=='c'||(lastone=='c'&&flag1==0))
        {
            lastone='c';
            ans=numa*numb;
            cout<<numa<<"*"<<numb<<'='<<ans<<endl;
            sum3=weishu(ans)-1;
        }
        sum=sum1+sum2+2+sum3;
        cout<<sum<<endl;
    }
    return 0;
}

by Flora_liu @ 2024-11-27 21:28:04

#include <cstring>
#include <algorithm>
int n,x,y;
char ss[20],ans[100],k;
int main() {
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {
        scanf("%s",&ss);
        if(isalpha(ss[0])) {
            k=ss[0];
            scanf("%d%d",&x,&y);
        } else {
            x=atoi(ss);
            scanf("%d",&y);
        }
        if(k=='a')
            sprintf(ans,"%d+%d=%d",x,y,x+y);
        else if(k=='b')
            sprintf(ans,"%d-%d=%d",x,y,x-y);
        else
            sprintf(ans,"%d*%d=%d",x,y,x*y);
        printf("%s\n%d\n",ans,strlen(ans));
    }
    return 0;
}

|