为啥识别码算不对啊?求助!

P1055 [NOIP2008 普及组] ISBN 号码

lwj200623 @ 2024-09-02 17:39:44

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

int main()
{
    string s;
    int a;
    cin >> s;
    a = (s[0] * 1) + (s[2] * 2) + (s[3] * 3) + (s[4] * 4) + (s[6] * 5) + (s[7] * 6) + (s[8] * 7) + (s[9] * 8) + (s[10] * 9);
    if (a % 11 == s[12])
    {
        cout << "Right";
    }
    else if (a % 11 == 10)
    {
        for (int i = 0; i < 12; i++)
        {
            cout << s[i];
        }
        cout << "X";

    }
    else
    {
        for (int i = 0; i < 12; i++)
        {
            cout << s[i];
        }
        cout << a % 11<<endl;
    }
}

by pmkmzfuzsotqotmzs @ 2024-09-02 18:02:33

@lwj200623
string类型怎么可以用乘法呢,你得把string转成int


by pmkmzfuzsotqotmzs @ 2024-09-02 18:07:18

@lwj200623
ac code:

#include<iostream>
#include<string>
using namespace std;
int num[100100];
int main()
{
    string s;
    cin>>s;
    int i_=1;
    for(int i=0;i<s.length()-1;i++)
    {
        if(s[i]!='-')
        {
            num[i_]=s[i]-'0';
            i_++;
        }
    }
    int sum=0;

    for(int i=1;i<i_;i++)
    {
        sum+=num[i]*i;
    }
    if(sum%11==s[s.length()-1]-'0')
    {
        cout<<"Right";
    }
    else if(sum%11==10&&s[s.length()-1]=='X')
    {
        cout<<"Right";
    }
    else
    {
        for(int i=0;i<s.length()-2;i++)
        {
            cout<<s[i];
        }
        if(sum%11==10)
            cout<<"-"<<"X";
        else
            cout<<"-"<<sum%11;
    }
    cout<<endl;
    return 0;
}

by lwj200623 @ 2024-09-02 18:09:35

@pmkmzfuzsotqotmzs 哦,原来是这样


by pmkmzfuzsotqotmzs @ 2024-09-02 19:25:30

@lwj200623 而且1SBN号码的最后一位有可能是十位数,不能把循环大小设置为12


|