P1055Help
查看原帖
P1055Help
338370
Merron楼主2020/7/13 17:27

ISBN号码

我代码错在哪?逻辑上应该没问题吧?

#include <iostream>
using namespace std ;

int Num[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9} ;
char str[10] ;
int num[10] ;

void Set ()
{
    for (int i = 0 ;i < 10 ;i ++)
    {
        num[i] = str[i] - 48 ;
    }
}

int Check ()
{
    Set();

    int ans = 0;
    for(int i = 0 ;i <= 8 ;i ++)
    {
        ans += (i + 1) * num[i];
    }
    ans %= 11 ;
    if(ans == num[9])return 1;
    else num[9] = ans;
}

int main ()
{
    int i = 0 ;
    char p ;
    while (cin >> p)
    {
        if(p != '-')
        {
            str[i] = p ;
            i++ ;
        }
        else
        {
            continue ;
        }
        
    }
    if(Check () == 1)
    cout<<"Right"<<endl ;
    else
    {
        cout<<num[0]<<'-';
        cout<<num[1];
        cout<<num[2];
        cout<<num[3]<<'-';
        cout<<num[4];
        cout<<num[5];
        cout<<num[6];
        cout<<num[7];
        cout<<num[8]<<'-';
        if(num[9]!=10)cout<<num[9];
        else cout<<'x'<<endl;//开玩笑的输出
    }
    return 0;
}
2020/7/13 17:27
加载中...