CCF 201312-2 ISBN number

Source: Internet
Author: User
Tags time limit

Question number: 201312-2
Question Name: ISBN number
Time limit: 1.0s
Memory Limit: 256.0MB
Problem Description: Problem 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 a delimiter (the keyboard minus), 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 separated five digits represent 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.
Write the program to determine whether the identification code in the input ISBN is correct, if correct, only the "right", if the error, the output is the correct ISBN number. Input format input only one line, is a sequence of characters, representing the ISBN number of a book (guaranteed to enter the format required for the ISBN number). Output format output 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 0-670-82162-4 sample Output Right example input 0-670-82162-0 sample Output 0-670-82162-4

Idea: Since the title said to ensure that the input conforms to the ISBN number format requirements, so save a lot of things, in fact, we have to do two things, the first thing is to find sum, the second is to determine whether the last character is in line with the title, right, otherwise the output is correct, it seems to be a very basic problem ~

Code:

#include <iostream>
using namespace std;
int main () {
	char str[13];
	while (CIN>>STR) {
		int temp=1;
		int sum=0;
		for (int i=0;i<11;i++)
			if (str[i]!= '-') {	
				sum+= (str[i]-' 0 ') *temp;
				temp++;
			}
		sum%=11;
		char c = sum==10? ' X ': sum+ ' 0 ';	Store the pair's identification code  
		if (str[12]==c)
			cout<< "right" << "\ n";
		else{
			str[12]=c;
			cout<<str<< "\ n";
		}
	}
	return 0;





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.