解题思路:
import java.util.Scanner; public class C1160 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ F(sc.nextInt(), sc.nextInt()); } sc.close(); } private static void F(int n, int m){ //构建循环链表 Node head = new Node(null, null); head.next = head; Node p = head; for(int i = 1; i <= n; i++){ p.next = new Node(i, p.next); p = p.next; } // count代表当前数的数字 int count = 0; p = head; while(head.next.next != head){ if(p.next == head) p = head; count++; //开始报数 if(count % m == 0){ //报数到第个m人 //System.out.println(p.next.data); p.next = p.next.next; //该人出圈 }else{ p = p.next; //往下继续循环到只剩一个人 } } System.out.println(head.next.data); } //链表节点类 private static class Node{ private Integer data; private Node next; public Node(Integer data, Node next){ this.data = data; this.next = next; } } }
注意事项:
参考代码:
0.0分
0 人评分