無界面!
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序定制開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了樂清免費建站歡迎大家使用!
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class LinkCircle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n, k, m, i, j, count, p, q;
int[] a = new int[100];
ListInteger array = new LinkedListInteger();
p = 0;
q = 0;
count = 0;
n = in.nextInt();
m = in.nextInt();
for (i = 0; i n; i++) {
array.add(i + 1);
System.out.printf(i != n - 1 ? "%d " : "%d\n", array.get(i));
}
int pos = -1;
int x = 0;
while(array.size() != 1)
{
//x = array.get(pos);
pos += m ;
if(pos = array.size())
{
pos = pos % array.size();
}
System.out.println("remove pos:" + (pos + 1) + ", value:" + array.get(pos));
array.remove(pos--);
System.out.print("Array : ");
for (i = 0; i array.size(); i++) {
System.out.printf(i != array.size() - 1 ? "%d " : "%d\n\n", array.get(i));
}
}
System.out.println("res:" + array.get(0));
}
}
public?class?TestJosephus?{
public?static?void?main(String[]?args)?{
//留幾個人
int?alive?=?2;
//總?cè)藬?shù)
int?total?=?41;
//自殺者報數(shù)
int?killMan?=?3;
Josephus(alive,?total,?killMan);
}
/**
?*?@param?alive?存活的人初始位置序號//留幾個人
?*?@param?total?總?cè)藬?shù)
?*?@param?killMan?自殺者報數(shù)
?*/
public?static?void?Josephus(int?alive,?int?total,?int?killMan)?{
int?[]man?=?new?int[total];
int?count?=?1;
int?i?=?0;
int?pos?=?-1;
while?(count?=?total)?{
do?{
pos?=?(pos+1)%total;
if?(man[pos]?==?0)?{
i++;
}
if?(i?==?killMan)?{
i?=?0;
break;
}
}?while?(true);
man[pos]?=?count;
System.out.print("第?"?+?(pos+1)?+?"?個人自殺!約瑟夫環(huán)編號為:"?+?man[pos]);
if?(count?%?2?!=?0)?{
System.out.print("?-?");
}else?{
System.out.println("?-=?");
}
count++;
}
System.out.println();
System.out.println("這?"?+?alive?+"?個需要存活的人初始位置應(yīng)排在以下序號:");
alive?=?total?-?alive;
for?(i?=?0;?i??total;?i++)?{
if?(man[i]??alive)?{
System.out.println("初始編號:"?+?(i+1)?+?",約瑟夫環(huán)編號:"?+?man[i]);
}
}
System.out.println();
}
}
用java數(shù)組實現(xiàn)約瑟夫環(huán)
package?Josephround;
public?class?Joseround?{
int?sit;
int?flagjo=0;
Joseround(){};
Joseround(int?x){
sit=x;
}
void?setflag(int?x){
flagjo=x;
}
}
package?Josephround;
public?class?Inijose?{
Joseround?jo[];
static?int?length=0;
Inijose(){};
Inijose(int?x){
jo=new?Joseround[x];
for(int?i=0;ix;i++){
jo[i]=new?Joseround(i+1);//創(chuàng)建對象數(shù)組
length++;
}
}
void?delete(int?n){
for(int?i=n;ilength-1;i++){
jo[i]=jo[i+1];
}
length--;
}
}
package?Josephround;
import?java.util.Scanner;
public?class?Text?{
public?static?void?main(String[]?args)?{
int?m,n;
System.out.println("input?m");
Scanner?m1=new?Scanner(System.in);
m=m1.nextInt();
System.out.println("input?n");
Scanner?n1=new?Scanner(System.in);
n=n1.nextInt();
int?temp=0;
int?x=0;
Inijose?joseph=new?Inijose(n);
while(joseph.length!=0){
for(int?i=1;i=m;i++){
joseph.jo[x].setflag(i);
if(joseph.jo[x].flagjo==m){
System.out.println(joseph.jo[x].sit);
joseph.delete(x);
x--;
}
if(xjoseph.length-1)?x++;
else?x=0;
}
}
}
}
精簡易懂版本 剛寫的
public class Yuesefu {
// 數(shù)三退一 約瑟夫環(huán) 求最后留在環(huán)中小朋友的位置 即原來的編號(1-500)
/**
* 初始化數(shù)組,添加標號
*/
private void markID(int[] child) {
for (int i = 0; i = child.length - 1; i++) {
child[i] = i + 1;
}
}
/**
* 求出小孩子原來的位置在什么地方,從一開始
*/
public void re_index(int[] i) {
for (int ii : i) {
if (ii 0) {
System.out.println("還在場內(nèi)的小朋友的序號為 : " + ii);
}
}
}
/**
* 統(tǒng)計剩余的同學
*/
public int record(int[] re) {
int reco = 0;
for (int i : re) {
if (i 0) {
reco = reco + 1;
} else {
continue;
}
}
// System.out.println("目前還剩下:" + reco + " 個小朋友在圈圈里面");
System.out.println("程序努力加載計算中.............................................................................................." + reco);
return reco;
}
public static void run() {
int childs[] = new int[500];
Yuesefu yuesefu = new Yuesefu();
yuesefu.markID(childs);
yuesefu.startGame(childs);
yuesefu.re_index(childs);
}
/**
* 游戲開始
*/
private void startGame(int[] i) {
int index = 0; // 用作循環(huán)
int leave = 0; // 用作離場
while (true) {
if (i[index] != 0) {// 還在場內(nèi)
leave = leave + 1; // 報數(shù)
if (leave == 3) {// 判斷是否需要離場 需要
i[index] = 0; // 離場
leave = 0;// 重新報數(shù)
}
index = index + 1;// 下一個人預(yù)備
if (index == i.length) {// 已經(jīng)到最后了 需重新循環(huán)
index = 0;
}
} else {// 不在場內(nèi)
if (index == i.length - 1) {// 判斷此出場的人是否為最后一個人
index = 0;
}
index = index + 1;// 跳過
}
int x = new Yuesefu().record(i);// 統(tǒng)計剩余人數(shù)
if (x == 1)
break;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
run();
}
}