编号为0到n-1的n个人围成一圈。从编号为0的人开始报数1,依次下去,报到m的人离开,问最后留下的一个人,编号是多少?
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int k = cin.nextInt(); ListNode head = null; ListNode curr = null; ListNode help = null; // for (int i = 0; i < n; i++) { ListNode listNode = new ListNode(i); if (i == 0) { head = listNode; head.next = head; curr = head; } else { curr.next = listNode; listNode.next = head; curr = listNode; } if (i == n - 1) { help = curr; } } while (head != help) { for (int i = 0; i < k - 1; i++) { head = head.next; help = help.next; } head = head.next; help.next = head; } System.out.println(head.n); } } class ListNode { int n; ListNode next; public ListNode(int i) { this.n = i; } }