70分求助

P1055 [NOIP2008 普及组] ISBN 号码

landernal @ 2022-02-01 22:40:30

#include <iostream>
using namespace std;
char s[10005];
int j, sum;
int main(){
    cin >> s;
    for(int i = 0; i <= 10; i++) {
        if(s[i] <= '9' && s[i] >= '0') {
            sum += (s[i] - '0') * (j + 1);
            j++;
        }
    }
    int a = s[12] - '0', b = sum % 11;
    if((a == b && b < 10) || (s[12] == 'X' && b == 10)) {
        cout << "right" << endl;
        return 0;
    }else{
        if(b == 10) {
            s[12] = 'X';
        }else{
            s[12] = b + '0';
        }
        cout << s << endl;
    }
    return 0;
}

by yuhaoran666 @ 2022-02-02 00:41:58

本人的代码,仅供参考

#include<bits/stdc++.h>
using namespace std;
char a1[15] = {};
int a[10], cnt;
int main() {
    cin >> a1;
    for (int i = 0; i < strlen(a1) - 2; i++) {
        if (a1[i] == '-') continue;
        if (a1[i] >= '0' && a1[i] <= '9') {
            a[cnt] = a1[i] - 48;
            cnt++;
        }
    }
    int temp = 0, inn = 0;
    if (a1[12] == 'X') inn = 10;
    else inn = (int)(a1[12] - 48);
    for (int i = 0; i < cnt; i++) {
        temp += a[i] * (i + 1);
    }
    a[9] = temp % 11;
    if (a[9] == inn) cout << "Right";
    else {
        for (int i = 0; i < strlen(a1) - 1; i++) {
            cout << a1[i];
        }
        if (a[9] == 10) cout << 'X';
        else cout << a[9];
    }
    return 0;
}

|