编程语言
首页 > 编程语言> > 场景算法

场景算法

作者:互联网

随机分红包

import java.text.*;

import java.math.*;
import java.util.*;

public class Main {
    // 随机红包 每个人都会分到钱至少0.01.最佳手气不超过总金额的80%.
    static void hb(double total, int num) {
        double min = 0.01;
        double one = Math.random() + total * (1 - 0.8);
        BigDecimal one_bd = new BigDecimal(one);
        one = one_bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        total = total - one;
        BigDecimal total_bd = new BigDecimal(total);
        total = total_bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println("第1个红包:" + one + ",余额为:" + total + "元");
        for (int i = 2; i < num; i++) {
            double free_total = (total - (num - i) * min) / (num - i);

            double money = Math.random() * (free_total - min) + min;// [min,free_total)

            BigDecimal money_bd = new BigDecimal(money);
            money = money_bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            total = total - money;
            total_bd = new BigDecimal(total);
            total = total_bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            System.out.println("第" + i + "个红包:" + money + ",余额为:" + total + "元");
        }
        System.out.println("第" + num + "个红包:" + total + ",余额为:0元");
    }

    public static void main(String[] args) {

        hb(100, 10);// 金额,个数

    }

}

 

标签:bd,场景,BigDecimal,min,money,算法,num,total
来源: https://www.cnblogs.com/tingtin/p/16072006.html