test

public static List<Integer> divideRedPackage(Integer totalAmount, Integer totalPeopleNum){
    List<Integer> amountList = new ArrayList<Integer>();
    Integer restAmount = totalAmount;
    Integer restPeopleNum = totalPeopleNum;
    Random random = new Random();
    for(int i = 0; i < totalPeopleNum-1; i++){
        int amount = random.nextInt(restAmount / restPeopleNum * 2 - 1) + 1;
        restAmount -= amount;
        restPeopleNum --;
        amountList.add(amount);
    }
    amountList.add(restAmount);
    return amountList;
}

public static void main(String[] args) {
    List<Integer> amountList = divideRedPackage(5000, 30);
    for(Integer amount : amountList){
        System.out.println("抢到的金额:" + new BigDecimal(amount).divide(new BigDecimal(100)));
    }
}
    

个人资料
crazybean
等级:8
文章:61篇
访问:15.7w
排名: 5
上一篇: 唯品会2018校招机器学习、算法笔试题(加分题)
下一篇:唯品会2018校招数据挖掘、机器学习笔试题(A卷)
标签: amountlist、integer、restamount、amount、totalpeoplenum、面试题
隐藏