C++98:满江红求助

P1055 [NOIP2008 普及组] ISBN 号码

__Harry_Haiyun__ @ 2023-05-18 18:19:00


#include <bits/stdc++.h>
using namespace std;
int main()
{
    string ISBN;
    cin >> ISBN;
    int sum = 0;
    for (int i = 0; i < ISBN.length() - 1; i++)
    {
        if (ISBN[i] >= '0' && ISBN[i] <= '9') sum += ISBN[i] - 48;
    }
    sum %= 11;
    string print = "Right";
    if (ISBN[12] == sum + 48) print = "Right";
    else if (ISBN[12] == 'X' && sum == 10) print = "Right";
    else 
    {
        if (sum == 10)
        {
            for (int i = 0; i < ISBN.length() - 1; i++) cout << ISBN[i];
            cout << "X" << endl;
            return 0;
        }
        else
        {
            for (int i = 0; i < ISBN.length() - 1; i++) cout << ISBN[i];
            cout << sum << endl;
            return 0;
        }
    }
    return 0;
}

by zxdjmq @ 2023-05-18 18:31:15

首位数字乘以 1 加上次位数字乘以 2 ……以此类推,用所得的结果 mod 11 所得的余数即为识别码

但是你没有乘诶


by DailyPracticeAnn @ 2023-05-18 18:33:47

注意到这句话,每位数字还要乘一个位权的,不能直接累加。


by yzm0325 @ 2023-05-18 19:20:57

@Harry_Haiyun 位权呢


by __Harry_Haiyun__ @ 2023-05-18 22:14:52

@zxdjmq @DailyPracticeAnn @Zhuyiming0325 按照你们说的来做的话:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string ISBN;
    cin >> ISBN;
    int sum = 0,time = 1;
    for (int i = 0; i < ISBN.length() - 1; i++)
    {
        if (ISBN[i] == '1') sum += 1 * time;
        else if (ISBN[i] == '2') sum += 2 * time;
        else if (ISBN[i] == '3') sum += 3 * time;
        else if (ISBN[i] == '4') sum += 4 * time;
        else if (ISBN[i] == '5') sum += 5 * time;
        else if (ISBN[i] == '6') sum += 6 * time;
        else if (ISBN[i] == '7') sum += 7 * time;
        else if (ISBN[i] == '8') sum += 8 * time;
        else if (ISBN[i] == '9') sum += 9 * time;
        time++;
    }
    sum %= 11;
    string print = "Right";
    if (ISBN[12] == '0' && sum == 0) print = "Right";
    else if (ISBN[12] == '1' && sum == 1) print = "Right";
    else if (ISBN[12] == '2' && sum == 2) print = "Right";
    else if (ISBN[12] == '3' && sum == 3) print = "Right";
    else if (ISBN[12] == '4' && sum == 4) print = "Right";
    else if (ISBN[12] == '5' && sum == 5) print = "Right";
    else if (ISBN[12] == '6' && sum == 6) print = "Right";
    else if (ISBN[12] == '7' && sum == 7) print = "Right";
    else if (ISBN[12] == '8' && sum == 8) print = "Right";
    else if (ISBN[12] == '9' && sum == 9) print = "Right";
    else if (ISBN[12] == 'X' && sum == 10) print = "Right";
    else 
    {
        if (sum == 10)
        {
            for (int i = 0; i < ISBN.length() - 1; i++) cout << ISBN[i];
            cout << "X" << endl;
            return 0;
        }
        else
        {
            for (int i = 0; i < ISBN.length() - 1; i++) cout << ISBN[i];
            cout << sum << endl;
            return 0;
        }
    }
    cout << print << endl;
    return 0;
}

最后终于得到了10分,打破了“满江红”的局面。 谢谢了


by yzm0325 @ 2023-05-18 22:34:11

@Harry_Haiyun no,请仔细读题,您对题意的理解有误


by zxdjmq @ 2023-05-20 12:21:01

@Harry_Haiyun 应当识别到数字才time++


|