CCF CSP Brush Question Record 2-201312 ISBN number

 

Question number:

201312-2
Question name: ISBN number
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  Every officially published book has an ISBN number corresponding to it. The ISBN code includes 9 digits, 1 identification code and 3 separators. The prescribed format is "x-xxx-xxxxx-x", where the symbol " -" is the separator (the minus sign on the keyboard), 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 publication language of the book, for example, 0 represents English; the three digits after the first separator "-" represents the publishing house, for example, 670 represents Virgin Press; the five digits after the second separator Represents the serial number of the book in 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 plus the next digit is multiplied by 2...and so on, using the result obtained mod 11, the remainder obtained 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 like this: For the 9 numbers 067082162, from left to right, multiply by 1, 2, ..., 9, and then sum them up, 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

  There is only one line of input, which is a sequence of characters, representing the ISBN number of a book (ensure that the input meets the format requirements of the ISBN number).

Output format

  Output one line. If the identification code of the input ISBN number is correct, output "Right", otherwise, output the correct ISBN number (including the separator "-") according to the prescribed format.

Sample input

0-670-82162-4

Sample output

Right

Sample input

0-670-82162-0

Sample output

0-670-82162-4

 

 

import java.util.Scanner;

public class ISBN号码201312_2 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String s=sc.nextLine();
		String[] isbn=s.split("");
		int a1=Integer.parseInt(isbn[0]);
		int a2=Integer.parseInt(isbn[2]);
		int a3=Integer.parseInt(isbn[3]);
		int a4=Integer.parseInt(isbn[4]);
		int a5=Integer.parseInt(isbn[6]);
		int a6=Integer.parseInt(isbn[7]);
		int a7=Integer.parseInt(isbn[8]);
		int a8=Integer.parseInt(isbn[9]);
		int a9=Integer.parseInt(isbn[10]);
		
		int resmod=(a1*1+a2*2+a3*3+a4*4+a5*5+a6*6+a7*7+a8*8+a9*9)%11;
			/*
			 * 总结:
			 * 首先考虑正确的情况,剩下的就是不正确的情况了
			 * 还有字符串相等不能用==,要用equals
			 * 其实用不着对最后一个字串再parseInt,输出的本来就是字符串,所以不要做的繁琐
			 * */

		if((resmod+"").equals(isbn[12])){
		
			System.out.println("Right");
			
		}else if(resmod==10&&(isbn[12].equals("X"))){
			System.out.println("Right");
			
		}else if(resmod==10){
			
			System.out.println(isbn[0]+"-"+isbn[2]+isbn[3]+isbn[4]+"-"+isbn[6]+isbn[7]+isbn[8]+isbn[9]+isbn[10]+"-"+"X");
		}else 
			System.out.println(isbn[0]+"-"+isbn[2]+isbn[3]+isbn[4]+"-"+isbn[6]+isbn[7]+isbn[8]+isbn[9]+isbn[10]+"-"+(resmod+""));
		}
}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108245022