哥们想问的是这个问起吧:12个人排成一圈,从1号报数,凡是数到5的人就走出队列(出局),然后继续报数,试问最后一人出局的是谁?- import java.util.ArrayList;
- import java.util.List;
- public class Joseph{
- private int interval;
- private List<String> knights;
- public Joseph(int step,String[] names){
- this.interval = step;
- this.knights = new ArrayList<String>();
- for (String name:names)
- knights.add(name);
- }
- public void start() {
- int position = 0;
- while (!knights.isEmpty()) {
- position = (position + interval - 1) % knights.size();
- String name = knights.remove(position);
- System.out.println(name + " was removed");
- }
- }
- public static void main(String[] args) {
- String[] names = new String[]{"L","H","S","J","R",
- "G","Y","E","D","T",
- "E"};
- Joseph j = new Joseph(5,names);
- j.start();
- }
- }
复制代码 |