public class Josephus {
public static void main(String[] args) {
int M = Integer.parseInt(args[0]);
int N = Integer.parseInt(args[1]);
// initialize the queue
Queue<Integer> q = new Queue<Integer>();
for (int i = 0; i < N; i++)
q.enqueue(i);
while (!q.isEmpty()) {
for (int i = 0; i < M-1; i++)
q.enqueue(q.dequeue());
StdOut.print(q.dequeue() + " ");
}
StdOut.println();
}
c++代码
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(int argc, char const *argv[])
{
int m,n;
cin>>m>>n;
queue<int> a;
for(int i=1;i<=m;i++)
a.push(i);
while(!a.empty()){
for(int i=0;i<n-1;i++){
int c=a.front();
a.pop();
a.push(c);
}
int c=a.front();
cout<<c<<endl;
a.pop();
}
return 0;
}