【CCF CSP】 201312-2 ISBN number (100 points)

Question number: 201312-2
Question name: ISBN number
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:
Problem Description
  Each officially published book has an ISBN number corresponding to it. The ISBN code includes 9 digits, 1 digit identification code and 3 digits separator. Its prescribed format is "x-xxx-xxxxx-x", where the symbol " -" is the separator (minus sign on the keyboard), and the last digit is the identification code, for example, 0-670-82162-4 is a standard ISBN code. The first digit of the ISBN code represents the publication language of the book, such as 0 for English; the three digits after the first separator "-" represent the publishing house, such as 670 for Viking Press; the five digits after the second separator Represents the serial number of the book at the publisher; the last digit is the identification code.
  The calculation method of the identification code is as follows:
  the first digit is multiplied by 1 and the second digit is multiplied by 2... and so on, using the obtained result mod 11, the remainder is the identification code, if the remainder is 10, then the identification code is Capital letter X. For example, the identification code 4 in the ISBN number 0-670-82162-4 is obtained as follows: for the 9 numbers 067082162, from left to right, multiply by 1, 2, ..., 9 respectively, and then sum up, that is, 0× 1+6×2+…+2×9=158, then take the result 4 of 158 mod 11 as the identification code.
  Write a program to judge whether the identification code in the input ISBN number is correct. If it is correct, only output "Right"; if it is wrong, the output is the correct ISBN number.
input format
  The input is only one line, which is a sequence of characters, representing the ISBN number of a book (ensure that the input meets the format requirements of ISBN numbers).
output format
  Output one line, if the identification code of the input ISBN number is correct, then output "Right", otherwise, according to the specified format, output the correct ISBN number (including the separator "-").
sample input
0-670-82162-4
Sample output
Right
sample input
0-670-82162-0
Sample output
0-670-82162-4

Code:

C++

#include <iostream>
#include <string>
#define MAX_LENGTH 10
using namespace std;


int main(int argc, char** argv) {
    string str;
    int numbers[10]={};
    int flag=0;
    cin>>str;

    //输入 
    for(int i=0,j=1;i<str.length()-2;i++)
    {
        if(str[i]!='-')
        {
            flag+=(j*(str[i]-'0'));
            j++;
        }
    }

    //判断校验位 
    char c;
    c=flag%11;
    if(c==10)
        c='X';
    else
        c=c+'0';

    //输出 
    if(c==(str[str.length()-1]))
        cout<<"Right"<<endl;
    else
    {
        str[str.length()-1]=c;
        cout<<str<<endl;
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325919320&siteId=291194637