2746:约瑟夫问题 百练noi

import java.util.Scanner;

public class Main {
		public static int flag[]=new int[305];
		public static void init(){
			for(int i=0;i<305;i++){
				flag[i]=0;
			}
		}
		public static void main(String[] args){
			Scanner sc=new Scanner(System.in);
			int n,m;
			n=sc.nextInt();
			m=sc.nextInt();
			while(n!=0 && m!=0){
				int index=-1;
				int count=0;
				init();
				for(int i=1;i<n;i++){
					count=0;
					while(count!=m){
						index=(index+1)%n;
						if(flag[index]==0){
							count++;
							if(count==m){
								flag[index]=1;
							}
						}
					}
				}
				for(int j=0;j<n;j++){
					if(flag[j]==0){
						System.out.println(j+1);
						break;
					}
				}
				n=sc.nextInt();
				m=sc.nextInt();
			}
		}
}
posted @ 2021-04-04 15:43  符瑾瑜  阅读(193)  评论(0)    收藏  举报