编程语言
首页 > 编程语言> > Java-POJ1012-Joseph

Java-POJ1012-Joseph

作者:互联网

打表

约瑟夫环,处理时下表统一为从0开始更方便!

 1 import java.util.Scanner;
 2 
 3 public class poj1012 {
 4     public static boolean calc(int k,int m) {
 5         boolean flag=true;
 6         for(int pos=0,res=2*k;res>k;res--) {
 7             pos=(pos+m-1)%res;//1~2k转为0~2k-1
 8             if(pos<k) flag=false;//<=k转为<k
 9         }
10         return flag;
11     }
12     public static void main(String[] args) {
13 
14         Scanner cin = new Scanner(System.in);
15         int[] ans = new int[30];
16         for (int k = 1; k < 14; k++) {
17             int m = k + 1;
18             while (true) {
19                 if (!calc(k, m))m++;
20                 else {ans[k] = m;break;}
21             }
22         }
23         while (cin.hasNext()){
24             int i=cin.nextInt();
25             if(i!=0)System.out.println(ans[i]);
26             else break;
27         }
28     }
29 }

 

标签:Java,int,res,POJ1012,pos,Joseph,boolean,public,2k
来源: https://www.cnblogs.com/JasonCow/p/12243265.html