//算法2 count(10,5)--- 5 10 6 2 9 8 1 4 7 3 public static void count(int m, int n) { int[] people = new int[m]; for(int i = 0; i < m; ++i) people[i]=i+1;
初学者版(算法比较幼稚,但很好理解) /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class yuesefu { public yuesefu() { } //getOriginalPosition public static void getOriginalPosition(int m, int n) { // int [] circlePeople = new int[m]; int [] linePeople = new int[m]; for(int p=0 ; p < m ; p ++) { circlePeople[p] = 0; linePeople[p] = 0; } int cursor = 0; int circleNum = 1; int outCount = 0; while(outCount < m) { if(circlePeople[cursor] == -1) { cursor++; if(cursor == m) { cursor = 0; } continue; } if(circleNum == n) { //-1 means this people has been out circlePeople[cursor] = -1; linePeople[outCount++] = cursor; circleNum=0; } cursor++; circleNum++; if(cursor == m) { cursor = 0; } } //print linepeople's information for(int out =0; out < m ; out++ ) { System.out.println("The origianl number of People in line " + (out+1) + " is " + (linePeople[out]+1) ); } } //main function public static void main(String [] args) { yuesefu.getOriginalPosition(30,5); } }result is : The origianl number of People in line 1 is 5The origianl number of People in line 2 is 10The origianl number of People in line 3 is 15The origianl number of People in line 4 is 20The origianl number of People in line 5 is 25The origianl number of People in line 6 is 30The origianl number of People in line 7 is 6The origianl number of People in line 8 is 12The origianl number of People in line 9 is 18The origianl number of People in line 10 is 24The origianl number of People in line 11 is 1The origianl number of People in line 12 is 8The origianl number of People in line 13 is 16The origianl number of People in line 14 is 23The origianl number of People in line 15 is 2The origianl number of People in line 16 is 11The origianl number of People in line 17 is 21The origianl number of People in line 18 is 29The origianl number of People in line 19 is 13The origianl number of People in line 20 is 26The origianl number of People in line 21 is 7The origianl number of People in line 22 is 22The origianl number of People in line 23 is 9The origianl number of People in line 24 is 28The origianl number of People in line 25 is 19The origianl number of People in line 26 is 17The origianl number of People in line 27 is 27The origianl number of People in line 28 is 4The origianl number of People in line 29 is 14The origianl number of People in line 30 is 3
上面的linePeople []数组,就是你想要的那个队列
public class test{public static int[] getMN(int M, int N){ int ret[]=new int[M]; int out=0; int start=-1; for (int i=0;i<M;i++){ret[i]=0;}
// 算法3 public static void count(int m, int n) { Vector people = new Vector(), selected = new Vector(); for (int i = 0; i < m; ++i) people.add("" + (i + 1)); for(int current = 0; people.size() != 0; ) selected.add(people.remove(current = (current + n - 1) % people.size())); for (int i = 0; i < m; ++i) System.out.print(selected.elementAt(i) + " "); }
int ret[]=new int[M];
int out=0;
int start=-1;
for (int i=0;i<M;i++){ret[i]=0;}
for(int i=0;i<M;i++){
while(out<N){
start++;
if(ret[start%M]==0) out++;
}
ret[start%M]=i+1;
out=0;
start=start%M;
}
return ret;
}人按数组顺序排列,数组存放的值是出圈的顺序
for(int i=0; i<ret.length; i++)
lastRet[ret[i]-1]=i+1;
count(10,5)--- 5 10 6 2 9 8 1 4 7 3
public static void count(int m, int n) {
int[] people = new int[m];
for(int i = 0; i < m; ++i) people[i]=i+1;
int j = 0,outCount=0,numCount = 0;
while (outCount < m) {
if (people[j++ % m] < 0 || (numCount = (numCount + 1) % n) != 0)
continue;
outCount++;
System.out.print(" " + people[ (j - 1 + m) % m]);
people[ (m + j - 1) % m] = -1;
}
}
public Class test{public static int[] getMN(int M, int N){
int ret[]=new int[M];
int out=0;
int start=-1;
for (int i=0;i<M;i++){ret[i]=0;}
for(int i=0;i<M;i++){
while(out<N){
start++;
if(ret[start%M]==0) out++;
}
ret[start%M]=i+1;
out=0;
start=start%M;
}
return ret;
}
}已经完整了
int ret[]=test.getMN(20,14);
你命名成test.java
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class yuesefu
{
public yuesefu()
{
}
//getOriginalPosition
public static void getOriginalPosition(int m, int n)
{
//
int [] circlePeople = new int[m];
int [] linePeople = new int[m];
for(int p=0 ; p < m ; p ++)
{
circlePeople[p] = 0;
linePeople[p] = 0;
}
int cursor = 0;
int circleNum = 1;
int outCount = 0;
while(outCount < m)
{
if(circlePeople[cursor] == -1)
{
cursor++;
if(cursor == m)
{
cursor = 0;
}
continue;
} if(circleNum == n)
{
//-1 means this people has been out
circlePeople[cursor] = -1;
linePeople[outCount++] = cursor; circleNum=0;
} cursor++;
circleNum++;
if(cursor == m)
{
cursor = 0;
}
} //print linepeople's information
for(int out =0; out < m ; out++ )
{
System.out.println("The origianl number of People in line " + (out+1) + " is " + (linePeople[out]+1) );
} }
//main function
public static void main(String [] args)
{
yuesefu.getOriginalPosition(30,5);
}
}result is :
The origianl number of People in line 1 is 5The origianl number of People in line 2 is 10The origianl number of People in line 3 is 15The origianl number of People in line 4 is 20The origianl number of People in line 5 is 25The origianl number of People in line 6 is 30The origianl number of People in line 7 is 6The origianl number of People in line 8 is 12The origianl number of People in line 9 is 18The origianl number of People in line 10 is 24The origianl number of People in line 11 is 1The origianl number of People in line 12 is 8The origianl number of People in line 13 is 16The origianl number of People in line 14 is 23The origianl number of People in line 15 is 2The origianl number of People in line 16 is 11The origianl number of People in line 17 is 21The origianl number of People in line 18 is 29The origianl number of People in line 19 is 13The origianl number of People in line 20 is 26The origianl number of People in line 21 is 7The origianl number of People in line 22 is 22The origianl number of People in line 23 is 9The origianl number of People in line 24 is 28The origianl number of People in line 25 is 19The origianl number of People in line 26 is 17The origianl number of People in line 27 is 27The origianl number of People in line 28 is 4The origianl number of People in line 29 is 14The origianl number of People in line 30 is 3
int ret[]=new int[M];
int out=0;
int start=-1;
for (int i=0;i<M;i++){ret[i]=0;}
for(int i=0;i<M;i++){
while(out<N){
start++;
if(ret[start%M]==0) out++;
}
ret[start%M]=i+1;
out=0;
start=start%M;
}
return ret;
}
}
public static void count(int m, int n) {
Vector people = new Vector(), selected = new Vector();
for (int i = 0; i < m; ++i)
people.add("" + (i + 1)); for(int current = 0; people.size() != 0; )
selected.add(people.remove(current = (current + n - 1) % people.size())); for (int i = 0; i < m; ++i)
System.out.print(selected.elementAt(i) + " ");
}