约瑟夫环

来源:互联网 发布:查手机真假软件 编辑:程序博客网 时间:2024/04/29 01:13

/*有M个人,其编号分别为1-M。这M个人按顺序排成一个圈。现在给定一个数N,从第一个人开始依次报数,数到N的人出列,然后又从下一个人开始又从1开始依次报数,数到N的人又出列...如此循环,直到最后一个人出列为止。

Input

输入只有一行,包括2个整数M(8 <= M <= 15 ),N( 5 <= N <= 32767 )。之间用一个空格分开。

Output

输出M行,每行一个整数。

Sample Input


8 5

Sample Output


5
2
8
7
1
4
6
3
*/
import java.util.Scanner;

public class Josph2{
 public static void main(String args[]){
  Scanner sc = new Scanner(System.in);  
  int person = sc.nextInt();
  int count = sc.nextInt();
    int []c = new int[person];//计数数组
  System.out.println();
  for(int i=0;i<c.length;i++){ //将数组元素置为1
   c[i] = 1;
  }
  for(int i=0;i<c.length;i++){
   System.out.print(c[i] + ",");
  }
  System.out.println();
  int i=0;   //下标
  int add=0; //计数
  int time=0;//循环结束标志
  while(true){   
   add = add + c[i];
   if(add == count){
    c[i] =0;
    System.out.println(i+1);
    add = 0;
    time++;
    if(time>= person){
     break;
    }
   }
   i++;
   if(i>=c.length){
    i=0;
    continue;
   }
      
  }  
 }
}