help me!

P1055 [NOIP2008 普及组] ISBN 号码

ycx20120224 @ 2023-08-13 18:42:39

#include<iostream>
#include<string>
using namespace std;
int main()
{
    char a[13];
    char n;
    long long sum=0,h=1;
    cin>>a;
    for(int i=0;i<=10;i++)
    {
        if(a[i]!='-')
        {
            sum=sum+(a[i]-'0')*h;
            h++;
        }
    }
    sum=sum%11;
    if(sum>=10)n='X';
    else n=sum;
    if(n==a[12])
    {
        cout<<"Right";
    }
    else 
    {
        a[12]=(char)n;
        cout<<a;
    }
    return 0;
}

by ycx20120224 @ 2023-08-13 18:44:18

不会了


by Gohldg @ 2023-08-13 18:51:04

@ycx20120224 以下代码供参考:

#include <iostream>
#include <cstring>
#include <string> 
using namespace std;
char a[13];
int main(){
    cin>>a;
    int a1=(a[0]-48)*1;
    int a2=(a[2]-48)*2;
    int a3=(a[3]-48)*3;
    int a4=(a[4]-48)*4;
    int a5=(a[6]-48)*5;
    int a6=(a[7]-48)*6;
    int a7=(a[8]-48)*7;
    int a8=(a[9]-48)*8;
    int a9=(a[10]-48)*9;
    int a10=(a1+a2+a3+a4+a5+a6+a7+a8+a9)%11;
    if(a10==10&&a[12]=='X'||a10==a[12]-48){
        cout <<"Right";
    }else if(a10==10&&a[12]!='X'){
        for(int i=0;i<strlen(a)-1;i++){
            cout <<a[i];
        }
        cout <<"X";
    }else{
        for(int i=0;i<strlen(a)-1;i++){
            cout <<a[i];
        }
        cout <<a10;
    }
    return 0;
}

by ycx20120224 @ 2023-08-14 07:41:23

@Gohldg 谢谢


|