60分求助!

P1957 口算练习题

hjsxhst2022 @ 2022-10-05 18:03:00

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    char a;
    for(int i=1;i<=n;i++){
        cin>>a;
        int s1,s2,s3,s4=1;
        cin>>s1>>s2;
        int s1ws,s2ws,dsws,sc,sz;
        sz=int(a)-'0';
        s3=s1;
        for(s1ws=1;s3!=0;s1ws++)
            s3/=10;
        s3=s2;
        for(s2ws=1;s3!=0;s2ws++)
            s3/=10;
        if(a=='a'){
            sc=1;
            s3=s1+s2;
            for(dsws=1;s3!=0;dsws++)
                s3/=10;
            s3=s1;
            for(;s3!=0;dsws++)
                s3/=10;
            s3=s2;
            for(;s3!=0;dsws++)
                s3/=10;
            if(s1<0)
                dsws++;
            if(s2<0)
                dsws++;
            dsws++;
            if(s1+s2<0)
                dsws++;
            cout<<s1<<'+'<<s2<<'='<<s1+s2<<endl;
        }else if(a=='b'){
            sc=2;
            s3=s1-s2;
            for(dsws=1;s3!=0;dsws++)
                s3/=10;
            s3=s1;
            for(;s3!=0;dsws++)
                s3/=10;
            s3=s2;
            for(;s3!=0;dsws++)
                s3/=10;
            if(s1<0)
                dsws++;
            if(s2<0)
                dsws++;
            dsws++;
            if(s1-s2<0)
                dsws++;
            cout<<s1<<'-'<<s2<<'='<<s1-s2<<endl;
        }else if(a=='c'){
            sc=3;
            s3=s1*s2;
            for(dsws=1;s3!=0;dsws++)
                s3/=10;
            s3=s1;
            for(;s3!=0;dsws++)
                s3/=10;
            s3=s2;
            for(;s3!=0;dsws++)
                s3/=10;
            dsws++;
            if(s1<0)
                dsws++;
            if(s2<0)
                dsws++;
            if(s1*s2<0)
                dsws++;
            cout<<s1<<'*'<<s2<<'='<<s1*s2<<endl;
        }else if(sc==1){
            s3=sz;
            for(dsws=1;s3!=0;dsws++)
                s3/=10;
            s3=s1;
            for(;s3!=0;dsws++){
                s3/=10;
                s4*=10;
            }
            if(s2!=0)
                s3=(sz*s4+s1)+s2;
            else
                s3=sz+s1;
            for(;s3!=0;dsws++)
                s3/=10;
            dsws++;
            if(sz<0)
                dsws++;
            if(s1<0)
                dsws++;
            if(sz+s1<0)
                dsws++;
            if(s2!=0){
            s3=s2;
            for(;s3!=0;dsws++)
                s3/=10;
                cout<<sz<<s1<<'+'<<s2<<'='<<(sz*s4+s1)+s2<<endl;
            }else
            cout<<sz<<'+'<<s1<<'='<<sz+s1<<endl;
        }else if(sc==2){
            s3=sz;
            for(dsws=1;s3!=0;dsws++)
                s3/=10;
            s3=s1;
            for(;s3!=0;dsws++){
                s3/=10;
                s4*=10;
            }
            if(s2!=0)
                s3=(sz*s4+s1)-s2;
            else
                s3=sz-s1;
            for(;s3!=0;dsws++)
                s3/=10;
            if(sz<0)
                dsws++;
            if(s1<0)
                dsws++;
            if(sz-s1<0)
                dsws++;
            if(s2!=0){
                s3=s2;
            for(;s3!=0;dsws++)
                s3/=10;
                cout<<sz<<s1<<'-'<<s2<<'='<<(sz*s4+s1)-s2<<endl;
            }else
            cout<<sz<<'-'<<s1<<'='<<sz-s1<<endl;
        }
        else{
            s3=sz;
            for(dsws=1;s3!=0;dsws++)
                s3/=10;
            s3=s1;
            for(;s3!=0;dsws++){
                s3/=10;
                s4*=10;
            }
            if(s2!=0)
                s3=(sz*s4+s1)*s2;
            else
                s3=sz*s1;
            for(;s3!=0;dsws++)
                s3/=10;
            dsws++;
            if(sz*s1<0)
                dsws++;
            if(sz<0)
                dsws++;
            if(s1<0)
                dsws++;
            if(s2!=0){
                s3=s2;
                for(;s3!=0;dsws++)
                    s3/=10;
                cout<<sz<<s1<<'*'<<s2<<'='<<(sz*s4+s1)*s2<<endl;
            }else
            cout<<sz<<'*'<<s1<<'='<<sz*s1<<endl;
        }
        cout<<dsws<<endl;
    }
    return 0;
}

做了2个小时,才60分,大佬求调


|