发放随机优惠券金额算法

/** * 依据红包数和红包总额 * @param number 红包总数 * @param total 红包总额 * @param min 最小红包金额 * @return 红包金额列表 */ public static List<Float> createHongbaoAmount(int number, float total, float min ,float maxHongbao){ float money;//红包金额 float moneyFloat;//红包金额小数点 double max; int i = 1; List<Float> math = new ArrayList<Float>(); DecimalFormat df = new DecimalFormat("##0.00"); while (i < number) { //保证即使一个红包是最大的了,后面剩下的红包,每个红包也不会小于最小值 max = total - min * (number - i); int k = (int)(number - i) / 2; //保证最后两个人拿的红包不超出剩余红包 if (number - i <= 2) { k = number - i; } //最大的红包限定的平均线上下 max = max / k; //保证每个红包大于最小值,又不会大于最大值 moneyFloat = (float) (min * 100 + Math.random() * (max * 100 - min * 100 + 1)); moneyFloat = (float)moneyFloat / 100; //保留两位小数 moneyFloat = Float.parseFloat(df.format(moneyFloat)); if(moneyFloat<min){ moneyFloat = min; } if(moneyFloat>maxHongbao){ moneyFloat = maxHongbao; } total=(total - moneyFloat); //total = total/100; money = Float.parseFloat(String.valueOf(moneyFloat).substring(0,String.valueOf(moneyFloat).length()-2)); math.add(moneyFloat); //log.info("the " + i + " get " + money + ";left" + total); i++; //最后一个人拿走剩下的红包 if (i == number) { money = Float.parseFloat(String.valueOf(total).substring(0,String.valueOf(total).length()-2)); math.add(Float.parseFloat(df.format(money))); //log.info("the " + i + " get " + total + ";left "+total); } } for(int j=0;j<math.size();j++){ Float maxMath = Collections.max(math); if(maxMath>maxHongbao){ int indexMax = math.indexOf(Collections.max(math)); int indexMin = math.indexOf(Collections.min(math)); math.set(indexMax, maxHongbao); math.set(indexMin, Collections.min(math) +maxMath-maxHongbao); } } float sumMath = 0; for(int j=0;j<math.size();j++){ sumMath += math.get(j); } System.out.println("sumMath:"+sumMath); //取数组中最大的一个值的索引 System.out.println("the " + (math.indexOf(Collections.max(math)) + 1) + " is max"); return math; }

原文链接:https://blog.csdn.net/qq_37252429/article/details/76287557?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165277499316781432920009%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165277499316781432920009&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-13-76287557-null-null.nonecase&utm_term=%E4%BC%98%E6%83%A0

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
文明发言,共建和谐米科社区
提交
头像

昵称

取消
昵称表情图片

    暂无评论内容