【求助】一直都是80分,到死都不知道那两个为啥是WA

P1055 [NOIP2008 普及组] ISBN 号码

Stardust_Surfer @ 2024-08-23 11:39:05

代码如下:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    char ISBN[13];
    int LastChar, sum[10], _sum, j = 1;
    for (int i = 0; i < 13; i++)
    {
        cin >> ISBN[i];
        if (ISBN[i] != '-')
        {
            sum[j] = ISBN[i] - 48;
            j++;
        }
    }
    for (int i = 1; i <= 9; i++)
    {
        _sum += i * sum[i];
    }
    if (_sum % 11 == ISBN[12] - 48)
    {
        cout << "Right" << endl;    
    }
    else
    {
        if (_sum % 11 == 10)
        {
            ISBN[12] = 'X';
        }
        else
        {
            ISBN[12] = _sum % 11 + '0';
        }
        cout << ISBN;
    }
}

by Stardust_Surfer @ 2024-08-23 11:39:55

不要在意那个LastChar,打的时候忘记删了


by XiaoYao789 @ 2024-08-23 11:49:50

#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    char ISBN[13];
    int sum[10], _sum=0, j = 1; //_sum初始化为0
    for (int i = 0; i < 13; i++)
    {
        cin >> ISBN[i];
        if (ISBN[i] != '-')
        {
            sum[j] = ISBN[i] - 48;
            j++;
        }
    }
    for (int i = 1; i <= 9; i++)
    {
        _sum += i * sum[i];
    }
    if (_sum % 11 == ISBN[12] - 48)
    {
        cout << "Right" << endl;
    }
    else
    {
        if (_sum % 11 == 10)
        {
            ISBN[12] = 'X';
        }
        else
        {
            ISBN[12] = _sum % 11 + '0';
        }
        cout << ISBN;
    }
}

@Stardust_Surfer


by XiaoYao789 @ 2024-08-23 11:51:22

#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    char ISBN[13];
    int sum[10], _sum=0, j = 1;
    for (int i = 0; i < 13; i++)
    {
        cin >> ISBN[i];
        if (ISBN[i] != '-')
        {
            sum[j] = ISBN[i] - 48;
            j++;
        }
    }
    for (int i = 1; i <= 9; i++)
    {
        _sum += i * sum[i];
    }
    if (_sum % 11 == ISBN[12] - 48)
    {
        cout << "Right" << endl;
    }
    else
    {
        if (_sum % 11 == 10)
        {
            ISBN[12] = 'X';
        }
        else
        {
            ISBN[12] = _sum % 11 + '0';
        }
        cout<<0;
        for(int i=0;i<=12;i++) cout<<ISBN[i];
    }
}

by XiaoYao789 @ 2024-08-23 11:51:54

刚才那个错了,看新发的


by Stardust_Surfer @ 2024-08-23 16:09:34

新代码AC了,但是我感觉和原来80分的代码没啥区别,各位大佬能帮忙看一下为啥这样做可以AC

#include <bits/stdc++.h>
using namespace std;

int main()
{
    char ISBN[14], LastChar;
    int sum[10], _sum = 0, j = 1, CheckNumber;
    cin >> ISBN;
    for (int i = 0; i < 11; i++)
    {
        if (ISBN[i] != '-')
        {
            sum[j] = ISBN[i] - 48;
            j++;
        }
    }
    for (int i = 1; i <= 9; i++)
    {
        _sum += i * sum[i];
    }
    CheckNumber = _sum % 11;
    if (_sum % 11 == 10)
    {
        LastChar = 'X';
    }
    else
    {
        LastChar = CheckNumber + '0';
    }
    if (ISBN[12] == LastChar)
    {
        cout << "Right" << endl;
    }
    else
    {
        ISBN[12] = LastChar;
        cout << ISBN << endl;
    }
    return 0;
}

|