求助

P1957 口算练习题

jiangzhencong @ 2024-11-30 10:04:04

#include <bits/stdc++.h>
#include <cstring>
using namespace std;
int len(int n){
    int cnt=0;
    while(n!=0){
        cnt++;
        n/=10;
    }
    return cnt;
}
int stoi(string s){
    int sum=0;
    int quan=1;
    for(int i=s.size()-1;i>=0;i--){
        sum+=(int(s[i])-48)*quan;
        quan *= 10;
    }return sum;
}
int main(){
    int n;
    cin>>n;
    string c;
    int a,b;
    char lc;
    for(int i=0;i<n;i++){
        cin>>c;
        if(!(c == "a"||c == "b"||c == "c")){
            a = stoi(c);
            cin>>b;
            int f=0;
            if(a-b<0)f = 1;
            switch(lc){
                case 'a':
                    cout<<a<<"+"<<b<<"="<<a+b<<endl<<len(a)+len(b)+len(a+b)+2<<endl;
                    break;
                case 'b':

                    cout<<a<<"-"<<b<<"="<<a-b<<endl<<len(a)+len(b)+len(a-b)+2+f<<endl;
                    break;
                case 'c':
                    cout<<a<<"*"<<b<<"="<<a*b<<endl<<len(a)+len(b)+len(a*b)+2<<endl;
                    break;
            }
        }else {
            lc = c[0];
            cin>>a>>b;
        }
        int f=0;
        if(a-b<0)f = 1;
        switch(c[0]){
            case 'a':
                cout<<a<<"+"<<b<<"="<<a+b<<endl<<len(a)+len(b)+len(a+b)+2<<endl;
                break;
            case 'b':
                cout<<a<<"-"<<b<<"="<<a-b<<endl<<len(a)+len(b)+len(a-b)+2+f<<endl;
                break;
            case 'c':
                cout<<a<<"*"<<b<<"="<<a*b<<endl<<len(a)+len(b)+len(a*b)+2<<endl;
                break;
        }
    }
    return 0;
}

80分,第四第五点错误


by pjh0625 @ 2024-11-30 10:10:09

#include<bits/stdc++.h>
using namespace std;
int n,x,y;
char ss[20],ans[100],k;
int main() {
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {
        scanf("%s",&ss);
        if(isalpha(ss[0])) {
            k=ss[0];
            scanf("%d%d",&x,&y);
        } else {
            x=atoi(ss);
            scanf("%d",&y);
        }
        if(k=='a')
            sprintf(ans,"%d+%d=%d",x,y,x+y);
        else if(k=='b')
            sprintf(ans,"%d-%d=%d",x,y,x-y);
        else
            sprintf(ans,"%d*%d=%d",x,y,x*y);
        printf("%s\n%d\n",ans,strlen(ans));
    }
    return 0;
}

@jiangzhencong


|