全WA,可是洛谷IDE跑案例和一些其他的数据好像都没错啊
查看原帖
全WA,可是洛谷IDE跑案例和一些其他的数据好像都没错啊
662286
FrankNanthan楼主2022/2/18 13:33

代码

#include <iostream>
using namespace std;


int str2int(string x){
    int re = 0;
    for (int i = 0; i < x.length(); ++i) {
        if (x[i] >= '0' && x[i]<= '9'){
            int temp = x[i] - '0';
            re = 10 * re + temp;
        }
    }
    return re;
}


int getLen(int x){
    if (x == 0) return 1;
    int dig = 0;
    // 计算负号位
    if (x < 0){
        dig++;
        x = -x;
    }

    for (int i = 1; x / i != 0; i *= 10) {
        dig++;
    }
    return dig;
}


void D(){
    int n;
    cin>>n;
    cin.ignore();
    for (int i = 0; i < n; ++i) {
        string a;
        char op;
        getline(cin, a);
        //开始用的switch case 判断a[0]是'a','b','c'的哪一个,结果发现getline会读如一些奇怪的数据在前面 如 \U0000200e 不知道是什么 导致后面全错 所以改成if;
         if (a.find('a') != -1){
            op = '+';
            a = a.substr(a.find('a') +2);
        } else if (a.find('b') != -1){
            op = '-';
            a = a.substr(a.find('b') +2);
        }else if (a.find('c') != -1){
            op = '*';
            a = a.substr(a.find('c') +2);
        }
        int x1 = str2int(a.substr(0, a.find(' '))),
        x2 = str2int(a.substr(a.find(' ')+ 1));
        int re;
        switch (op) {
            case '+':
                re = x1 + x2;
                break;
            case '-':
                re = x1 - x2;
                break;
            case '*':
                re = x1 * x2;
                break;
        }
        cout<<x1<<op<<x2<<"="<<re<<endl;
        cout<<a.length() + getLen(re) + 1<<endl;
    }
}


int main(){
    D();
    return 0;
}

不知道错误是在哪儿

以下是我尝试的一些案例:

in:
5
c 2620 6343
a 216 1149
b 7761 5655
7168 3961
a 9755 3461

out:
2620*6343=16618660
18
216+1149=1365
13
7761-5655=2106
14
7168-3961=3207
14
9755+3461=13216
15

in:
4
a 64 46
275 125
c 11 99
b 46 64

out:
64+46=110
9
275+125=400
11
11*99=1089
10
46-64=-18
9

2022/2/18 13:33
加载中...