C++ 20,本地能编译,提交过不了

P1957 口算练习题

eofitg @ 2023-11-20 21:17:49

是C++20没错吧,本地我在CLion里面可以编译运行样例也能过,但是一提交连编译都编译不了,是split的问题吗,请教有没有懂这方面的人指点一下

# include <cstdio>
# include <iostream>
# include <sstream>
# include <cstring>

using namespace std;

vector<string> stringSplit(const string& str, char delim) {
    stringstream ss(str);
    string item;
    vector<string> elems;
    while (getline(ss, item, delim)) {
        if (!item.empty()) {
            elems.push_back(item);
        }
    }
    return elems;
}

int str2int(string str) {
    int n;
    istringstream iss;
    iss.clear();
    iss.str(str);
    iss>>n;
    return n;
}

int len(int n) {
    int ans = 0;
    if (n < 0) {
        ans ++;
        n = -n;
    }
    if (n > 0) {
        while (n) {
            n /= 10;
            ans ++;
        }
    } else {
        ans ++;
    }
    return ans;
}

int n;
string ch;
string symbol;

int main() {

    scanf("%d", &n);
    getchar();
    for (int i = 0; i < n; i ++) {
        getline(cin, ch);
        vector<string> str = stringSplit(ch,' ');
        int a = 0, b = 0;
        string sa, sb;
        if (str.size() == 3) {
            symbol = str[0];
            a = str2int(str[1]);
            b = str2int(str[2]);
            sa = str[1];
            sb = str[2];
        } else {
            a = str2int(str[0]);
            b = str2int(str[1]);
            sa = str[0];
            sb = str[1];
        }
        if (symbol == "a") {
            cout<<a<<'+'<<b<<'='<<a + b<<endl;
            cout<<sa.length() + sb.length() + 2 + len(a + b)<<endl;
        } else if (symbol == "b") {
            cout<<a<<'-'<<b<<'='<<a - b<<endl;
            cout<<sa.length() + sb.length() + 2 + len(a - b)<<endl;
        } else if (symbol == "c") {
            cout<<a<<'*'<<b<<'='<<a * b<<endl;
            cout<<sa.length() + sb.length() + 2 + len(a * b)<<endl;
        }
    }

    return 0;
}

by tder @ 2023-11-20 21:22:48

#include <vector>


by eofitg @ 2023-11-20 22:35:49

@tder 感谢解答

刚才又排查几遍,加上这一行也是能过编译了但是还是RE 翻了翻一些讨论感觉可能是getline的问题,刚才写了一个不用getline的就AC了(

# include <cstdio>
# include <iostream>
# include <sstream>
# include <string>

using namespace std;

struct problem {
    string symbol;
    int a, b;
    string sa, sb;
} p[50];

int str2int(string str) {
    int n;
    istringstream iss;
    iss.clear();
    iss.str(str);
    iss>>n;
    return n;
}

int len(int n) {
    int ans = 0;
    if (n < 0) {
        ans ++;
        n = -n;
    }
    if (n > 0) {
        while (n) {
            n /= 10;
            ans ++;
        }
    } else {
        ans ++;
    }
    return ans;
}

int n;
string ch;

int main() {

    scanf("%d", &n);
    getchar();
    for (int i = 0; i < n; i ++) {
        cin>>ch;
        int a = 0, b = 0;
        if (ch[0] >= 'a' && ch[0] <= 'c') {
            cin>>a>>b;
            p[i].symbol = ch;
        } else {
            a = str2int(ch);
            cin>>b;
            p[i].symbol = p[i - 1].symbol;
        }
        p[i].a = a;
        p[i].b = b;
    }

    for (int i = 0; i < n; i ++) {
        string symbol = p[i].symbol;
        int a = p[i].a, b = p[i].b;
        string sa = p[i].sa, sb = p[i].sb;
        if (symbol == "a") {
            cout<<a<<'+'<<b<<'='<<a + b<<endl;
            cout<<len(a) + len(b) + 2 + len(a + b)<<endl;
        } else if (symbol == "b") {
            cout<<a<<'-'<<b<<'='<<a - b<<endl;
            cout<<len(a) + len(b) + 2 + len(a - b)<<endl;
        } else if (symbol == "c") {
            cout<<a<<'*'<<b<<'='<<a * b<<endl;
            cout<<len(a) + len(b) + 2 + len(a * b)<<endl;
        }
    }

    return 0;
}

|