Flask接口签名sign原理与实例代码浅析
245
2022-10-30
JAVA实现红包分发的示例代码
大体思路
代码
import java.util.Arrays;
import java.util.Random;
import java.math.*;
import java.util.Scanner;
public class Main {
public static long now_time;
public static long seed;
public static int[] get_red_packets(int money, int num) {
Random random = new Random(seed);
seed = random.nextLong();
int[] res = new int[num];
double[] temp=new double[num];
double sum = 0;
int sum2 = 0;
for (int i = 0; i < num; i++) {
temp[i] = random.nextDouble();
sum += temp[i];
}
for (int i = 0; i < num; i++) {
res[i] = 1+ (int)(temp[i] / sum * (money-num));
sum2 += res[i]-1;
}
res[random.nextInt(num)]+=money-sum2-num;
return res;
}
public static void show(int[] red_packet){
System.out.println("红包 : " + Arrays.toString(red_packet));
}
public static void main(String[] args) {
int num, money;
Schttp://anner scanner = new Scanner(System.in);
now_time = System.currentTimeMillis();
Random init_random = neaEPHSw Random(now_time);
seed = init_random.nextLong();
System.out.println("请输入要分发的红包数量:");
num = scanner.nextInt();
System.out.println("请输入要分发的红包总金额(分):");
money = scanner.nextInt();
int a[] = get_red_packets(money,num);
show(a);
}
http://}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~