问题分析
一,问题描述
500个人围成一圈 ,从第一个人开始数数,第一个人数1 第二个人数2 第三个人数3 ,数到3的人退出圈子,第四个人重新开始数数 ,第四个数1 ,第五个2 ,第六个 3(退出圈子),一直这样数下去,求最后剩余的那个人所在位置。二,问题详细求解思路
1.面向过程的思想
数组表示500个人围成一个圈 数组有类型 描述该人不在圈子中 boolean[] cicle = new boolean[500] true 表示在圈子中 false 表示不在数数从数组的下标为0的那个元素开始
count = 0 用来计数
数数的过程 循环的过程 退出循环的条件是圈子中只剩下一个人,cicle数组中只有一个值为true的元素的时候就退出
leftCont = 500 表示数组中还剩余值为true的元素的个数(剩余人的个数)
index = 0;//记录数组的下标
while(leftCont > 1)
if(cicle[index])
count ++
判断index ++ 之后是否为3
if(count == 3)
退出圈子
cicle[index] = false
重新赋值index的值
count = 0;
leftCont --
index ++
//i不能超过数组的长度
if(index >= 500)
index = 0
2.面向对象封装的思想
人对象 personid 表示在圈子的位置
左边的人对象 left
右边的人对象 right
圈子对象 cicle
圈子大小 count = 0
圈子的第一个人person first
圈子的最后一个人person last
方法抽象
圈子要初始化
new cicle(500)
方法 加人(person p)
圈子中已经有人的情况
count ++
原来圈子中最后一个人的右边 > 新加人p
新加人p的左边 》 原来圈子中的最后一个人
新加人p的右边 》 原来圈子中第一个人
原来圈子中最后一个人 = 新加人p
如果是一个空圈 count= 0
圈子中的第一个人 = 新加人p
圈子中最后一个人 = 新加人p
方法 删除人(person p)
删除人p左边的人 》 删除人右边的人
删除人右边的人 》 删除人左边的人
如果删除的是第一个人
删除人的右边的人变成了圈子中的第一个人
如果删除人是最后一个人
删除人的左边变成了圈子中的最后一个人
java代码实现
一,代码结构
Person类:
用于封装人对象的,主要成员有:1.自身的ID 2.右边的人 3.左边的人。personCircle类:
用于封装圈对象:主要成员有:1.圈内人数 2.圈内第一个人 3.圈内最后一个人。Test类:
用于测试程序的类二,可运行代码
Person类:
package com.kp;
/**
* @author Fiee
@filecomment /**
*
*/
public class Person {
/**
* 人对象
*/
/**
* id号
*/
private int id;
/**
* 左边的人
*/
private Person left;
/**
* 右边的人
*/
private Person right;
/**
* @param id
* @param left
* @param right
*/
public Person(int id, Person left, Person right) {
super();
this.id = id;
this.left = left;
this.right = right;
}
/**
* @param id
*/
public Person(int id) {
super();
this.id = id;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the left
*/
public Person getLeft() {
return left;
}
/**
* @param left the left to set
*/
public void setLeft(Person left) {
this.left = left;
}
/**
* @return the right
*/
public Person getRight() {
return right;
}
/**
* @param right the right to set
*/
public void setRight(Person right) {
this.right = right;
}
}
h6>personCircle类:
package com.kp;
/**@author Fiee
@version 创建时间:2019年6月24日 下午1:44:27
*/
public class PersonCircle {
/**
* 圈对象
*/
/**
* 圈中的人数
*/
private int count;
/**
* 圈中的第一个人
*/
private Person first;
/**
* 圈中的最后一个人
*/
private Person last;
/**
* @return the count
*/
public int getCount() {
return count;
}
/**
* @param count the count to set
*/
public void setCount(int count) {
this.count = count;
}
/**
* @return the first
*/
public Person getFirst() {
return first;
}
/**
* @param first the first to set
*/
public void setFirst(Person first) {
this.first = first;
}
/**
* @return the last
*/
public Person getLast() {
return last;
}
/**
* @param last the last to set
*/
public void setLast(Person last) {
this.last = last;
}
/**
* @param count
*/
public PersonCircle(int count) {
super();
//this.count = count;
for (int i = 0; i < count; i++) {
Person p=new Person(i);
add(p);
}
}
/**
*
* @param p 进圈的人
*/
public void add(Person p)
{
//当圈中没人时
if (this.count==0) {
this.first=p;
this.last=p;
}
else
{
p.setLeft(this.last);
p.setRight(this.first);
this.first.setLeft(p);
this.last.setRight(p);
this.last=p;
}
this.count++;
}
/**
*
* @param p 出圈的人
*/
public void remove(Person p)
{
p.getLeft().setRight(p.getRight());
p.getRight().setLeft(p.getLeft());
//如果删除的是第一个人
if (p.equals(this.first)) {
this.first=p.getRight();
}
//如果删除的是最后一个人
if (p.equals(p.getLeft())) {
this.last=p.getLeft();
}
this.count--;
}
public void location(PersonCircle pc)
{
// //初始化圈
// PersonCircle pc=new PersonCircle(5);
//计数器
int countNum=0;
Person p=pc.first;
while(pc.count>1)
{
countNum++;
if (countNum==3) {
System.out.println("本次出去的人的ID是:"+p.getId());
pc.remove(p);
countNum=0;
}
p=p.getRight();
}
System.out.println("最后在圈里的人是:"+pc.first.getId());
}
/**
* 打印圈里的id
*/
public void printAll()
{
System.out.println(this.first.getId());
Person p=this.first.getRight();
while (p!=this.first) {
System.out.println(p.getId());
p=p.getRight();
}
}
}
Test类:
package com.kp;
/**@author Fiee
@version 创建时间:2019年6月24日 下午2:26:49
*/
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonCircle pc=new PersonCircle(5);
pc.printAll();
pc.location(pc);
}
}
三,运行结果
0
1
2
3
4
本次出去的人的ID是:2
本次出去的人的ID是:0
本次出去的人的ID是:4
本次出去的人的ID是:1
最后在圈里的人是:3