30分求助!

P1055 [NOIP2008 普及组] ISBN 号码

Algorithm_ZRF @ 2023-07-31 14:25:46

#include<bits/stdc++.h>
using namespace std;
string a;
int n, m, b[114514], j = 1;
char shibiema2;
void read() {
    cin >> a;
}
void jisuan() {
    n = a.size();
    for (int i = 1; i <= n; ++i) {
        if (a[i] >= 0 && a[i] <= 9 ) {
            b[j] = a[i];
            j++;
        }
    }
    j--;
    for (int i = 1; i <= j; ++i) {
        m += b[j] * j;
    }
    m = m % 11;
    if (m == 10) {
        shibiema2 = 'x';
    }
    shibiema2 = '0' + m;
}
void print() {
    if (b[j + 1] == m) {
        printf("Right");
    } else {
        for (int i = 1; i < n; ++i) {
            cout << a[i];
        }
        cout << shibiema2;
    }
}
int main() {
    read();
    jisuan();
    print();
    return 0;
}

by CODuckY @ 2023-07-31 15:06:06

这代码错的好像有点太离谱了,思维有点混乱大致照着你思路改了下

#include<bits/stdc++.h>
using namespace std;
string a;
int n, m=0, b[114514], j = 1;
char shibiema2;
void read() {
    cin >> a;
}
void jisuan() {
    n = a.size();
    for (int i = 0; i < n; ++i) {
        if ((a[i] >= '0' && a[i] <= '9')|| a[i]=='X') {
            b[j] = a[i]-48;
            j++;
        }
    }
    for (int i = 1; i < 10; ++i) {
        m += b[i]* i;
    }
    m = m % 11;
    if (m == 10) {
        shibiema2 = 'X';
    }
    else{shibiema2 = '0' + m;}
}
void print() {
    if (b[10] == shibiema2-48) {
        printf("Right");
    } else {
        for (int i = 0; i < n-1; ++i) {
            cout << a[i];
        }
        cout << shibiema2;
    }
}
int main() {
    read();
    jisuan();
    print();
    return 0;
}

by YSzita @ 2023-07-31 15:08:19

x是大写


by Algorithm_ZRF @ 2023-07-31 15:19:00

@CODuckY 谢谢dalao


|