import java.util.ArrayList;
import java.util.List;
/**
* 一架飞机快要掉了,上面有 20 个人,但是只有 3 包降落伞。于是 20 个人凑成一圈,每数到第 7 个,就将他踢下去。
* 最后三包降落伞将落在哪几个人手里?
*/
public class Poll{
public static void main(String[] args) {
int peoples = 20;
int count = 7;
int parachutes = 3;
String a;
Group g = new Group(peoples);
while(g.size() > parachutes) {
g.kick(count);
}
a = g.getRemains();
System.out.println("剩下的人:" + a );
}
private static class Group {
private List<Integer> peoples;
private int lastIndex = 0;
public Group(int size) {
this.peoples = new ArrayList<Integer>(size);
for (int i = 1; i <= size; i++) {
this.peoples.add(new Integer(i));
}
}
public String getRemains() {
return this.peoples.toString();
}
public void kick(int count) {
int index = (this.lastIndex + count) % size();
this.peoples.remove(index);
this.lastIndex = index;
}
public int size() {
return this.peoples.size();
}
}
}
评论1
最新资源