黑马程序员技术交流社区
标题:
有100个人围成一个圈,从1开始报数,报到14的这个人就要...
[打印本页]
作者:
a9623a
时间:
2016-4-25 14:40
标题:
有100个人围成一个圈,从1开始报数,报到14的这个人就要...
package test;
import java.util.ArrayList;
public class Mytest {
public static void main(String[] args) {
System.out.println(getPerson(100));
}
public static int getPerson(int count) {
ArrayList<Integer> as = new ArrayList<>(100);
for (int i = 0;i < count;i++) {
as.add(i + 1);
}
return getNumber(as, 0);
}
public static int getNumber(ArrayList<Integer> as,int numberEnd) {
for (int i = 0;i < as.size();i++) {
if ((as.get(i) + numberEnd) % 14 == 0) {
as.remove(i);
}
}
if (as.size() > 1) {
numberEnd += as.get(as.size() - 1);
getNumber(as,numberEnd);
}
return as.get(0);
}
}
复制代码
有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
我使用递归算出来是55,不知道有没有大神来帮忙鉴定一下.
作者:
toukya
时间:
2016-4-25 17:44
不是92吗?
作者:
943480861
时间:
2016-4-25 19:21
好像不是55
作者:
小K哥
时间:
2016-4-25 19:36
import java.util.ArrayList;
public class Ezample {
public static void main(String[] args) {
ArrayList<Integer> array=new ArrayList<Integer>();
for(int i=1;i<=100;i++)
array.add(i);
int i=0;
while(array.size()>1){
i=(i+14-1)%array.size();
System.out.println("删除"+array.remove(i));
}
System.out.println(array.get(0));
}
}
是92
作者:
a9623a
时间:
2016-4-26 10:53
package test;
public class Person {
private int value;
private Person previousPerson;
private Person nextPerson;
public Person() {};
public Person(int value) {
this.value = value;
}
public Person(int value,Person previousPerson) {
this.value = value;
this.previousPerson = previousPerson;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Person getNextPerson() {
return nextPerson;
}
public void setNextPerson(Person nextPerson) {
this.nextPerson = nextPerson;
}
public Person getPreviousPerson() {
return previousPerson;
}
public void setPreviousPerson(Person previousPerson) {
this.previousPerson = previousPerson;
}
}
package test;
public class Mytest {
public static void main(String[] args) {
Person first = new Person(1);
Person pre = first;
for (int i = 2;i < 100;i++) {
Person p = new Person(i, pre);
pre.setNextPerson(p);
pre = p;
}
Person last = new Person(100, pre);
pre.setNextPerson(last);
last.setNextPerson(first);
first.setPreviousPerson(last);
Person p = first;
int count = 1;
while (p.getNextPerson() != p) {
if (count % 14 == 0) {
System.out.println(p.getValue() + "出局");
Person prePerson = p.getPreviousPerson();
Person nextPerson = p.getNextPerson();
prePerson.setNextPerson(nextPerson);
nextPerson.setPreviousPerson(prePerson);
p = nextPerson;
count = 1;
} else {
p = p.getNextPerson();
count++;
}
}
System.out.println("最后还剩第" + p.getValue() + "人");
}
}
复制代码
谢谢各位提醒,是错误的。
我重新写了一个,仿照链表的方式写的, 答案是92了。
作者:
Midicy
时间:
2016-4-26 15:59
//面向对象的思路去做
public class Test9 {
public static void main(String[] args) {
//调用后面的Personcircle类,有100人
Personcircle pc = new Personcircle(100);
//用countNumber计数,等于14的人退出,原来右边的人重新报1
int countNumber = 0;
Person p = pc.first;
while (pc.count > 1) {
countNumber ++;
if( countNumber == 14){
countNumber = 0;
pc.delete(p);
}
p = p.right;
}
//打印剩下的一个人的位置
System.out.println("原排在第"+(pc.first.id+1)+"位的人留下了。");
}
}
//person类,属性包括ID,左边人,右边人。
class Person{
int id;
Person left;
Person right;
}
//Personcircle类,,定义属性
class Personcircle{
int count = 0;
Person first,last;
//Personcircle构造方法
Personcircle(int n){
for(int i = 0; i < n; i++){
add();
}
}
//添加人的方法
void add(){
Person p = new Person();
p.id = count;
if(count <= 0){
//一个人时,最开始,最后的人都是自己
first = p;
last = p;
p.left = p;
p.right = p;
}
else{
//多人时,相互连接构成圈
last.right = p;
p.left = last;
p.right = first;
first.left = p;
last = p;
}
//表明多了一个人
count ++;
}
//删除人的方法
void delete (Person p){
if(count <= 0){
return;
}//就一个人,不用删
else if (count == 1){
first = last = null;
}
else{
//一个人退出后,原来这个人左右两边的人会互相连接
p.left.right = p.right;
p.right.left = p.left;
//如果正好是第一个人被删除的情况
if(p == first){
first = p.right;
}//如果删除的是最后一个人的情况
else if (p ==last){
last = p.left;
}
}
//少了一个人
count --;
}
}
作者:
九天玄妖
时间:
2016-4-26 16:43
我的结果是92
代码如下:
package com.itheima;
import java.util.ArrayList;
public class Test9 {
public static void main(String[] args) {
//创建一个集合
ArrayList<Integer> list = new ArrayList<>();
//把100人按编号存放到集合中
for(int i=1; i<=100; i++){
list.add(i);
}
int count = 1;
for (int i = 0; list.size() != 1; i++) {
//数到一圈的最后一个人,从头开始数
if(i == list.size()){
i = 0;
}
//去掉数14的这个人,并重新开始数数
if(count % 14 == 0){
list.remove(i--);
count = 0;
}
count++;
}
System.out.println(list.get(0));
}
}
System.out.println(list.get(0));
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2