20分求助!!!(玄关)

P1055 [NOIP2008 普及组] ISBN 号码

joker__wang @ 2023-09-06 19:00:35

ISBN号码20分求调(不知道为什么不输出Right)

#include<bits/stdc++.h>
using namespace std;
char a[100];
int t,l=1;
int main()
{
    cin>>a;
    for(int i=0;i<=strlen(a)-1;i++)
    {
        if(a[i]=='-')
        {
            break;
        }
        else
        {
            t=a[i]*l;
            l++;
        }
    }
    if(t%11==a[strlen(a)])
    {
        cout<<"Right";
    }
    if(t%11>=10)
    {
        if(a[strlen(a)]=='X')
        {
            cout<<"Right";
        }
    }
    else
    {
        for(int i=0;i<=strlen(a)-2;i++)
        {
            cout<<a[i];
        }
    }
    if(a[strlen(a)]>=10)
    {
        cout<<'X';
    }
    else
    {
        cout<<t%11;
    }
}

by _YTY_ @ 2023-09-06 19:13:18

一个判断就好了

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

by so_find_skind @ 2023-09-06 19:15:25

@joker__wang

给你的代码改了一下

#include<bits/stdc++.h>
using namespace std;
char a[100];
int t,l=1;
int main()
{
    cin>>a;
    for(int i=0;i<=strlen(a)-2;i++)
    {
        if(a[i]=='-')
        {
            continue;
        }
        else
        {
            t+=(a[i]-'0')*l;
            l++;
        }
    }
    if(t%11==a[strlen(a)-1]-'0')
    {
        cout<<"Right";
    }
    else if(t%11>=10 && a[strlen(a)-1]=='X')
    {
        cout<<"Right";
    }
    else
    {
        for(int i=0;i<=strlen(a)-2;i++)
        {
            cout<<a[i];
        }
        if(t%11==10)
        {
            cout<<"X";
        }
        else
        {
            cout<<t%11;
        }
    }
}

【汗】


by joker__wang @ 2023-09-07 18:34:33

@zhezhikongdanruxue 大佬,-‘0’是什么意思


by so_find_skind @ 2023-09-07 21:33:29

@joker__wang

就是把一个字符型的数字改成数字型的数字,因为实际上 '1'=49 而非 1


by joker__wang @ 2023-09-09 17:26:51

@zhezhikongdanruxue 谢谢


|