回复 4楼 wtuaimmmm
楼上的代码自己测试过么?虽然是帮人,给的代码还是全点比较好,还有你的算法,没有考虑到最后剩下不到3个人的时候怎么办,
我给你加了一条测试语句,把人数改成三个,你看下你的结果
程序代码:
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)+"位");
}
}
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)+"位");
}
}
运行结果是:
3死了
5死了
7死了
2死了
4死了
1死了
3死了
2死了
2死了
你应该站在旅游团的: 4位
----------------解决方案--------------------------------------------------------
酒吧行酒令代码,不知道是否符合楼主的要求
程序代码:
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;
}
}
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;
}
}
----------------解决方案--------------------------------------------------------
提示: 作者被禁止或删除 内容自动屏蔽