n个人排成一圈,从1开始报数,数到3,退出,剩下的最后一个人

 

package test;

import java.util.LinkedList;
import java.util.List;

public class Test4 {
 public static void main(String[] args){
  Joesephe joesephe = new Joesephe();
  int count = 1 ;
  int index = 0 ;
  for( int i = 1 ; i < 11 ; i++){
   joesephe.addElemnet(i);
  }
  
  while(joesephe.getLength() != 1){
   if(joesephe.getElement(index) == 0){
    index = (index + 1) >= joesephe.getSize() ? 0 : (index + 1) ;
    continue;
   }
   if(count == 3){
    joesephe.changeElement(index, 0);
   }
   count = (count + 1) == 4 ? 1 : count + 1;
   index = (index + 1) >= joesephe.getSize() ? 0 : (index + 1) ;
  }
  for(int i = 0 ;i<joesephe.getSize() ; i++){
   if(joesephe.getElement(i) != 0 ){
    System.out.println(joesephe.getElement(i));
   }
  }
 }
}
class Joesephe{
 private List<Integer> list = new LinkedList<Integer>();
 private int changCount = 0;
 public void addElemnet(Integer element){
  list.add(element);
 }
 public void deleteElement(int index){
  list.remove(index);
 }
 public void changeElement(int index,Integer element){
  list.remove(index);
  list.add(index, element);
  changCount ++;
 }
 public Integer getLength(){
  return list.size() - changCount;
 }
 public Integer getSize(){
  return list.size();
 }
 public Integer getElement(int index){
  return list.get(index);
 }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值