P1957求助!都是WA

P1957 口算练习题

Hongyq48 @ 2022-07-21 22:57:12

本地测的时候都是对的,一上OJ就无qwq

代码如下:

#include <bits/stdc++.h>
using namespace std;

string a;
char ope;
int s[2],cnts=1,ans,s1l,s2l,al;

int main(){
    int o;
    scanf("%d\n",&o);

    for(int i=1;i<=o;i++){
        getline(cin,a);
        if(a[0]=='a')   ope='+';
        else if(a[0]=='b')  ope='-';
        else if(a[0]=='c')  ope='*';

        for(int k=10;k>=0;k--){
            if(a[k]!='\000' && a[k]!=' '){
                int temp=k;
                for(int e=k;a[e]!=' ' && e>=0;e--){
                    s[cnts]+=pow(10,temp-e)*(a[e]-'0');
                    k=e;
                }
                if(cnts==-1)    break;
                else    cnts--;
            }
        }
        cnts=1;
        if(ope=='+'){
            ans=s[0]+s[1];
            if(s[0]==0) s1l=1;
            else    s1l=log10(s[0])+1;
            if(s[1]==0) s2l=1;
            else    s2l=log10(s[1])+1;
            if(ans==0)  al=1;
            else    al=log10(ans)+1;
            cout<<s[0]<<'+'<<s[1]<<'='<<ans<<endl<<s1l+s2l+al+1+1;
            if(i!=o)    cout<<endl;
        }
        else if(ope=='-'){
            ans=s[0]-s[1];
            if(s[0]==0) s1l=1;
            else    s1l=log10(s[0])+1;
            if(s[1]==0) s2l=1;
            else    s2l=log10(s[1])+1;
            if(ans==0)  al=1;
            else if(ans<0)  al=log10(abs(ans))+2;
            else    al=log10(ans)+1;
            cout<<s[0]<<'-'<<s[1]<<'='<<ans<<endl<<s1l+s2l+al+1+1;
            if(i!=o)    cout<<endl;
        }
        else if(ope=='*'){
            ans=s[0]*s[1];
            if(s[0]==0) s1l=1;
            else    s1l=log10(s[0])+1;
            if(s[1]==0) s2l=1;
            else    s2l=log10(s[1])+1;
            if(ans==0)  al=1;
            else    al=log10(ans)+1;
            cout<<s[0]<<'*'<<s[1]<<'='<<ans<<endl<<s1l+s2l+al+1+1;
            if(i!=o)    cout<<endl;
        }

        memset(s,0,sizeof(s));
        a="";
    }

    return 0;
}

by nikodo @ 2022-07-23 21:19:41

@Hongyq48 有可能因为洛谷是linux操作系统,不支持getline

if大军照样能过:

#include<bits/stdc++.h>
using namespace std;
int n[50],m[50],f,ans;
struct tsuanshi{
    char c;
    int aa;
    int bb;
    int len1;
    int len2;
};
void output(tsuanshi y,bool o){
    int rr1;
    switch(y.c){
        case 'a':cout<<y.aa<<"+"<<y.bb<<"="<<y.aa+y.bb<<endl;rr1=y.aa+y.bb;break;
        case 'b':cout<<y.aa<<"-"<<y.bb<<"="<<y.aa-y.bb<<endl;rr1=y.aa-y.bb;break;
        case 'c':cout<<y.aa<<"*"<<y.bb<<"="<<y.aa*y.bb<<endl;rr1=y.aa*y.bb;break;
    }
    int ans=0,ans1=0,ans2=0;
    if(y.aa<0)
    ans++;
    if(y.bb<0)
    ans1++;
    if(rr1<0)
    ans2++;
    if(y.aa==0)
    ans=1;
    if(y.bb==0)
    ans1=1;
    if(rr1==0)
    ans2=1;
    if(!rr1==0)
        while(rr1!=0){
            ans2++;
            rr1/=10;
        }
    if(!y.bb==0)
        while(y.bb!=0){
            ans1++;
            y.bb/=10;
        }
    if(!y.aa==0)
        while(y.aa!=0){
            ans++;
            y.aa/=10;
        }
    if(o==false)
    cout<<ans+1+ans1+1+ans2<<endl;
    else
    cout<<ans+1+ans1+1+ans2;    
}
int main(){
    cin>>f;
    tsuanshi s[50];
    string a,b;
    for(int i=0;i<f;i++){
        cin>>a;
        if(a.length()==1){
            s[i].c=a[0];
            cin>>a>>b;
            s[i].aa=atoi(a.c_str());
            s[i].bb=atoi(b.c_str());
            s[i].len1=a.length();
            s[i].len2=b.length();
            continue;
        }else{
            s[i].c=s[i-1].c;
            cin>>b;
            s[i].aa=atoi(a.c_str());
            s[i].bb=atoi(b.c_str());
            s[i].len1=a.length();
            s[i].len2=b.length();
        }
    }
    for(int i=0;i<f-1;i++)
    output(s[i],false);
    output(s[f-1],true);
    return 0;
}

by Hongyq48 @ 2022-07-27 17:50:48

@yiheng0913 牛哇牛哇


|