CCF-CSP 201312-2 ISBN number

Problem description
  Each officially published book has an ISBN number corresponding to it. The ISBN code consists of 9 digits, 1 identification code and 3 separators. 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 indicates the language in which the book is published, for example, 0 represents English; the three digits after the first separator "-" represent the publishing house, for example, 670 represents the Viking Publishing House; the five digits after the second separator Represents the number of the book in the publishing house; the last digit is the identification code.
  The calculation method of the identification code is as follows: the
  first digit is multiplied by 1 plus the second digit is multiplied by 2 ... and so on, using the result mod 11 obtained, the resulting remainder is the identification code, if the remainder is 10, 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 digits 067082162, from left to right, they are multiplied by 1, 2, ..., 9, and then summed, that is, 0 × 1 + 6 × 2 + …… + 2 × 9 = 158, and 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 "Right" is output; 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 that represents the ISBN number of a book (to ensure that the input meets the format requirements of the ISBN number).
Output format
  Output one line, if the input ISBN number identification code 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

Experience summary: It
is more convenient to use string processing, if it is more troublesome to deal with integers.
Remember: the string can replace a certain character separately, for example: s [i] = '1'; (i is any value in the length range of the string )

C ++ code:

#include<bits/stdc++.h>
using namespace std;
int main() {
	int sum = 0,cnt = 0;
	string s;
	cin>>s;
	for(decltype(s.size()) i=0; i<s.size()-2; i++) {
		if(s[i]!='-') {
			cnt++;
			sum += (s[i]-'0')*cnt;
		}
	}
	sum %= 11;
	char check;
	if(10 == sum) check = 'X';
	else check = sum+'0';
	if(check == s[s.size()-1]) cout<<"Right";
	else {
		//此处可替换成s[s.size()-1] = check;
		for(decltype(s.size()) i=0; i<s.size()-1; i++) {
			cout<<s[i];
		}
		cout<<check;
	}
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/100523201