Title Source: http://jmunetds.openjudge.cn/crace1/8/
8:ISBN Number
-----------------------------------------------------
Total time limit:
1000ms
Memory Limit:
500000kB
Description
Title Description
Each published book has an ISBN number corresponding to it, the ISBN code includes 9-digit, 1-bit identification number and 3-bit delimiter, the specified format such as "X-xxx-xxxxx-x", where the symbol "-" is the delimiter (the minus sign on the keyboard), the last one is the identification code, For example, 0-670-82162-4 is a standard ISBN code. The first digit of the ISBN code represents the publishing language of the book, for example 0 for English; the three digits after the first delimiter "-" represent the publishing house, for example 670 for the Viking publishing house; the second delimiter after the five digits represents the number of the book in the publishing house; the last one is the identification code.
The identification code is calculated as follows:
Multiply the first number by 1 plus the second digit by 2 ... And so on, with the resulting mod 11, the remainder is the identification code, if the remainder is 10, then the identification code is an uppercase X. For example, the ISBN number 0-670-82162-4 in the identification code 4 is obtained: 067082162 of the 9 numbers, from left to right, respectively multiplied by 1,2,...,9, and then summed, that is, 0x1+6x2+......+2x9=158, and then take 158 mod 11 results of 4 as identification code.
Your task is to write the program to determine if the identification code in the input ISBN is correct, and if correct, only "right" is output, and if wrong, the ISBN number you think is the correct one.
input
Input format:
The input file isbn.in has only one line, which is a sequence of characters representing the ISBN number of a book (guaranteed to enter the format required for the ISBN number).
Output
Output format:
Output file Isbn.out A total line, if the input ISBN number is correct, then output "right", otherwise, in accordance with the specified format, output the correct ISBN number (including the delimiter "-").
Sample Input
Input Sample # #:
0-670-82162-4
Input Sample #:
0-670-82162-0
Sample Output
Sample # # of output:
Right
Output Example #:
0-670-82162-4
-----------------------------------------------------
Thinking of solving problems
Preventing an array from overstepping the bounds of an array can produce unexpected results:
For example, array code[] crossed out the first two bits of the string isbn[]
-----------------------------------------------------
Code
#include <stdio.h>
const int index[9] = {0,2,3,4,6,7,8,9,10}; Remove the "-" Index Table
int main ()
{
char isbn[14] = {}; ISBN code: 13+1 (cutoff character)
int code[9] = {}; Digit bit
int check = 0; Check bit
int result = 0; Checksum calculation
scanf ("%s", ISBN);
int i=0;
for (i=0;i<9;i++)
{
code[i] = isbn[index[i]]-48; int
result + = (i+1) *code[i];
}
check = isbn[12]; Char
result = result%11;
if (result==10)
{
result = ' X ';
}
else
{
result + =; Char
}
if (Result==check)
{
printf ("right");
}
else
{
isbn[12] = result;
printf ("%s", ISBN);
}
return 0;
}