QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#749651#7698. ISBN ConversionalexhamidiWA 0ms3736kbC++141.6kb2024-11-15 08:48:072024-11-15 08:48:08

Judging History

你现在查看的是最新测评结果

  • [2024-11-15 08:48:08]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3736kb
  • [2024-11-15 08:48:07]
  • 提交

answer

#include <iostream>

using namespace std;



bool isValid(string ups) {

    string s = "";
    if (ups.back() == '-' || ups.front() == '-') {
        return false;
    }
    int numH = 0;
    bool prevH = false;
    for (char c : ups) {
        if (c=='-') {
            if (prevH) {
                return false;
            }
            numH++;
            prevH = true;
        } else {
            prevH = false;
            s.push_back(c);
        }
    }
    if (s.size() != 10) {
        return false;
    }

    if (numH > 3) {
        return false;
    }
    if (numH == 3 && ups[ups.size()-2] != '-') { //here is where we fail
        return false;
    }
    int S = 0;
    for (int i = 0; i < 9; i++) {
        S += (10-i) * (s[i]-'0');
    }
    S += s.back() == 'X' ? 10 : s.back() - '0';
    cout << S << "\n";
    return (S%11==0);
}



string convert(string ups) {
    string ans = "978-" + ups;



    int pre = 38;
    bool three = true;
    for (int i = 0; i < ups.size()-1;i++) {
        char c = ups[i];
        if (c != '-') {
            int val = c-'0';
            if (three) {
                pre += 3*val;
            } else {
                pre += val;
            }
            three = !three;
        }
    }

    ans.back() = (10 - (pre%10))+'0';
    return ans;
}

int main() {

    string out = "";

    int n;
    cin >> n;
    while (n--) {
        string s;
        cin >> s;
        if (isValid(s)) {
            out += convert(s) + "\n";
        } else {
            out += "invalid\n";
        }
    }
    cout << out;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3736kb

input:

4
3-540-4258-02
039428013X
3-540-42580-2
0-14-028333-3

output:

198
187
123
invalid
978-0394280134
978-3-540-42580-9
invalid

result:

wrong answer 1st lines differ - expected: 'invalid', found: '198'