大佬们,帮忙看看有什么优化方法,敲的时候一直改越堆越长

P1055 [NOIP2008 普及组] ISBN 号码

hejh_ @ 2025-01-05 15:56:10

#include<stdio.h>
#include<ctype.h>
int main() {
    char a[14];
    int b[10] = { 0 };
    fgets(a, sizeof(a),stdin);
    int number = 0;
    for (int i = 0; a[i] != '\0'; i++) {
        if (isdigit(a[i])) {
            b[number] = a[i] - '0';
            number++;
        }
        if (a[i] == 'X') {
            b[number] = 'X';
            number++;
        }
    }
    //求出判定值
    int sum = 0;
    for (int i = 0; i < 9; i++) {
        sum += (i + 1) * b[i];
    }
    //判断对错
    int is = 0;
    if (sum % 11 == 10) {
        if (b[9] == 'X') {
            printf("Right");
            is = 1;
        }
    }else if (b[9] == sum % 11) {
        printf("Right");
        is = 1;
    }
    if (is == 0) {
        if (sum % 11 == 10) {
            b[9] = 'X';
        }
        else {
            b[9] = sum % 11;
        }
        int s = 1;
        for (int i = 0; i < 10; i++) {
            if (sum % 11 == 10) {
                if (i < 9) {
                    printf("%d", b[i]);
                    s++;
                }
                else {
                    printf("%c", b[i]);
                    s++;
                }
            }
            else {
                printf("%d", b[i]);
                s++;
            }
            if (s == 2 || s == 6 || s == 12) {
                printf("-");
                s++;
            }
        }
    }

    return 0;
}

by csxx601cjy @ 2025-01-05 16:09:32


#include<bits/stdc++.h>
using namespace std;
string s;
int a[100],l=0,n;
int main(){
    cin>>s;
    for(int i=0;i<s.size();i++)
        if(s[i]!='-'&&s[i]!='X')
            a[l++]=s[i]-'0';
    n=(a[0]+a[1]*2+a[2]*3+a[3]*4+a[4]*5+a[5]*6+a[6]*7+a[7]*8+a[8]*9)%11;
    if(s[12]==(n+'0')||(s[12]=='X'&&n==10)) cout<<"Right";
    else {
        for(int i=0;i<s.size()-1;i++)
            cout<<s[i];
        if(n==10)cout<<'X';
        else cout<<n;
    }
    return 0;
}

|