zoukankan      html  css  js  c++  java
  • 针对较大基数的排列组合算法Java实现类(n选m)

    package com.utils;
    
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    
    public class PLZUUtils {
        
        public static BigDecimal computePaiLie(int n, int m) {
            if(m > n || n < 0 || m < 0) {
                throw new IllegalArgumentException("n必须大于m!");
            }
            return computerJC(n).divide(computerJC(n - m), 1, RoundingMode.HALF_UP);
        }
        
        public static BigDecimal computeZuhe(int n, int m) {
            if(m > n || n < 0 || m < 0) {
                throw new IllegalArgumentException("n必须大于m!");
            }
            //=n!/m!(n-m)!
            
            return computerJC(n).divide((computerJC(m).multiply(computerJC(n - m)).setScale(1, RoundingMode.HALF_UP)), 1, RoundingMode.HALF_UP);
        }
        
        
        public static BigDecimal computerJC(int n) {
            if(n < 0) {
                throw new IllegalArgumentException("n不能为负数!");
            } else if(n == 0) {
                return new BigDecimal(1);
            }
            BigDecimal bd = new BigDecimal(1.0);
            for(int i=n; i>=1; i--) {
                bd = bd.multiply(new BigDecimal(i)).setScale(1, RoundingMode.HALF_UP);
            }
            return bd;
        }
        
        public static void main(String[] args) {
            BigDecimal zh = computeZuhe(462, 442);
            System.out.println(zh.doubleValue());
        }
        
    }
  • 相关阅读:
    惊讶
    BLOG休假
    因考试得福
    Shape of My HeartSting !
    一个月的第一天了
    BLOG开张喽~~~
    该走了
    脏话
    EditText的属性
    游戏引擎
  • 原文地址:https://www.cnblogs.com/tq03/p/3803757.html
Copyright © 2011-2022 走看看