全WA,不知道哪出问题

P1957 口算练习题

99wwww @ 2024-03-08 13:04:29

#include<bits/stdc++.h>
using namespace std;
long long n,m,a[100005],b[100005],c[100005],mm,kg,sf,gw,sw,bw,qw,ab;
string s;
char ss[100005];
int main(){
    cin>>n;
    for(int i=1;i<=n+1;i++)
    {
        kg=0,m=0;
        getline(cin,s);
        //cout<<s<<" ";
        for(int j=0;j<s.size();j++)
        {
            if(s[j]>='a' && s[j]<='c'){
                ss[i]=s[j];
            }
            else if(s[j]>='0' && s[j]<='9'){
                m=m*10+(s[j]-'0');
            }
            else{
                kg++;
                if(ss[i]=='a' || ss[i]=='b' || ss[i]=='c'){
                    if(kg==2){
                        a[i]=m;
                        m=0;
                    }
                }
                else{
                    a[i]=m;
                    m=0;
                }
            }
        }
        b[i]=m;
        if(ss[i]=='a' || ss[i]=='b' || ss[i]=='c'){
            c[i]=s.size()-1;
            //cout<<c[i]<<" ";
        }
        else c[i]=s.size()+1;
    }
    for(int i=1;i<=n+1;i++)
    {
        if(i==1) continue;
        if(ss[i]=='a') sf=1;
        else if(ss[i]=='b') sf=2;
        else if(ss[i]=='c') sf=3;
        if(sf==1){
            ab=a[i]+b[i];
            cout<<a[i]<<"+"<<b[i]<<"="<<a[i]+b[i]<<endl;
            if(a[i]+b[i]<0){
                c[i]++;
                ab=abs(ab);
            }
            qw=(ab)/1000;
            bw=(ab)/100;
            sw=(ab)/10;
            gw=(ab)/1;
            if(qw>0) c[i]+=4;
            else if(bw>0) c[i]+=3;
            else if(sw>0) c[i]+=2;
            else if(gw>=0) c[i]+=1;
            cout<<c[i]<<endl;
            //cout<<qw<<" "<<bw<<" "<<sw<<" "<<gw<<endl;
        }
        else if(sf==2){
            ab=a[i]-b[i];
            cout<<a[i]<<"-"<<b[i]<<"="<<a[i]-b[i]<<endl;
            if(a[i]-b[i]<0){
                c[i]++;
                ab=abs(ab);
            }
            qw=(ab)/1000;
            bw=(ab)/100;
            sw=(ab)/10;
            gw=(ab)/1;
            if(qw>0) c[i]+=4;
            else if(bw>0) c[i]+=3;
            else if(sw>0) c[i]+=2;
            else if(gw>0) c[i]+=1;
            cout<<c[i]<<endl;
        }
        else if(sf==3){
            ab=a[i]*b[i];
            cout<<a[i]<<"*"<<b[i]<<"="<<a[i]*b[i]<<endl;
            if(a[i]*b[i]<0){
                c[i]++;
                ab=abs(ab);
            }
            qw=(ab)/1000;
            bw=(ab)/100;
            sw=(ab)/10;
            gw=(ab)/1;
            if(qw>0) c[i]+=4;
            else if(bw>0) c[i]+=3;
            else if(sw>0) c[i]+=2;
            else if(gw>0) c[i]+=1;
            cout<<c[i]<<endl;
        }

    }
    return 0;
}

by james2333 @ 2024-03-14 13:43:58

#include<bits/stdc++.h>
using namespace std;
long long n,x,y,s1,s2,s3,k1,k2,s4,s5,k3,s6,s7;
string s;
int main(){
    cin>>n;
    getline(cin,s);
    for(int i=1;i<=n;i++)
    {
        getline(cin,s);
        if(s[0]=='a')s1=1;
        else if(s[0]=='b')s1=2;
        else if(s[0]=='c')s1=3;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]>='0'&&s[i]<='9')
            {
                s2=s2*10+(s[i]-'0');
            }
            else if(s[i]==' ')
            {
                x=s2;
                s4=x;
                s2=0;
            }
        }
        y=s2;
        s5=y;
        s2=0;
        if(x==0)k1++;
        for(;x>0;x=x/10)k1++;
        if(y==0)k2++;
        for(;y>0;y=y/10)k2++;
        if(s1==1)
        {
            s3=s4+s5;
            s6=s3;
            cout<<s4<<"+"<<s5<<"="<<s4+s5;
            s7+=2;
        }
        if(s1==2)
        {
            s3=s4-s5;
            if(s3<0)
            {
                s3=abs(s3);
                s7+=1;
            }
            s6=s3;
            cout<<s4<<"-"<<s5<<"="<<s4-s5;
            s7+=2;
        }
        if(s1==3)
        {
            s3=s4*s5;
            s6=s3;
            cout<<s4<<"*"<<s5<<"="<<s4*s5;
            s7+=2;
        }
        if(s3==0)k3++;
        for(;s6>0;s6=s6/10)k3++;
        cout<<endl<<s7+k1+k2+k3;
        cout<<endl;
        k1=k2=k3=s2=s3=s4=s5=s6=s7=0;
    }
    return 0;
}

可以试试这样写


by mlemtion @ 2024-03-14 13:46:29

@99wwww 行数更少

#include <bits/stdc++.h>
using namespace std;
int n,a,b,c,o,l;
string s;
int main(){
    cin>>n;getline(cin,s);
    while(n--){
        getline(cin,s);
        stringstream ss<<s;
        if('a'<=s[0]&&s[0]<='z'){
            switch(s[0]){
                case 'a':o=1;break;
                case 'b':o=2;break;
                case 'c':o=3;break;
            }char t;ss>>t;
        }
        ss>>a;ss>>b;
        if(o==1){
            c=a+b;string p;
            p=to_string(a)+'+'+to_string(b)+'='+to_string(c);
            cout<<p<<'\n'<<p.size()<<'\n';
        }else if(o==2){
            c=a-b;string p;
            p=to_string(a)+'-'+to_string(b)+'='+to_string(c);
            cout<<p<<'\n'<<p.size()<<'\n';
        }else if(o==3){
            c=a*b;string p;
            p=to_string(a)+'*'+to_string(b)+'='+to_string(c);
            cout<<p<<'\n'<<p.size()<<'\n';
        }
    }
    return 0;
}

by 99wwww @ 2024-03-15 13:01:16

@mlemtion @james2333 屑屑


by cpp_xhq @ 2024-03-16 09:25:08

不要用


getline(cin,s);

可以用先输入,如果不是a~c
再转成int

by 99wwww @ 2024-03-18 12:52:23

@cpp_xhq 豪德


|