模拟
参考代码:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.ArrayList; import java.util.List; public class Main{ static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer in = new StreamTokenizer(reader); static int STnextInt() throws IOException{ in.nextToken(); return (int)in.nval; } public static void main(String[] args) throws IOException{ int n = STnextInt(); int k = STnextInt(); boolean[] vis = new boolean[n]; int cnt = n; int index = 0; List<Integer> res = new ArrayList<>(); while(cnt > 0){ int num = 1; // 这里num只是帮忙计数,1~k while(num < k) { num++; index = (index + 1) % n; while(vis[index]) { index = (index + 1) % n; } } vis[index] = true; res.add(index + 1); cnt--; while(cnt > 0 && vis[index]) { index = (index + 1) % n; } } for(int x: res) { System.out.print(x + " "); } } }
0.0分
0 人评分