Others Rob Red Envelopes, we study the red envelope algorithm

Source: Internet
Author: User

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.