P1055-ISBN号码,#1不过,求助

P1055 [NOIP2008 普及组] ISBN 号码

asdfghjklzxcv @ 2023-11-25 23:10:56

#include<iostream>

using namespace std;
int main()
{
    string a;
    cin>>a;
    int i,sum=0,j=1,temp;
    for(i=0;i<11;i++)
    {
        if(a[i]!='-')
        {
            temp=a[i]-48;
            sum+=j*temp;
            j++;
        }
    }
    int num=sum%11;
    int c=a[12]-48;
    if(num==c&&a[12]>=0&&a[12]<=9||num==10&&a[12]=='X')
    {
        cout<<"Right";
    }
    else
    {
        for(i=0;i<12;i++)
        {
            cout<<a[i];
        }
        if(num==10)
        cout<<'X';
        else
        cout<<num;

    }
    return 0;
}

by xibaby @ 2023-11-25 23:58:11

AC Code:

#include<cstdio>
using namespace std;
char a,b,c,d,e,f,g,h,i,j; int s;
int main(){
    scanf("%c-%c%c%c-%c%c%c%c%c-%c",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j);
    s=(a-'0')*1+(b-'0')*2+(c-'0')*3+(d-'0')*4+(e-'0')*5+(f-'0')*6+(g-'0')*7+(h-'0')*8+(i-'0')*9;
    s%=11;
    if((s==10&&j=='X')||s==j-'0') printf("Right");
    else
      printf("%c-%c%c%c-%c%c%c%c%c-%c",a,b,c,d,e,f,g,h,i,s==10?'X':s+'0');
    return 0;
}

若要感谢关注足矣......


by Azzero @ 2023-12-01 11:12:43

第一个样例就没过,0-670-82162-4

if(num==c&&a[12]>=0&&a[12]<=9||num==10&&a[12]=='X')

这个if判断前半句num==c就够了,后面&&a[12]>=0&&a[12]<=9没必要,因为a[]数组是字符,肯定和数字相等


by Azzero @ 2023-12-01 11:14:39

@Azzero 字符和数字肯定不相等。题目说了测试数据一定是ISBN编码格式,最后一位肯定是数字或者说X


|