解题思路:
注意事项:
参考代码:
import java.util.Scanner; public class 模拟_奥运会开幕式 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); // 循环链表创建并且赋值(带头节点) Node head = new Node(0, null); for(int i = n ; i >= 1 ; i--) { Node t = new Node(i, null); t.setNext(head.getNext()); head.setNext(t); if(i == n) { t.setNext(head); } } //删到只剩两个节点为止,其中一个是头节点 Node p = head; Node pre = null; boolean flag = true; for(int i = 0 ; i < n - 1; i++) { for(int j = 0 ; j < m ; j++) { if(flag == true) { if(p.getIndex() == 0) { flag = false; } } pre = p; p = p.getNext(); if(flag == false && p.getIndex() == 0) { pre = p; p = p.getNext(); } } pre.setNext(p.getNext()); } //只剩下头节点和另外一个节点,另外一个节点就是我们要的答案 System.out.println(head.getNext().getIndex()); } } class Node { int index; Node next; public Node(int index, Node next) { super(); this.index = index; this.next = next; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题10.5 (C语言代码)浏览:1022 |
【蟠桃记】 (C语言代码)浏览:785 |
P1001 (C++代码)浏览:773 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:698 |
C语言程序设计教程(第三版)课后习题8.9 (C语言代码) 用函数传参的方法浏览:4064 |
【回文数(二)】 (C++代码)浏览:865 |
【出圈】 (C语言代码)浏览:553 |
回文串 (C语言代码)浏览:2846 |
水仙花 (C语言代码)浏览:1047 |
回文数字 (C语言代码)浏览:2509 |