新人求助,样例都没过不知道输出的什么鬼东西QAQ

P1957 口算练习题

spencer @ 2023-07-09 21:46:02

#include<bits/stdc++.h>
using namespace std;
char op,s[100],l[100],a;
int n;
int getsize(int a){
    int cnt=0;
    if(a<0)a=-a;
    if(a==0)return 1;
    while(a>0){
        a/=10;
        cnt++;
    }
    return cnt;
}

int main(){
    cin>>n;
    while(n--){
        char l[100],s[100];
        scanf("%s",l);
        if(l[0]<'0'||l[0]>'9'){
            op=l[0];//if there is an operator
            for(int i=2;i<strlen(l);i++){
                s[i-2]=l[i];
            }
        }
        else {
            for(int i=0;i<strlen(l);i++){
                s[i]=l[i];
            }
        }
        int x,y,flag=0,len=strlen(s);
        for(int i=0;i<len;i++){
            if(s[i]!=' '&&flag==0)x=x*10+(s[i]-'0');
            if(s[i]!=' '&&flag==1)y=y*10+(s[i]-'0');
            if(s[i]==' ')flag=1;
        }
        cout<<x;
        int ans=0,k=2;
        if(op=='a')cout<<'+'<<y<<'='<<x+y<<'\n',ans=x+y;
        if(op=='b')cout<<'-'<<y<<'='<<x-y<<'\n',ans=x-y;
        if(op=='c')cout<<'*'<<y<<'='<<x*y<<'\n',ans=x*y;
        if(ans<0)k++;
        cout<<getsize(x)+getsize(y)+getsize(ans)+k<<'\n';

    }   
    return 0;
}

by a_lucky_star_of_love @ 2023-07-09 21:49:15

let me see


by chenfeizhou @ 2023-07-09 22:06:44

RE的原因是你没有为变量x和y初始化初始值


by chenfeizhou @ 2023-07-09 22:07:40

int x = 0, y = 0, flag = 0, len = strlen(s);

这样子^_^


by chenfeizhou @ 2023-07-09 22:08:32

@spencer


by spencer @ 2023-07-10 20:50:17

@chenfeizhou ok,谢谢大佬


by spencer @ 2023-07-10 20:54:27

@chenfeizhou 可是好像还是有问题qaq


|