package test;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
public class test {
public static void main(String[] args) {
int money = 1500;//紅包金額
int people = 11;//參與人數
if(people >money){
System.out.println("紅包金額必須大于等于參與人數");
}else{
System.out.println("紅包金額:"+(money/100)+"元,參與人數:"+people);
int index = 1;
Map<String, String> map = new HashMap<String, String>();
while(true){
int f1 =0;
if(people == money){
f1 = 1;
}else{
while(true){
f1 =(int)(Math.random()* money);
// System.out.println(f1+"/"+people+"/"+money);
if(f1 > 0 && people <= (money-f1)){
break;
}
}
}
// System.out.println("随機數:"+f1+"/當時的錢:"+money+"/當時的人數:"+people);
if(people > 2){
money = money- f1;
people = people-1;
// System.out.println(money+"/"+people);
map.put((index++)+"", f1+"");
}else if(people == 2){
money = money- f1;
people = people-1;
// System.out.println(money+"/"+people);
map.put((index++)+"", f1+"");
map.put((index++)+"", money+"");
break;
}else if(people == 1){
map.put((index++)+"", money+"");
break;
}
}
int i=0 ;
index =1 ;
for (Entry<String,String> string : map.entrySet()) {
i=i+Integer.parseInt(string.getValue());
System.out.println("目前第"+(index++)+"人,搶到的紅包金額為:"+(Double.valueOf(string.getValue())/100)+"元");
}
System.out.println("總金額:"+(i/100)+"元");
}
}
}