哪位神犇帮我看一下这坨代码

P1957 口算练习题

handsome6667633 @ 2022-07-15 08:01:22

本地能过,

交了RE;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
string a[105];
int p[105][10];
int s_i(string ah);
int wei(int u);
int main(){
    int n;
    string x,y;
    cin>>n;
    getchar();
    for(int i=1;i<=n;i++){
        getline(cin,a[i]);
    }

    char c;
    bool f;
    for(int i=1;i<=n;i++){
        x="";
        y="";
        if(a[i][0]>='a'&&a[i][0]<='c'){
            c=a[i][0];
            f=1;
        }
        else{
            f=0;
        }
        if(f==1){
            p[i][1]=c-'a'+1;
            for(int j=2;j<a[i].find(' ',2+1);j++){
                x+=a[i][j];
            }
            for(int j=a[i].find(' ',2+1)+1;j<a[i].size();j++){
                y+=a[i][j];
            }
            p[i][2]=s_i(x);
            p[i][3]=s_i(y);
        }
        if(f==0){
            p[i][1]=c-96;
            for(int j=0;j<a[i].find(' ',0);j++){
                x+=a[i][j];
            }
            for(int j=a[i].find(' ',0)+1;j<a[i].size();j++){
                y+=a[i][j];
            }
            p[i][2]=s_i(x);
            p[i][3]=s_i(y);
        }
    }
    for(int i=1;i<=n;i++){
        if(p[i][1]==1){
            printf("%d+%d=%d\n",p[i][2],p[i][3],p[i][2]+p[i][3]);
            printf("%d\n",wei(p[i][2])+wei(p[i][3])+wei(p[i][2]+p[i][3])+2);
        }
        if(p[i][1]==2){
            printf("%d-%d=%d\n",p[i][2],p[i][3],p[i][2]-p[i][3]);
            printf("%d\n",wei(p[i][2])+wei(p[i][3])+wei(p[i][2]-p[i][3])+2);
        }
        if(p[i][1]==3){
            printf("%d*%d=%d\n",p[i][2],p[i][3],p[i][2]*p[i][3]);
            printf("%d\n",wei(p[i][2])+wei(p[i][3])+wei(p[i][2]*p[i][3])+2);
        }
    }
    return 0;
}

int s_i(string ah){
    int b=0;
    for(int i=0;i<ah.size();i++){
        b=b*10+(ah[i]-'0');
    }
    return b;
}
int wei(int u){
    int num=0;
    if(u==0) return 1;
    if(u<0) num++;
    while(u!=0){
        u=u/10;
        num++;
    }
    return num;
}

by zhongboxuan123 @ 2022-07-16 08:14:22

@handsome6667633 main里面第四行的getchar()是读什么的? 没看懂


by handsome6667633 @ 2022-07-16 08:59:35

@zhongboxuan123 吃'\n', cin会把回车留在缓存区


by nikodo @ 2022-07-27 14:30:44

@handsome6667633

#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;
}

|