C 30分求助

P1055 [NOIP2008 普及组] ISBN 号码

Gyicyo @ 2022-11-09 13:23:22

#include<stdio.h>
int main()
{
    char s[14];
    int sum=0,ans=0;
    scanf("%s",s);
    for(int i=1,j=0;i<=9;i++)
    {
        if(!(s[j]>='0'&&s[j]<='9'))
        {
            j++;
            sum+=(s[j]-48)*i;
            j++;
        }
        else
        {
            sum+=(s[j]-48)*i;
            j++;
        }

    }
    ans=sum%11;//识别码
    if(ans+48==s[12]&&ans!=10)
        printf("Right");
    else if(ans==10&&s[12]=='X')
        printf("Right");
    else
    {
        if(ans!=10)
        printf("%s\b%c",s,ans+48);
        else
            printf("%s\bX",s);
        }
    return 0;
}

by Li_mz__ @ 2022-11-09 13:48:27

@Gyicyo

#include <bits/stdc++.h>
using namespace std;
string s,mod = "0123456789X";
int a,j = 1;
int main(){
    cin>>s;
    for(int i = 0 ; i < 12 ; i ++){
        if(s[i] == '-'){
            continue;
        }
        a += (s[i] - '0') * j;
        j ++;
    }
    if(s[12] == mod[a % 11]){
        cout<<"Right";
    }
    else{
        s[12] = mod[a % 11];
        cout<<s;
    }
}

by VitrelosTia @ 2022-11-09 15:08:52

@Li_mz__ 你觉得贴代码对于lz有任何帮助吗?


|