java有n个人围城一圈,顺序排号,从第一个开始报数(从一到三报数),凡报道三的人退出
面向对象的方法
package com.mssysde.edu.demo.haoyihang;
import com.mssysde.edu.demo.teacher.Persion;
public class BaoShuTest2 {
public static void main(String[] args) {
System.out.println(" 最后剩下的人是最帅的 "+baoshu(100));
}
public static String baoshu(int input) {
//建立报数人的数组
Persion[] arr1=new Persion[input];
//index数组下标,count报数3的判断数,inputt剩下的人
int index = 0, count = 0, inputt = input;
//每一个Persion对象的初始化
for(int i=0;i<arr1.length;i++) {
//使每个对象都实例化,名字和生命值数
arr1[i]=new Persion("郝"+(i+1), 1);
}
while (inputt > 1) {
if (arr1[index].isLive()) {
count++;
if (count == 3) {
arr1[index].setLife(arr1[index].getLife()-1);//命数减一
count = 0;
if(arr1[index].getLife()<=0) {//生命值为0的时候
arr1[index].setLive(false);
inputt--;
}
}
}
index++;
if (index == input) {
index = 0;
}
}
//输出剩下的人的名字
for(Persion a: arr1) {
if(a.isLive()) {
return a.getName();
//System.out.println("最终剩下的人是最帅的 "+a.getName());
}
}
return null;
}
}
//Persion类
package com.mssysde.edu.demo.teacher;
public class Persion {
/**
* @param name
* @param life
*/
public Persion(String name, int life) {
this.name = name;
this.life = (life <= 0) ? 1 : life;
}
private String name;
private int life;
private boolean isLive=true;
/**
* @return the isLive
*/
public boolean isLive() {
return isLive;
}
/**
* @param isLive the isLive to set
*/
public void setLive(boolean isLive) {
this.isLive = isLive;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the life
*/
public int getLife() {
return life;
}
/**
* @param life the life to set
*/
public void setLife(int life) {
this.life = life;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class DequeueNumThree {
//数组
public static void MethodArr(){
System.out.println("Please input total people : ");
Scanner scanner = new Scanner(System.in);
int totalPeople = scanner.nextInt();
boolean[] arr = new boolean[totalPeople];
for(int i = 0; i < arr.length; i++){
arr[i] = true;
}
int count = 0;//报数 1 2 3
int leftPeople = totalPeople;//去掉报道3之后剩余的人数
int index = 0;//数组下标
while(leftPeople > 1){//最后只能剩余一个人
if(arr[index] == true){//如果当前状态为true
count++;//报数
if(count == 3){//如果报数为3
arr[index] = false;//状态设置成false
count = 0;//为下次重新报数做准备
leftPeople--;//剩余人数减一
}
}
index++;//到下一个人了
if(index == totalPeople){//如果到最后一个人了,那么,从头重新开始报数
index = 0;
}
}
for(int i = 0; i < totalPeople; i++){
if(arr[i] == true){
System.out.println("last number is " + (i+1));
}
}
}
}
//链表
package test01;
import java.util.LinkedList;
import java.util.List;
public class forxx {
public static int cycle(int total,int k){
List<Integer> dataList = new LinkedList<Integer>();//创建一个链表
for(int i=0;i<total;i++) //添加数据成员
dataList.add(new Integer(i+1));
int index=-1;
//循环
while(dataList.size()>1){
index = (index+k)%dataList.size();//以(index+k)对size()取余
dataList.remove(index--);
}
return ((Integer)dataList.get(0).intValue());//返回它的值
}
public static void main(String[] args) {
System.out.println("该人原来的位置是: "+cycle(10000,3));
}
}