AbuserBIG 于 2016.05.04 16:44 提问

4个回答

lp897225636   2016.05.04 18:14
``````public static void compute(int n) {
ArrayList<Integer> number = new ArrayList<>();
for (int i = 1; i <= n; i++) {
}
while (number.size() > 1) {
int index = 0;
int count = 0;
while (index < number.size()) {
number.remove(index);
count++;
index = count * (count + 1) / 2;
}
}
}
``````
``````
``````
AbuserBIG 您没看明白要求吧, 我自己搞出来了. 总之还是谢谢了

lp897225636   2016.05.04 18:14
``````    public static void compute(int n) {
ArrayList<Integer> number = new ArrayList<>();
for (int i = 1; i <= n; i++) {
}
while (number.size() > 1) {
int index = 0;
int count = 0;
while (index < number.size()) {
number.remove(index);
count++;
index = count * (count + 1) / 2;
}
}
}
``````
qinjisheng_11   2016.05.05 01:12
``````
public class Person {
private int id;
private boolean live;

public Person(int id, boolean live) {
this.id = id;
this.live = live;
}

public boolean isLive() {
return live;
}

public void setLive(boolean live) {
this.live = live;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

}
``````
`````` import java.util.ArrayList;
import java.util.List;

public class CountNumber {
public static final int NUMBER = 100;

List<Person> persons = new ArrayList<Person>();

public static void main(String[] args) {
CountNumber cn = new CountNumber();
cn.deals();
}

public void deals() {
while (persons.size() != 1) {
deal();
}
System.out.println("最后留下的数字是：" + persons.get(0).getId());
}

public void deal() {
int count = 1;// 计数器
int index = 0;// 指示器

while (index < persons.size()) {
persons.get(index).setLive(false);
count++;
index += count;
}

for (int i = 0; i < persons.size(); i++) {
if (persons.get(i).isLive() == false) {
persons.remove(i);
}
}
}

for (int i = 1; i < NUMBER+1; i++) {
Person p = new Person(i, true);
}
}
}

``````

AbuserBIG 可能我说的不明白, 100数相当于摆成一个圈, 数到100之后接着从2开始数, 并不是说新的循环开始之前数到就不算了. 我自己搞了一早上搞出来了, 总之还是谢谢了.

lp897225636 回复有节奏的键盘: 感觉搞得太复杂了

qinjisheng_11 上面的Person.java,下面的CountNumber.java，自己运行没问题

AbuserBIG   2016.05.05 14:02