帮忙做一个关于无序列表的问题
一个旅游团被海盗抓住了。海盗决定在收到赎金前,每过半个小时杀一个人质。将所有的人质排成一个圈,每隔三个人质杀一个人质。请问,如果你在这个旅游团中,如何才能保证自己坚持到最后。帮忙写出具体的代码,十分感谢了!!!!!!!!!!!
import java.util.*; public class testhaidao { public static void main(String args[]) { System.out.println("请输入旅游团的人数"); int num,i,k=0; Scanner keyBoard = new Scanner(System.in); num=keyBoard.nextInt(); boolean lvyoutuan[]=new boolean[num]; for(i=0;i<num;i++) lvyoutuan[i]=true; for(i=1;i<num;i++) { System.out.println("第"+(k+1)+"个人被杀死了"); lvyoutuan[k]=false; do { k++; k=k%num; }while(!lvyoutuan[k]); do { k++; k=k%num; }while(!lvyoutuan[k]); do { k++; k=k%num; }while(!lvyoutuan[k]); do { k++; k=k%num; }while(!lvyoutuan[k]); } System.out.println("坚持到最后的是旅游团的第"+(k+1)+"个人"); } }
import java.util.*; public class testhaidao { static int m = 0;//定义下朋友的号码 public static void main(String args[]) { ArrayList<Integer> a = new ArrayList<Integer>(); for (int i = 1; i <= 10; i++) {//10为你要输入的旅游的人数, a.add(i); } while (a.size() >1) { for (int i = 0; i < a.size(); i++) { m++; if (m % 3 == 0) {//当为3的时候就删除 m = 0;//删除的这个小朋友看作是0 a.remove(i);//执行删除 System.out.println((i+1)+"死了"); i--;//删除一个之后将这个小朋友的号码去掉 } } } System.out.println("你应该站在旅游团的: " + a.get(0)+"位"); } }
import java.util.ArrayList; import java.util.Scanner; public class B_H_J { private ArrayList<Person> persons = new ArrayList<Person>(); public B_H_J() { } private void init(int n) { persons.clear(); for (int i = 1; i <= n; i++) { persons.add(new Person(i)); } } private boolean has7(int num) { while (num > 0) { if (num % 10 == 7) return true; else num = num / 10; } return false; } public void execute(int n) { try { if (n < 10) throw new Exception(); } catch (Exception e) { e.printStackTrace(); } init(n); boolean direct = true;// 正序为true,反序为false; int i = 1; int current = 0; while (persons.size() > 1) { current = (current + persons.size()) % persons.size();//将current转换为合法的索引值 System.out.println("person " + persons.get(current).getId() + "数数,数为:" + i); if (i % 7 == 0 || has7(i)) { System.out.println("person " + persons.get(current).getId() + "行酒令,并退出圈子"); persons.remove(current); direct = !direct; if (!direct) current -= 1; } else { if (direct) current += 1; else current -= 1; } i++; } } public static void main(String args[]) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); B_H_J test = new B_H_J(); test.execute(n); } } class Person { private int id; public Person(int id) { this.id = id; } public int getId() { return this.id; } }