• CalISBN.java


    /******************************************************************************
     *  Compilation:  javac CalISBN.java
     *  Execution:    java CalISBN n
     *  
     *  Determines the check digit of an ISBN-10 number given the first 9 digits.
     *
     *  An ISBN-10 number is valid if it consists of 10 digits and
     *  d_1 + 2*d_2 + 3*d_3 + ... + 10*d_10 is a multiple of 11.
     *  For example, 0-201-31452-5 is valid since
     *  1*5 + 2*2 + 3*5 + 4*4 + 5*1 + 6*3 + 7*1 + 8*0 + 9*2 + 10*0 = 88
     *  and 88 is a multiple of 11.
     *
     *  % java CalISBN 013407642
     *  The full ISBN number is 0134076427
     *
     *  % java CalISBN 067233784
     *  The full ISBN number is 0672337843
     *
     *  % java CalISBN 032157351
     *  The full ISBN number is 032157351X
     *
     ******************************************************************************/
    
    public class CalISBN {
    
        public static void main(String[] args) { 
    
            // read in one command-line argument
            int n = Integer.parseInt(args[0]);
    
            // compute the weighted sum of the digits, from right to left
            int sum = 0;
            for (int i = 2; i <= 10; i++) {
                int digit = n % 10;                // rightmost digit
                sum = sum + i * digit;
                n = n / 10;
            }
         
            // print out check digit, use X for 10
            System.out.print("The full ISBN number is " + args[0]);
            if      (sum % 11 == 1) System.out.println("X");
            else if (sum % 11 == 0) System.out.println("0");
            else                    System.out.println(11 - (sum % 11));
        }
    }
  • 相关阅读:
    Java集合
    插入排序
    修改button的可点击区域
    这就是工作
    Cocos2dx使用TextField实现输入框
    SVN解决本地版本控制与服务器版本冲突问题
    ParallaxNode视差节点实现远景近景的不同层次移动
    人生最重要的三个领域——健康、财富和爱
    什么是开发框架-- (转载)
    C++函数模版的简单使用
  • 原文地址:https://www.cnblogs.com/bayes/p/9664429.html
Copyright © 2020-2023  润新知