tianyun 3 年之前
父节点
当前提交
314b1fea68
共有 2 个文件被更改,包括 25 次插入20 次删除
  1. 23 0
      test1/src/main/java/RedPackets.java
  2. 2 20
      test1/src/main/java/tmp.java

+ 23 - 0
test1/src/main/java/RedPackets.java

@@ -0,0 +1,23 @@
+import java.util.concurrent.TimeUnit;
+
+public class RedPackets {
+    public double[] redPackets(Double all, int num) {
+        double[] res = new double[num];
+        Double left = 0D;
+        for (int i = 0; i < num; i++) {
+            if (i == num - 1) {
+                res[i] = all - left;
+            } else {
+                double res_i = Math.random() * (2 * (all - left) / (num - i)) + 0.01;
+                res_i = (double) Math.round(res_i * 100) / 100;
+                res[i] = res_i;
+                left += res_i;
+            }
+        }
+        return res;
+    }
+
+    public static void main(String[] args) throws InterruptedException {
+        System.out.println(new RedPackets().redPackets(100d, 10));
+    }
+}

+ 2 - 20
test1/src/main/java/tmp.java

@@ -1,27 +1,9 @@
 import java.nio.file.Files;
-import java.util.ArrayList;
-import java.util.Base64;
-import java.util.BitSet;
-import java.util.Stack;
+import java.util.*;
 import java.util.concurrent.*;
 
 public class tmp {
     public static void main(String[] args) throws InterruptedException {
-        int[] ints = {2, 1, 2, 4, 3};
-        nextGreaterElement(ints);
-    }
-
-    public static int[] nextGreaterElement(int[] nums) {
-        int n = nums.length;
-        int[] res = new int[n];
-        Stack<Integer> stack = new Stack<>();
-        for (int i = n - 1; i >= 0; i--) {
-            while (!stack.isEmpty() && stack.peek() <= nums[i]) {
-                stack.pop();
-            }
-            res[i] = stack.isEmpty() ? -1 : stack.peek();
-            stack.push(nums[i]);
-        }
-        return res;
+        TimeUnit.SECONDS.sleep(3);
     }
 }