题目1188:约瑟夫环
来源:互联网 发布:js中new date 的参数 编辑:程序博客网 时间:2024/04/30 06:54
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.StringTokenizer;public class Main { public static void main(String[] args) { new Task().solve() ; }}class Task{InputReader in = new InputReader(System.in) ;PrintWriter out = new PrintWriter(System.out) ;class Node{int val ;Node next ;Node(int val){this.val = val ;next = null ;}}Node build(int n){Node head = new Node(1) ;Node now = head ; for(int i = 2 ; i <= n ; i++){Node node = new Node(i) ;now.next = node ;now = now.next ;}now.next = head ;return head ;}Node remove(Node head , int k , List<Integer>res){Node now = head ;Node pre = now ;for(int i = 2 ; i <= k ; i++){pre = now ;now = now.next ;}res.add(now.val) ;pre.next = now.next ;return now.next ;}void solve(){while(in.hasNext()){int n = in.nextInt() ;int k = in.nextInt() ;Node node = build(n) ;List<Integer> res = new ArrayList<Integer>() ;for(int i = 0 ; i < n ; i++){node = remove(node, k , res) ;}for(int i = 0 ; i < n ; i++){out.print((i > 0 ? " " : "") + res.get(i)) ;}out.println();out.flush();}out.flush();}}class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = new StringTokenizer(""); } private void eat(String s) { tokenizer = new StringTokenizer(s); } public String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } }
0 0
- 题目1188:约瑟夫环
- 题目1188:约瑟夫环
- 题目1188:约瑟夫环
- 题目1188:约瑟夫环
- 题目1188:约瑟夫环
- 九度OJ 题目1188:约瑟夫环
- 九度题目1188:约瑟夫环
- 【题目33】约瑟夫环
- 题目1189:还是约瑟夫环
- 题目1189:还是约瑟夫环
- 九度OJ—题目1188:约瑟夫环
- [链表]九度OJ 题目1188:约瑟夫环
- 约瑟夫环与POJ约瑟夫环题目解析
- 再做2个面试题目:(约瑟夫环)
- 华为笔试题目--约瑟夫环(Joseph)
- C++经典题目:约瑟夫环问题
- 一个类似于约瑟夫环的编程题目
- Java编程题目-1:约瑟夫环问题
- 关于C的小程序-3
- Android Arcgis入门(一) Arcgis开发配置
- javascript点击空白位置关闭弹出层/点击包含指定区域但不属于指定区域的部分响应点击事件
- [问题]第一行代码_生命周期_you need to use a theme.appcompat theme (or descendant) with this activity
- 学生管理系统
- 题目1188:约瑟夫环
- 美团实习生面试题
- 练习系统 实验四 red and black
- SSH整合问题
- CentOS 6.5上升级openssh7.5
- DB2客户端连接不上db2默认端口50000
- L1-001. Hello World
- 蓝桥杯 数组排序去重
- orcle排序