样例对的,但全WA

P1957 口算练习题

zhaoshuang @ 2021-08-08 16:20:42


#include<bits/stdc++.h>

using namespace std;
int fen(int a)
{int ans=0;
    while(a!=0)
    {
        a=a/10;
        ans++;
    }
    return ans;
}
int main()
{
    int n;int u,v;
    cin>>n;
    char a[n][20];int b[n][2],ans=0;
    for(int i=0;i<=n;i++)
    for(int j=0;j<2;j++)
    b[i][j]=0;
    for(int i=0;i<=n;i++){

    gets(a[i]);
    }

    for(int i=0;i<=n;i++)
    {v=0;ans=0;
    if(a[i][0]=='a')
        {

         u=1;
            for(int j=2;j<strlen(a[i]);j++)
            {ans++;
                if(a[i][j]==' ')
                {
                    v=j+1;
                    break;
                }
                b[i][0]=b[i][0]*10;
                b[i][0]=b[i][0]+(a[i][j]-'0');
            }
            for(int j=v;j<strlen(a[i]);j++)
            {
                ans++;
                b[i][1]=b[i][1]*10;
                b[i][1]=b[i][1]+(a[i][j]-'0');
            }
            cout<<b[i][0]<<"+"<<b[i][1]<<"="<<b[i][0]+b[i][1]<<endl;
            ans=ans+2+fen(b[i][0]+b[i][1]);
            cout<<ans-1<<endl;
            continue;}

        if(a[i][0]=='b')
        {

         u=2;
            for(int j=2;j<strlen(a[i]);j++)
            {ans++;
                if(a[i][j]==' ')
                {
                    v=j+1;
                    break;
                }
                b[i][0]=b[i][0]*10;
                b[i][0]=b[i][0]+(a[i][j]-'0');
            }

            for(int j=v;j<strlen(a[i]);j++)
            {
                ans++;
                b[i][1]=b[i][1]*10;
                b[i][1]=b[i][1]+(a[i][j]-'0');
            }
            cout<<b[i][0]<<"-"<<b[i][1]<<"="<<b[i][0]-b[i][1]<<endl;
            ans=ans+2+fen(b[i][0]-b[i][1]);
            if(b[i][0]-b[i][1]<0)
            ans++;
            cout<<ans-1<<endl;continue;}
            if(a[i][0]=='c')
        {

         u=3;
            for(int j=2;j<strlen(a[i]);j++)
            {ans++;
                if(a[i][j]==' ')
                {
                    v=j+1;
                    break;
                }
                b[i][0]=b[i][0]*10;
                b[i][0]=b[i][0]+(a[i][j]-'0');
            }
            for(int j=v;j<strlen(a[i]);j++)
            {
                ans++;
                b[i][1]=b[i][1]*10;
                b[i][1]=b[i][1]+(a[i][j]-'0');
            }
            cout<<b[i][0]<<"*"<<b[i][1]<<"="<<b[i][0]*b[i][1]<<endl;ans=ans+2+fen(b[i][0]*b[i][1]);
            cout<<ans-1<<endl;continue;}

            if(a[i][0]!='a'&&a[i][0]!='b'&&a[i][0]!='c')
            {
                if(u==1)
                {
                    for(int j=0;j<strlen(a[i]);j++)
                    {ans++;
                        if(a[i][j]==' ')
                {
                    v=j+1;
                    break;
                }
                b[i][0]=b[i][0]*10;
                b[i][0]=b[i][0]+(a[i][j]-'0');
                    }

                    for(int j=v;j<strlen(a[i]);j++)
            {ans++;

                b[i][1]=b[i][1]*10;
                b[i][1]=b[i][1]+(a[i][j]-'0');
            }
            cout<<b[i][0]<<"+"<<b[i][1]<<"="<<b[i][0]+b[i][1]<<endl;ans=ans+2+fen(b[i][0]+b[i][1]);
            cout<<ans-1<<endl;
                }

                if(u==2)
                {
                    for(int j=0;j<strlen(a[i]);j++)
                    {ans++;
                        if(a[i][j]==' ')
                {
                    v=j+1;
                    break;
                }
                b[i][0]=b[i][0]*10;
                b[i][0]=b[i][0]+(a[i][j]-'0');
                    }

                    for(int j=v;j<strlen(a[i]);j++)
            {
                ans++;
                b[i][1]=b[i][1]*10;
                b[i][1]=b[i][1]+(a[i][j]-'0');
            }
            cout<<b[i][0]<<"-"<<b[i][1]<<"="<<b[i][0]-b[i][1]<<endl;ans=ans+2+fen(b[i][0]-b[i][1]);
            if(b[i][0]-b[i][1]<0)
            ans++;
            cout<<ans-1<<endl;
                }
                if(u==3)
                {
                    for(int j=0;j<strlen(a[i]);j++)
                    {ans++;
                        if(a[i][j]==' ')
                {
                    v=j+1;
                    break;
                }
                b[i][0]=b[i][0]*10;
                b[i][0]=b[i][0]+(a[i][j]-'0');
                    }

                    for(int j=v;j<strlen(a[i]);j++)
            {
                ans++;
                b[i][1]=b[i][1]*10;
                b[i][1]=b[i][1]+(a[i][j]-'0');
            }
            cout<<b[i][0]<<"*"<<b[i][1]<<"="<<b[i][0]*b[i][1]<<endl;ans=ans+2+fen(b[i][0]*b[i][1]);
            cout<<ans-1<<endl;
                }
                continue;
            }
    }
    return 0;
}   

by c20210623 @ 2021-08-08 17:17:52

没有特判0


by zhaoshuang @ 2021-08-08 17:39:44

@c20210623 哪个0


by zhaoshuang @ 2021-08-09 14:07:09

@c20210623 特判过后还是WA


by Graugear @ 2021-08-10 18:07:31

程序如果是一个一个字符处理的话可以考虑看看是不是换行符的问题,用洛谷的在线IDE,把样例输入复制后末尾加个回车看看输出结果对不对。


|