Home > Back-end >  WeChat small program code optimization for bosses to specify maze, modify the original code
WeChat small program code optimization for bosses to specify maze, modify the original code

Time:10-11

 
Package red;

import java.util.Random;
import java.util.Scanner;

Class Program
{
Public static void main (String [] args)
{
System. The out. Println (" -- -- -- -- -- - analog WeChat grab a red envelope -- -- -- -- -- - \ n ");
Scanner sc=new Scanner(System.in);
System. The out. Println (" please enter the total amount ");
Double Total=sc. NextDouble ();
System. The out. Println (" please enter the number of red envelopes ");
Int Red=sc. NextInt ();
Double min=0.01;//set the minimum amount
Random random=new Random();//create a random number
If (Total/Red==0.01)
{
For (int I=1; i <=Red; I++)
{
Double money=min;//set each a red envelope is 0.01;
System. The out. Println (" first "+ I +" is a red envelope "+ String. The format (. % 2 f, money) +" RMB ");
}
} else if (Total/Red & lt; 0.01)//when the amount divided by the number of less than 0.01
{
System. The out. Println (" everyone to go at least 0.01 ");
} the else
{
For (int I=1; i <=Red; I++)
{
Double Max min=Total - (Red - I) *;//a red envelope of the maximum=the remaining amount minus (total minus and send filled) * minimum amount
Double bound=Max - min;//every random random value range between the minimum and maximum (min<=boundDouble safe=(double) random. NextInt ((int) (100) bound *)/100;//integer value into double
Double money=safe + min;//value may be safe out of 0, each packet to some 0.01 yuan
Total=Total - money;//here is the next cycle can be allocated amount equal to the last time on this occasion, the distributable amount minus the amount of money has been allocated
System. The out. Println (" first "+ I +" is a red envelope: "+ String. The format (. % 2 f, money) +" RMB ");
}
}
}
}

From behind the distribution of the normal amount of red envelopes, the maximum can lead to the first packet is the maximum share out bonus, what is the place to optimize let him like WeChat applet to send a red envelope, there is big of small, medium red envelopes,

CodePudding user response:

 
//...
Double left=Total;
Double avg=Total/Red;
For (int I=1; i Int j=Red - I;
Double Max=left - j * min;//a red envelope of the maximum=the remaining amount minus (total minus and send filled) * minimum amount

//double bound=Max - min;//every random random value range between the minimum and maximum (min<=bound//System. Out. Println (Max).

Double safe=random. NextDouble () * Max;//integer value into double
While (j & gt; 0 {
If (safe & gt; Avg) {
Safe=random. NextDouble () * Max;
} else {
break;
}
j--;
}
//System. Out. Println (safe).
If (safe & lt;=0.01)
Safe=0.01 D;
//double money=safe + min;//value may be safe out of 0, each packet to some 0.01 yuan
Left=left - safe;//here is the next cycle can be allocated amount equal to the last time on this occasion, the distributable amount minus the amount of money has been allocated
System. The out. Println (" first "+ I +" is a red envelope: \ t "+ String. The format (. % 2 f, safe) +" \ t ");
}
System. The out. Println (" first "+ Red +" is a Red envelope: \ t "+ String. The format (. % 2 f, left) +" \ t ");
//...
  • Related