80分救助大佬

P1957 口算练习题

qew12312 @ 2023-09-12 20:55:01


#include<bits/stdc++.h>
using namespace std;
struct we{
    char a;
    long s,d,sum;
};
long zhuan(char as[100]){//将字符数组转换为long类型
    int t=1,i,s,qw;
long sum;
    s=strlen(as);
    for(i=s-1;i>=0;i--){
    qw=(as[i]-'0')*t;
        sum+=qw;
        t*=10;
    }
    return sum;
}
int shu(int a){//求数字的长度
    int sum=0;
    for(;a!=0;){
        a/=10;sum++;
    }
    return sum;
}

int main(){
    int i,w;
    cin>>w;
    we sd[w];
    char q[100];
    for(i=0;i<w;i++){
        cin>>q;
        if(q[0]>='1'&&q[0]<='9')
        {
            sd[i].a=sd[i-1].a;
            cin>>sd[i].d;
            sd[i].s=zhuan(q);
        }
        else {
        sd[i].a=q[0];cin>>sd[i].s>>sd[i].d;     
    }}
        for(i=0;i<w;i++){
            if(sd[i].a=='a'){
                sd[i].sum=sd[i].s+sd[i].d;
                cout<<sd[i].s<<"+"<<sd[i].d<<"="<<sd[i].sum<<endl;
            }
            if(sd[i].a=='b'){
                sd[i].sum=sd[i].s-sd[i].d;
                cout<<sd[i].s<<"-"<<sd[i].d<<"="<<sd[i].sum<<endl;
            }
            if(sd[i].a=='c') {
                sd[i].sum=sd[i].s*sd[i].d;
                cout<<sd[i].s<<"*"<<sd[i].d<<"="<<sd[i].sum<<endl;
            }

            if(sd[i].sum<0)
            cout<<shu(sd[i].s)+shu(sd[i].d)+shu(fabs(sd[i].sum))+3<<endl;
            else cout<<shu(sd[i].s)+shu(sd[i].d)+shu(fabs(sd[i].sum))+2<<endl;}

    return 0;
}

by One_JuRuo @ 2023-09-12 21:04:22

@qew12312

0的数字长度错了


by easy_for @ 2024-02-21 22:05:24

include<bits/stdc++.h>

define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

int init(string x){ int t=0; for(int i=0;i<x.size();i++){ t=t*10+x[i]-'0'; } return t; } int main(){ IOS; string A,B,C,D; int T; cin>>T; while(T--){

    int res=2,a,b,c,d;
    cin>>A;
    if(A>="a"&&A<="c"){
        D=A;
        cin>>B>>C;
        res=res+B.size()+C.size();
        b=init(B);
        c=init(C);
        if(A=="a")d=b+c,cout<<b<<'+'<<c<<'='<<d;
        else if(A=="b")d=b-c,cout<<b<<'-'<<c<<'='<<d;
        else d=c*b,cout<<b<<'*'<<c<<'='<<d;
        cout<<endl;
        if(d<=0)res++;
        while(d){
            d=d/10;
            res++;
        }
    }
    else{
        cin>>B;
        res=res+A.size()+B.size();
        a=init(A);
        b=init(B);
        if(D=="a")d=b+a,cout<<a<<'+'<<b<<'='<<d;
        else if(D=="b")d=a-b,cout<<a<<'-'<<b<<'='<<d;
        else d=a*b,cout<<a<<'*'<<b<<'='<<d;
        cout<<endl;
        if(d<=0)res++;
        while(d){
            d=d/10;
            res++;
        }
    }
    cout<<res<<endl;
}
return 0;

}```cpp

include<bits/stdc++.h>

define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

int init(string x){ int t=0; for(int i=0;i<x.size();i++){ t=t*10+x[i]-'0'; } return t; } int main(){ IOS; string A,B,C,D; int T; cin>>T; while(T--){

    int res=2,a,b,c,d;
    cin>>A;
    if(A>="a"&&A<="c"){
        D=A;
        cin>>B>>C;
        res=res+B.size()+C.size();
        b=init(B);
        c=init(C);
        if(A=="a")d=b+c,cout<<b<<'+'<<c<<'='<<d;
        else if(A=="b")d=b-c,cout<<b<<'-'<<c<<'='<<d;
        else d=c*b,cout<<b<<'*'<<c<<'='<<d;
        cout<<endl;
        if(d<=0)res++;
        while(d){
            d=d/10;
            res++;
        }
    }
    else{
        cin>>B;
        res=res+A.size()+B.size();
        a=init(A);
        b=init(B);
        if(D=="a")d=b+a,cout<<a<<'+'<<b<<'='<<d;
        else if(D=="b")d=a-b,cout<<a<<'-'<<b<<'='<<d;
        else d=a*b,cout<<a<<'*'<<b<<'='<<d;
        cout<<endl;
        if(d<=0)res++;
        while(d){
            d=d/10;
            res++;
        }
    }
    cout<<res<<endl;
}
return 0;

}


|