P1055 #4和#7WA求助

P1055 [NOIP2008 普及组] ISBN 号码

a046957 @ 2022-05-11 12:39:19

求教大佬帮忙看下哪里有问题啊>﹏<

C++ 14(GCC 9):

#include<iostream>
using namespace std;
int main()
{
    char a[13];
    int arr[10];
    for (int i = 0; i < 13; i++)
    {
        cin >> a[i];
    }

    int j = 0;
    for (int i = 0; i < 13; i++)
    {
        if (i == 1 || i == 5 || i == 11)
        {
            continue;
        }
        else
        {
            arr[j] = a[i] - '0';  // arr 里存的是ISBN里的数字(不包括"-")
            j++;
        }
    }

    int len = 0;
    int x = 1;
    for (int i = 0; i < 9; i++)
    {
        len = len + arr[i] * x;
        x++;
    }
    len = len % 11;

    if (arr[9] == len)
    {
        cout << "Right";
    }
    else
    {
        arr[9] = len;

        cout << arr[0] << "-";
        for (int i = 1; i < 4; i++)
        {
            cout << arr[i];
        }
        cout << "-";

        for (int i = 4; i < 9; i++)
        {
            cout << arr[i];
        }
        cout << "-";

        if (arr[9] == 10)
        {
            cout << "X";
        }
        else
        {
            cout << arr[9];
        }
    }

    return 0;
}

by Li_wenjie @ 2022-05-11 20:54:57

当a[12]为‘X’时你没有判断len是不是等于10啊 修改了一下



include<iostream>
using namespace std; int main() { char a[13]; int arr[10]; for (int i = 0; i < 13; i++) { cin >> a[i]; }

int j = 0;
for (int i = 0; i < 13; i++)
{
    if (i == 1 || i == 5 || i == 11)
    {
        continue;
    }
    else
    {
        arr[j] = a[i] - '0'; 
        j++;
    }
}

int len = 0;
int x = 1;
for (int i = 0; i < 9; i++)
{
    len = len + arr[i] * x;
    x++;
}
len = len % 11;

if (arr[9] == len||(a[12]=='X'&&len==10))
{
    cout << "Right";
}
else
{
    arr[9] = len;

    cout << arr[0] << "-";
    for (int i = 1; i < 4; i++)
    {
        cout << arr[i];
    }
    cout << "-";

    for (int i = 4; i < 9; i++)
    {
        cout << arr[i];
    }
    cout << "-";

    if (arr[9] == 10)
    {
        cout << "X";
    }
    else
    {
        cout << arr[9];
    }
}

return 0;
}

by a046957 @ 2022-05-11 22:34:51

@Li_wenjie 感谢大佬指点!


|