输入输出正确,但是全都显示RE,求助大佬们!

P1957 口算练习题

lybhbslsd123 @ 2023-05-31 15:22:54

cpp

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int num;
    cin>>num;
    cin.get();
    string str[600];
    for(int i=0;i<num;i++)
    {
        getline(cin,str[i]);
    }
//    for(int i=0;i<num;i++)
//    {
//        cout<<str[i]<<endl;
//    }
char k;
for(int i=0;i<num;i++)
{
    string str1,str2,str3,str4;
    if(str[i][0]>='a'&&str[i][0]<='c')
    {
        k=str[i][0];
        str1=str[i].substr(2,str[i].size()-2);
        int space;
        space=str1.find(' ',0);
        str2=str1.substr(0,space);
        str3=str1.substr(space+1,str1.size()-space-1);
        int s1,s2,s3;
        s1=stoi(str2,0,10);
        s2=stoi(str3,0,10);
        if(k=='a')
        {
            s3=s1+s2;
            cout<<str2<<"+"<<str3<<"=";
        }
        else if(k=='b'){
            s3=s1-s2;
            cout<<str2<<"-"<<str3<<"=";
        }
        else if(k=='c'){
        s3=s1*s2;    
        cout<<str2<<"*"<<str3<<"=";
        }
        str4=to_string(s3);
        cout<<str4<<endl;
        cout<<str2.size()+str3.size()+str4.size()+2<<endl;
    }
    else
    {
        string str1,str2,str3,str4;
        str1=str[i];
        int space;
        space=str1.find(' ',0);
        str2=str1.substr(0,space);
        str3=str1.substr(space+1,str1.size()-space-1);
        int s1,s2,s3;
        s1=stoi(str2,0,10);
        s2=stoi(str3,0,10);
        if(k=='a')
        {
            s3=s1+s2;
            cout<<str2<<"+"<<str3<<"=";
        }
        else if(k=='b'){
            s3=s1-s2;
            cout<<str2<<"-"<<str3<<"=";
        }
        else if(k=='c'){
        s3=s1*s2;    
        cout<<str2<<"*"<<str3<<"=";
        }
        str4=to_string(s3);
        cout<<str4<<endl;
        cout<<str2.size()+str3.size()+str4.size()+2<<endl;
}
}
    return 0;
}

by hexuchen @ 2023-06-14 17:29:29

你的代码好像有问题,我的Dev-C++编译失败了。建议在修修BUG (附上我的代码)

#include <bits/stdc++.h>
using namespace std;
int sum(int a){
    int s=0;
    if(a==0)
        return 1;
    while(a!=0){
        s++;
        a/=10;
    }
    return s;
}
int main(){
    long long n,a,b,base=1;
    string s;
    char c;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s;
        if(isalpha(s[0])){
            c=s[0];
            cin>>a>>b;
            switch(c){
                case 'a':printf("%d+%d=%d\n%d\n",a,b,a+b,sum(a)+sum(b)+sum(a+b)+2);break;
                case 'b':if(a>=b){
                    printf("%d-%d=%d\n%d\n",a,b,a-b,sum(a)+sum(b)+sum(a-b)+2);
                }
                else{
                    printf("%d-%d=%d\n%d\n",a,b,a-b,sum(a)+sum(b)+sum(a-b)+3);
                }break;
                case 'c':printf("%d*%d=%d\n%d\n",a,b,a*b,sum(a)+sum(b)+sum(a*b)+2);break;
            } 
        } 
        else{
            base=1;
            a=0;
            for(int i=s.size()-1;i>=0;i--){
                a+=(s[i]-'0')*base;
                base*=10;
            }
            cin>>b;
            switch(c){
                case 'a':printf("%d+%d=%d\n%d\n",a,b,a+b,sum(a)+sum(b)+sum(a+b)+2);break;
                case 'b':if(a>=b){
                    printf("%d-%d=%d\n%d\n",a,b,a-b,sum(a)+sum(b)+sum(a-b)+2);
                }
                else{
                    printf("%d-%d=%d\n%d\n",a,b,a-b,sum(a)+sum(b)+sum(a-b)+3);
                }break;
                case 'c':printf("%d*%d=%d\n%d\n",a,b,a*b,sum(a)+sum(b)+sum(a*b)+2);break;
            } 
        }
    }
    return 0;
}

|