Public Static voidMain (string[] args) {//number of red envelopes intNumber = 10; //Total Red Envelopes floatTotal = 100; floatMoney ; //Minimum Red envelopes DoubleMin = 1; DoubleMax; inti = 1; List Math=NewArrayList (); DecimalFormat DF=NewDecimalFormat ("###.##"); while(I <Number ) { //ensure that even if a red envelope is the largest, the rest of the red envelopes, each red envelope will not be less than the minimum valuemax = Total-min * (number-i); intK = (int) (Number-i)/2; //to ensure that the last two people take the red envelopes not exceeding the remaining red envelopes if(Number-i <= 2) {k= number-i; } //The largest red envelope limits the average line up and downmax = max/K; //guarantee that each red envelope is greater than the minimum value and not greater than the maximum valueMoney = (int) (min * + math.random () * (max * 100-min * 100 + 1)); Money= (float) money/100; //keep two decimal placesMoney =float.parsefloat (Df.format (money)); Total=(int) (total*100-money*100); Total= total/100; Math.add (Money); System.out.println ("First" + i + "personal get" + Money + "left" +Total ); I++; //the last one took the rest of the red envelopes. if(i = =Number ) {Math.add (total); System.out.println ("First" + i + "personal get" + Total + "0 remaining"); } }//the index of the largest value in the arraySystem.out.println ("Math.indexof" + (Collections.max (math) + 1) + "personal best luck"); }
Others Rob Red Envelopes, we study the red envelope algorithm