约瑟夫问题
男孩类
class Boy {
private int no;
private Boy next;
public Boy ( int no) {
this . setNo ( no) ;
}
public int getNo ( ) {
return no;
}
public void setNo ( int no) {
this . no = no;
}
public Boy getNext ( ) {
return next;
}
public void setNext ( Boy next) {
this . next = next;
}
}
定义头节点
class CircleSingleLinkList {
private Boy first = null;
添加小孩
public void addBoy ( int nums) {
if ( nums< 1 ) {
System. out. println ( "数据不正确" ) ;
return ;
}
Boy cusBoy = null;
for ( int i = 1 ; i<= nums; i++ ) {
Boy boy = new Boy ( i) ;
if ( i== 1 ) {
first = boy;
boy. setNext ( first) ;
cusBoy = first;
}
else {
cusBoy. setNext ( boy) ;
boy. setNext ( first) ;
cusBoy = boy;
}
}
}
显示
public void showBoy ( ) {
if ( first == null) {
System. out. println ( "链表为空" ) ;
return ;
}
Boy cusBoy = first;
while ( true ) {
System. out. println ( "Boy-编号:" + cusBoy. getNo ( ) ) ;
if ( cusBoy. getNext ( ) == first) {
break ;
}
cusBoy = cusBoy. getNext ( ) ;
}
}
出圈算法
public void countBoy ( int startNo, int countNo, int nums) {
if ( first == null|| startNo< 1 || startNo> nums) {
System. out. println ( "参数错误" ) ;
return ;
}
Boy helper = first;
while ( true ) {
if ( helper. getNext ( ) == first) {
break ;
}
helper = helper. getNext ( ) ;
}
for ( int j= 0 ; j< startNo- 1 ; j++ ) {
first = first. getNext ( ) ;
helper = helper. getNext ( ) ;
}
while ( true ) {
if ( helper == first) {
break ;
}
for ( int j = 0 ; j< countNo- 1 ; j++ ) {
first = first. getNext ( ) ;
helper = helper. getNext ( ) ;
}
System. out. println ( "出圈:" + first. getNo ( ) ) ;
first = first. getNext ( ) ;
helper. setNext ( first) ;
}
System. out. println ( "-----------------" ) ;
System. out. println ( "最后的小孩为:" + helper. getNo ( ) ) ;
}
全部代码
package linkedList;
public class Josepfu {
public static void main ( String[ ] args) {
CircleSingleLinkList list = new CircleSingleLinkList ( ) ;
list. addBoy ( 1000 ) ;
list. showBoy ( ) ;
list. countBoy ( 1 , 9 , 1000 ) ;
}
}
class CircleSingleLinkList {
private Boy first = null;
public void addBoy ( int nums) {
if ( nums< 1 ) {
System. out. println ( "数据不正确" ) ;
return ;
}
Boy cusBoy = null;
for ( int i = 1 ; i<= nums; i++ ) {
Boy boy = new Boy ( i) ;
if ( i== 1 ) {
first = boy;
boy. setNext ( first) ;
cusBoy = first;
}
else {
cusBoy. setNext ( boy) ;
boy. setNext ( first) ;
cusBoy = boy;
}
}
}
public void showBoy ( ) {
if ( first == null) {
System. out. println ( "链表为空" ) ;
return ;
}
Boy cusBoy = first;
while ( true ) {
System. out. println ( "Boy-编号:" + cusBoy. getNo ( ) ) ;
if ( cusBoy. getNext ( ) == first) {
break ;
}
cusBoy = cusBoy. getNext ( ) ;
}
}
public void countBoy ( int startNo, int countNo, int nums) {
if ( first == null|| startNo< 1 || startNo> nums) {
System. out. println ( "参数错误" ) ;
return ;
}
Boy helper = first;
while ( true ) {
if ( helper. getNext ( ) == first) {
break ;
}
helper = helper. getNext ( ) ;
}
for ( int j= 0 ; j< startNo- 1 ; j++ ) {
first = first. getNext ( ) ;
helper = helper. getNext ( ) ;
}
while ( true ) {
if ( helper == first) {
break ;
}
for ( int j = 0 ; j< countNo- 1 ; j++ ) {
first = first. getNext ( ) ;
helper = helper. getNext ( ) ;
}
System. out. println ( "出圈:" + first. getNo ( ) ) ;
first = first. getNext ( ) ;
helper. setNext ( first) ;
}
System. out. println ( "-----------------" ) ;
System. out. println ( "最后的小孩为:" + helper. getNo ( ) ) ;
}
}
class Boy {
private int no;
private Boy next;
public Boy ( int no) {
this . setNo ( no) ;
}
public int getNo ( ) {
return no;
}
public void setNo ( int no) {
this . no = no;
}
public Boy getNext ( ) {
return next;
}
public void setNext ( Boy next) {
this . next = next;
}
}