八十分求助

P1055 [NOIP2008 普及组] ISBN 号码

ltzx2022_zhanghr_5 @ 2023-07-21 15:12:08

#include<bits/stdc++.h>
using namespace std;
char a[105];int sum=0,ans=0,ac=0;
int main(){
    cin.getline(a,105);
    int u=strlen(a);
    for(int i=0;i<strlen(a)-1;i++){
        if(a[i]!='-'){
            sum++;
            ans+=sum*(a[i]-'0');
            continue;
        }
    }
    ans%=11;
    //cout<<ans;
    bool b=true;
    if(ans!=10){
    for(int i=0;i<strlen(a);i++){
        if(i==strlen(a)-1){
            if(a[i]-'0'==ans){
                cout<<"Right";
            }
            else b=false;
        }
    }
    if(b==false){
        for(int i=0;i<strlen(a);i++){
            if(i==strlen(a)-1){
                cout<<ans;
            }
            else {
                cout<<a[i];
            }
        }
    }
    }
    else{
        for(int i=0;i<strlen(a);i++){
            if(i==strlen(a)-1){
                cout<<"X";
            }
            else {
                cout<<a[i];
            }
        }
    }
}

by xingxing121 @ 2023-07-22 18:22:30

输入的也可能是X


by xingxing121 @ 2023-07-22 18:23:08

特别暴力写法如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

int main(){
    int a,b,c;
    char d;
    int d1;
    scanf("%d-%d-%d-%c",&a,&b,&c,&d);
    if(d=='X'){
        d1 = 10;
    }else{
        d1 = d-'0';//转为数字 
    }
    int two = b/100;
    int three = b/10%10;
    int four = b%10;
    int five = c/10000;
    int six = c/1000%10;
    int seven = c/100%10;
    int eight = c/10%10;
    int nine = c%10;
    int sum = a+two*2+three*3+four*4+five*5+six*6+seven*7+eight*8+nine*9;
    if(sum%11==d1){
        cout<<"Right";
    }else if(sum%11==10){
        printf("%d-%d-%d-X",a,b,c);
    }else{
        printf("%d-%d-%d-%d",a,b,c,sum%11);
    }

    return 0;
}

by ltzx2022_zhanghr_5 @ 2023-07-23 10:05:59

好的,谢谢大佬


|