本地测都过了 为什么提交全WA,看了案例输出也一样啊

P1957 口算练习题

Chen_08 @ 2024-03-26 09:34:51

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int n;
char ch;//存放运算符
int main()
{
    cin>>n;
    cin.ignore();
    while(n--)
    {
        if(n==0)//最后一行不输出空格
        {
          string    str1,str2;//输入字符串 输出字符串
                    stringstream ss2;//输出流
                    getline(cin,str1); //读入字符串
                    stringstream ss1(str1);//输入流
                    int a,b;//两个运算数
                    if(str1[0]=='a')
                    {
                        ss1>>ch>>a>>b;
                        cout<<a<<'+'<<b<<'='<<a+b<<endl;
                        ss2<<a<<'+'<<b<<'='<<a+b;
                        str2=ss2.str();
                        cout<<str2.size();

                    }
                    else if(str1[0]=='b')
                    {
                        ss1>>ch>>a>>b;
                        cout<<a<<'-'<<b<<'='<<a-b<<endl;
                        ss2<<a<<'-'<<b<<'='<<a-b;
                        str2=ss2.str();
                        cout<<str2.size();

                    }
                    else if(str1[0]=='c')
                    {
                        ss1>>ch>>a>>b;
                        cout<<a<<'*'<<b<<'='<<a*b<<endl;
                        ss2<<a<<'*'<<b<<'='<<a*b;
                        str2=ss2.str();
                        cout<<str2.size();

                    }
                    else {
                        if(ch=='a')
                        {
                            ss1>>a>>b;
                                        cout<<a<<'+'<<b<<'='<<a+b<<endl;
                                        ss2<<a<<'+'<<b<<'='<<a+b;
                                        str2=ss2.str();
                                        cout<<str2.size();
                        }
                        else if(ch=='b')
                        {
                            ss1>>a>>b;
                                        cout<<a<<'+'<<b<<'='<<a+b<<endl;
                                        ss2<<a<<'+'<<b<<'='<<a+b;
                                        str2=ss2.str();
                                        cout<<str2.size();
                        }
                        else if(ch=='c')
                        {
                            ss1>>a>>b;
                                        cout<<a<<'+'<<b<<'='<<a+b<<endl;
                                        ss2<<a<<'+'<<b<<'='<<a+b;
                                        str2=ss2.str();
                                        cout<<str2.size();
                        }
                    }
        }
        else{ 
        string str1,str2;//输入字符串 输出字符串

        stringstream ss2;//输出流
        getline(cin,str1); //读入字符串
        stringstream ss1(str1);//输入流
        int a,b;
        if(str1[0]=='a')
        {
            ss1>>ch>>a>>b;
            cout<<a<<'+'<<b<<'='<<a+b<<endl;
            ss2<<a<<'+'<<b<<'='<<a+b;
            str2=ss2.str();
            cout<<str2.size()<<endl;

        }
        else if(str1[0]=='b')
        {
            ss1>>ch>>a>>b;
            cout<<a<<'-'<<b<<'='<<a-b<<endl;
            ss2<<a<<'-'<<b<<'='<<a-b;
            str2=ss2.str();
            cout<<str2.size()<<endl;

        }
        else if(str1[0]=='c')
        {
            ss1>>ch>>a>>b;
            cout<<a<<'*'<<b<<'='<<a*b<<endl;
            ss2<<a<<'*'<<b<<'='<<a*b;
            str2=ss2.str();
            cout<<str2.size()<<endl;

        }
        else {
            if(ch=='a')
            {
                ss1>>a>>b;
                            cout<<a<<'+'<<b<<'='<<a+b<<endl;
                            ss2<<a<<'+'<<b<<'='<<a+b;
                            str2=ss2.str();
                            cout<<str2.size()<<endl;
            }
            else if(ch=='b')
            {
                ss1>>a>>b;
                            cout<<a<<'+'<<b<<'='<<a+b<<endl;
                            ss2<<a<<'+'<<b<<'='<<a+b;
                            str2=ss2.str();
                            cout<<str2.size()<<endl;
            }
            else if(ch=='c')
            {
                ss1>>a>>b;
                            cout<<a<<'+'<<b<<'='<<a+b<<endl;
                            ss2<<a<<'+'<<b<<'='<<a+b;
                            str2=ss2.str();
                            cout<<str2.size()<<endl;
            }
        }       
        }

    }
    return 0;

}

by Chen_08 @ 2024-03-26 13:54:49

不要用getline() 否则丢失输出 敲烂警钟


by Episode33_ @ 2024-03-30 00:19:04

@Chen_08 为什么不能用getline,我也碰见全wa了


by Episode33_ @ 2024-03-30 00:20:04

@Chen_08 不用getline又该用什么来读取一行呢?


by Chen_08 @ 2024-03-30 10:20:12

@Episode33_ 这个测试数据最后应该是EOF结尾 没有回车 所以最后一行读不了 出不了结果


by Chen_08 @ 2024-03-30 10:22:19

@Episode33_

string a;
        cin>>a;
        int n1,n2;
        if(a=="a"||a=="b"||a=="c") 
        {
            ch=a[0];
            cin>>n1>>n2;
        }
        else {
            stringstream s(a);
            s>>n1;
            cin>>n2;
        }

我用的这种


by Episode33_ @ 2024-04-01 11:44:40

@Chen_08 感谢解惑


|