60分代码,大佬求助错的都是RE

P1957 口算练习题

chenmumin @ 2023-07-21 21:40:48

#include<bits/stdc++.h>
using namespace std;
int main(){
    char l[2],s[105];
    int n,b,c;
    char a;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>l;
        if(l[0]>='a'&&l[0]<='z'){
            a=l[0];
            cin>>b>>c;
        }else{
            sscanf(l,"%d",b);
            cin>>c;
        }
        memset(s,0,sizeof(s));
        if(a=='a'){
            sprintf(s,"%d+%d=%d",b,c,b+c);
        }else if(a=='b'){
            sprintf(s,"%d-%d=%d",b,c,b-c);
        }else if(a=='c'){
            sprintf(s,"%d*%d=%d",b,c,b*c);
        }else sprintf(s,"%d+%d=%d",b,b,b+c);
        cout<<s<<endl<<strlen(s)<<endl;
    }
}

by bk1212 @ 2023-07-21 22:25:44

@chenmumin 应该是这行的问题

sscanf(l,"%d",b);

应该写为

sscanf(l,"%d",&b);

地址符拉写了哟


by chenmumin @ 2023-07-22 12:58:33

@bk1212 谢谢


by chenmumin @ 2023-07-22 12:59:35

@bk1212 改完WA掉了还是60分


by bk1212 @ 2023-07-22 22:13:48

@chenmumin 把l数组改大一点看看?


by bk1212 @ 2023-07-22 22:15:12

@chenmumin 改成10就能过


by bk1212 @ 2023-07-22 22:16:30

@chenmumin 因为你第一个数输入进来,有可能是符号,也有可能是数字,所以说要设大一点,如果只有2位保存不了


|