帮我看看我代码哪里有问题吧,感谢感谢感谢

P1055 [NOIP2008 普及组] ISBN 号码

QYlucky @ 2022-04-04 16:29:02

#include<iostream>
using namespace std;
int main() {
    char a[20];
    int code,sum;
    for(int i=0;i<13;i++){
        cin>>a[i];
    }
    code=(a[0]-'0')*1+(a[2]-'0')*2+(a[3]-'0')*3+(a[4]-'0')*4+(a[6]-'0')*5+(a[7]-'0')*6+(a[8]-'0')*7+(a[9]-'0')*8+(a[10]-'0')*9;
    sum=code%11;
    if(a[12]==sum){
        cout<<"Right"<<endl;
    }
    else{
        for(int i=0;i<12;i++)
        cout<<a[i];
        cout<<sum<<endl;
    }
    return 0;
}

by Eason_AC @ 2022-04-04 17:00:23

@QYlucky

if(a[12]==sum){

改成:

if(a[12]-'0'==sum){

因为 a[12] 是一个数字字符,后者则将其转化为了数字。


by QYlucky @ 2022-04-04 17:08:34

@Eason_AC 还是通过不了怎么办


by Eason_AC @ 2022-04-04 17:10:39

@QYlucky 艹,忘记考虑 sum=10 的情况了。

还是那一行,改成:

if(a[12]-'0'==sum||(sum==10&&a[12]=='X')){

by Eason_AC @ 2022-04-04 17:13:12

还有:

cout<<sum<<endl;

改成:

if(sum==10)
            cout<<'X'<<endl;
        else
            cout<<sum<<endl;

by QYlucky @ 2022-04-04 17:21:57

@Eason_AC 感谢大佬!!!!


|