Java 紅包演算法、限定最小和最大臨界值

來源:互聯網
上載者:User
package com.cdjj.test;


public class Demo {
public static void main(String[] args) {
randomPackage();
}
public static void randomPackage() {
double total_money=2;
int total_people=20;
double min_money=5; // 每個人最少能收到0.05元、
double max_money=20; // 每個人最少能收到0.05元、
double lingjiezhi=(total_money/total_people)*100;
double allresult=0;
for (int i = 0; i <total_people; i++) {
//保護值
double procte=(total_people-i-1)*min_money/100;;
//可支配金額
double zpje=total_money-procte;
if(zpje*100<max_money){
max_money=zpje*100;
}
double result=restRound(min_money, max_money, i, zpje, total_people-1);
total_money=total_money-result;
allresult+=result;
System.out.format("紅包  %.2f,餘額  %.2f \n",result,total_money);
}
System.out.format("總金額%.2f",allresult);
}
public static double restRound(double min,double max,int i,double money,int count){
double redpac=0;
if(i==19){
redpac=money;
}else{
redpac=(Math.random()*(max-min)+min)/100;
}
return redpac;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.