Love丶FFC's Blog

CCF-CSP_20131202:ISBN号码

2019-10-30 09:57:33
阅读:493   •   评论:3
标签:,

问题描述

每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如0代表英语;第一个分隔符“-”之后的三位数字代表出版社,例如670代表维京出版社;第二个分隔之后的五位数字代表该书在出版社的编号;最后一位为识别码。

识别码的计算方法如下:

首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,…,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。

编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出是正确的ISBN号码。

输入格式

输入只有一行,是一个字符序列,表示一本书的ISBN号码(保证输入符合ISBN号码的格式要求)。

输出格式

输出一行,假如输入的ISBN号码的识别码正确,那么输出“Right”,否则,按照规定的格式,输出正确的ISBN号码(包括分隔符“-”)。

样例输入

0-670-82162-4

样例输出

Right

样例输入

0-670-82162-0

样例输出

0-670-82162-4 

编程语言:Java

解题思想:

1.求出正确识别码

2.判断实际识别码与正确识别码

时间复杂度:O(1)

IDE代码如下:

  1. package CCF_CSP;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class _20131202_100point {
  6.  
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. String ISBN = scanner.next(); //输入ISBN号
  10. String rightISBN = ISBN.substring(0, 12); //存放正确的ISBN号的前11位
  11.  
  12. int recognizecode = (Integer.parseInt(ISBN.charAt(0)+"")*1+Integer.parseInt(ISBN.charAt(2)+"")*2
  13. +Integer.parseInt(ISBN.charAt(3)+"")*3+Integer.parseInt(ISBN.charAt(4)+"")*4+Integer.parseInt(ISBN.charAt(6)+"")*5+
  14. Integer.parseInt(ISBN.charAt(7)+"")*6+Integer.parseInt(ISBN.charAt(8)+"")*7+Integer.parseInt(ISBN.charAt(9)+"")*8+
  15. Integer.parseInt(ISBN.charAt(10)+"")*9) % 11; //首先求出正确识别码
  16.  
  17. if(recognizecode == 10) //如果识别码为10,则需要用X替代
  18. {
  19. if((ISBN.charAt(12)+"").equals("X")==true) //识别码正确
  20. {
  21. System.out.println("Right");
  22. }
  23. else //识别码错误
  24. {
  25. rightISBN += "X"; //追加正确识别码
  26. System.out.println(rightISBN);
  27. }
  28. }
  29. else
  30. {
  31. if((ISBN.charAt(12)+"").equals(recognizecode+"")==true) //识别码正确
  32. {
  33. System.out.println("Right");
  34. }
  35. else //识别码错误
  36. {
  37. rightISBN += recognizecode; //追加正确识别码
  38. System.out.println(rightISBN);
  39. }
  40. }
  41. }
  42.  
  43. }
  44.  

评论板

共有 3 条评论

  1. FoorryNox

    droperidol increases and arformoterol decreases sedation buy cialis generic online Advantages vs

  2. Preliaf

    A number of pharmaceuticals that are not prescribed as hormonally active drugs have been shown to cause rodent mammary tumors Rudel et al viagra boys warsaw

  3. oeSIvKeBl

    In addition, a case of sclerosing glomerulonephritis has been reported in a postmenopausal woman with breast cancer after ASZ adjunct therapy initiation 49, pointing to the importance of monitoring renal outcomes in future clinical studies for aromatase inhibitors occasion levitra Asymptomatic increases in aspartate AST SGOT and or alanine ALT SGPT aminotransferase levels greater than 3 times the upper limit of normal of the laboratory reference range have been reported in up to 8

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:502585