日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java黑皮书课后题第5章:*5.47(商业:检测ISBN-13)ISBN-13是标识书籍的新标准。它使用13位数字d1d2d3~d12d13,d13是校验和。如果校验和为10,则替换为0。求所有数字

發(fā)布時間:2024/7/23 java 34 豆豆

5.47(商業(yè):檢測ISBN-13)ISBN-13時標識書籍的新標準。它使用13位數(shù)字d1d2d3~d12d13,d13是校驗和。讀入前12位輸出全部位數(shù)

  • 題目
    • 題目概述
    • 運行示例
    • 破題
  • 代碼

題目

題目概述

5.47(商業(yè):檢測ISBN-13)ISBN-13時標識書籍的新標準。它使用13位數(shù)字d1d2d3d4d5d6d7d8d9d10d11d12d13,最后一位數(shù)字d13是校驗和,是使用下面的公式從其他數(shù)字中計算出來的:
10 - (d1 + 3 * d2 + d3 + 3 * d4 + d5 + 3 * d6 + d7 + 3 * d8 + d9 + 3 * d10 + d11 + 3 * d12) % 12
如果校驗和為10,則替換為0。程序應(yīng)該將輸入作為一個字符串讀入。

運行示例

Enter the first 12 digits of an ISBN-13 as a string: 978013213080 The ISBN-13 number is 9780132130806 Enter the first 12 digits of an ISBN-13 as a string: 978013213079 The ISBN-13 number is 9780132130790 Enter the first 12 digits of an ISBN-13 as a string: 97801320 97801320 is an invalid input

破題

  • 用string類型讀入前12位數(shù)字
  • 轉(zhuǎn)化為int類型
  • 取出每位數(shù)字
  • 計算d13,如果是10則替換為0
  • 輸出結(jié)果
  • 代碼

    import java.util.Scanner;public class Test5_47 {public static void main(String[] args) {// 1. 用string類型讀入前12位數(shù)字Scanner input = new Scanner(System.in);System.out.print("Enter the first 12 digits of an ISBN-13 as a string: ");String str = input.nextLine();// 判斷長度是否符合要求int length = str.length();if (length < 9 ) {System.out.println(str + " is an invalid input");System.exit(1);}// 2. 轉(zhuǎn)化為int類型long user_input = Long.parseLong(str);// 3. 取出每位數(shù)字int temp1 = 0, temp2 = 0, result = 0;for ( long i = 10000000000L ; i >= 1 ; i /= 100){temp1 = (int) (user_input / (i * 10));temp2 = (int) (user_input % (i * 10) / i);result += temp1 + 3 * temp2;user_input %= i;}// 4. 計算d13,如果是10則替換為0result = 10 - result % 10;if (result == 10)result = 0;// 5. 輸出結(jié)果System.out.print("The ISBN-13 number is " + str + result);} }

    總結(jié)

    以上是生活随笔為你收集整理的Java黑皮书课后题第5章:*5.47(商业:检测ISBN-13)ISBN-13是标识书籍的新标准。它使用13位数字d1d2d3~d12d13,d13是校验和。如果校验和为10,则替换为0。求所有数字的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。