• JAVA:实现Damm达姆算法(附完整源码)


    JAVA:实现Damm达姆算法

    package com.thealgorithms.others;
    
    import java.util.Objects;
    
    
    public class Damm {
    
        private static final byte[][] DAMM_TABLE = {
            {0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
            {7, 0, 9, 2, 1, 5, 4, 8, 6, 3},
            {4, 2, 0, 6, 8, 7, 1, 3, 5, 9},
            {1, 7, 5, 0, 9, 8, 3, 4, 2, 6},
            {6, 1, 2, 3, 0, 4, 5, 9, 7, 8},
            {3, 6, 7, 4, 2, 0, 9, 5, 8, 1},
            {5, 8, 6, 9, 7, 2, 0, 1, 3, 4},
            {8, 9, 4, 5, 3, 6, 2, 0, 1, 7},
            {9, 4, 3, 8, 6, 1, 7, 2, 0, 5},
            {2, 5, 8, 1, 4, 3, 6, 7, 9, 0}
        };
    
        /**
         * Check input digits by Damm algorithm.
         *
         * @param digits input to check
         * @return true if check was successful, false otherwise
         * @throws IllegalArgumentException if input parameter contains not only
         * digits
         * @throws NullPointerException if input is null
         */
        public static boolean dammCheck(String digits) {
            checkInput(digits);
            int[] numbers = toIntArray(digits);
    
            int checksum = 0;
            for (int number : numbers) {
                checksum = DAMM_TABLE[checksum][number];
            }
    
            return checksum == 0;
        }
    
    
        public static String addDammChecksum(String initialDigits) {
            checkInput(initialDigits);
            int[] numbers = toIntArray(initialDigits);
    
            int checksum = 0;
            for (int number : numbers) {
                checksum = DAMM_TABLE[checksum][number];
            }
    
            return initialDigits + checksum;
        }
    
        public static void main(String[] args) {
            System.out.println("Damm algorithm usage examples:");
            var validInput = "5724";
            var invalidInput = "5824";
            checkAndPrint(validInput);
            checkAndPrint(invalidInput);
    
            System.out.println("\nCheck digit generation example:");
            var input = "572";
            generateAndPrint(input);
        }
    
        private static void checkAndPrint(String input) {
            String validationResult = Damm.dammCheck(input)
                    ? "valid"
                    : "not valid";
            System.out.println("Input '" + input + "' is " + validationResult);
        }
    
        private static void generateAndPrint(String input) {
            String result = addDammChecksum(input);
            System.out.println("Generate and add checksum to initial value '" + input + "'. Result: '" + result + "'");
        }
    
        private static void checkInput(String input) {
            Objects.requireNonNull(input);
            if (!input.matches("\\d+")) {
                throw new IllegalArgumentException("Input '" + input + "' contains not only digits");
            }
        }
    
        private static int[] toIntArray(String string) {
            return string.chars()
                    .map(i -> Character.digit(i, 10))
                    .toArray();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
  • 相关阅读:
    痞子衡嵌入式:从JLink V7.62开始优化了手动增加新MCU型号支持方法
    广和通&高通物联网技术开放日成功举办
    【数据结构】栈的模拟和使用理解
    Java 21 新特性:虚拟线程(Virtual Threads)
    2022/8/19 树莓派烧录与连接
    涟源举办系列文旅活动推动湖南文旅产业发展!
    保姆级Ubuntu从下载到正常使用,遇到各种问题解决方案。
    每日一博 - Code如何被发布到生产环境
    人工神经网络的拓扑结构,三层神经网络结构图
    【机器学习】梯度下降预测波士顿房价
  • 原文地址:https://blog.csdn.net/it_xiangqiang/article/details/126292055