@@ -0,0 +1,1222 @@
+package test2;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Random;%0Aimport java.util.TreeMap;%0A%0Apublic class DistributeApple %7B%0A%0A%09public static void main(String args%5B%5D) %7B%0A%09%09String%5B%5D person = %7B %22A%22, %22B%22, %22C%22 %7D;%0A%09%09int%5B%5D weight = %7B 5, 3, 2 %7D; // even if weight is double not int, we can%0A%09%09%09%09%09%09%09%09%09// calculate it from least common multiple.%0A%09%09%09%09%09%09%09%09%09// If double decimal is too long, we can%0A%09%09%09%09%09%09%09%09%09// still estimate a percentage at most as we%0A%09%09%09%09%09%09%09%09%09// can.%0A%09%09int numOfApples = 10;%0A%09%09calc(person, weight, numOfApples);%0A%09%7D%0A%0A%09private static void calc(String%5B%5D person, int%5B%5D weight, int n) %7B%0A%0A%09%09List%3CString%3E weightedPerson = new ArrayList%3C%3E();%0A%0A%09%09for (int i = 0; i %3C weight.length; i++) %7B%0A%09%09%09int w = weight%5Bi%5D;%0A%09%09%09for (int j = 0; j %3C w; j++) %7B%0A%09%09%09%09weightedPerson.add(person%5Bi%5D);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09Map%3CString, Integer%3E result = new TreeMap%3C%3E();%0A%09%09for (int i = 0; i %3C person.length; i++) %7B%0A%09%09%09result.put(person%5Bi%5D, 1); // make sure everyone have at least one apple.%0A%09%09%7D%0A%0A%09%09for (int i = 0; i %3C n - person.length; i++) %7B%0A%09%09%09int x = new Random().nextInt(weightedPerson.size());%0A%09%09%09String s = weightedPerson.get(x);%0A%09%09%09result.put(s, result.get(s) + 1);%0A%09%09%7D%0A%0A%09%09System.out.println(result);%0A%09%7D%0A%7D%0A

Create DistributeApple.java