有没有大神教一下为什么RE了

P1957 口算练习题

yishanyi @ 2023-08-11 20:11:26

#include<bits/stdc++.h>

using namespace std;
int n;
string s;

char change(char ch) {
    if (ch == 'a') return '+';
    else if (ch == 'b') return '-';
    else if (ch == 'c') return '*';
}

string str(char ex) {
    string ss;
    ss += ex;
    return ss;
}

int getnum(int num) {
    int b = 10;
    int i = 1;
    if (num < 0) i++;
    while (num % b != num) {
        b *= 10;
        i++;
    }
    return i;
}

int main() {
    cin >> n;
    char ex;
    getchar();
    while (n--) {
        getline(cin, s);
        if (s[0] == 'a' || s[0] == 'b' || s[0] == 'c') {
            ex = change(s[0]);
            s.erase(0, 2);
        }
        int l, r, re;
        int i = 0;
        int number = 0;
        while (s[i] != ' ') {
            number = number * 10 + s[i] - '0';
            i++;
        }
        l = number;
        number = 0;
        i++;
        for (i; i < s.size(); i++) {
            number = number * 10 + s[i] -'0';
        }
        r = number;
        if (ex == '+') re = l + r;
        else if (ex == '-') re = l - r;
        else re = l * r;
        for (i = 0; i < s.size(); i++) {
            if (s[i] == ' ') {
                s.replace(i, 1,str(ex));
                break;
            }
        }
        s += '=';
        cout << s << re << endl << s.size() + getnum(re) << endl;
    }
    return 0;
}

第一个点我的编译器是能跑的


by liusishu @ 2023-08-11 21:00:03

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

by yishanyi @ 2023-08-12 16:31:24

@liusishu 必须用字符数组吗


|