哪里错了?一直看不出

P1055 [NOIP2008 普及组] ISBN 号码

Tang1106 @ 2024-09-22 19:02:59

#include<iostream>
using namespace std;
int main() {
    char s[13],c;
    int sum=0,k=0;
    cin>>s;
    for(int i=0;i<11;i++) {
        if(s[i]=='-') {
            k++;
            sum+=(s[i]-'0')*k;
        }
    }sum%=11;
    if(sum==10) {
        c='X';
    }else {
        c=sum+'0';
    }
    if(s[12]==c) {
        cout<<"Right";
    }else {
        s[12]=c;
        cout<<s;
    }

    return 0;
}

by I2147483647I @ 2024-10-14 16:22:54

改了:


#include<iostream>
using namespace std;
int main() {
    char s[13],c;
    int sum=0,k=0;
    cin>>s;
    for(int i=0;i<11;i++) {
        if(s[i]!='-') {//此处,不是-时k++,而是数字时k++.
            k++;
            sum+=(s[i]-'0')*k;//在数字时才累加
        }
    }sum%=11;
    if(sum==10) {
        c='X';
    }else {
        c=sum+'0';
    }
    if(s[12]==c) {
        cout<<"Right";
    }else {
        s[12]=c;
        cout<<s;
    }

    return 0;
}

by I2147483647I @ 2024-10-14 16:24:54

@Tang1106 帮你改了 (求关)


|