Problem description :( Joseph Ring )

sample input :

The code is as follows : Using linked list simulation
import java.util.Scanner; public class FindTheWinner { // Find the winner of the game //1 <= k <=
n <= 500 class Node{ int data; Node next; public Node(int data) { this.data =
data; } } public int findTheWinner(int n, int k) {// Use the circular list to solve the problem if (k==1) return n
; Node first=new Node(1); Node p=first; for (int i = 2; i <=n ; i++) { p.next=
new Node(i); p=p.next; } p.next=first;// end to end Node p1=first; Node pre=null; int
cnt=1; while (p1!=pre){ pre=p1; p1=p1.next; cnt++; if (cnt==k){ pre.next=p1.next
;// delete p1 Node referred to cnt=1; p1=pre.next;// take p1 Point to next node } } return p1.data; } public static
void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner
.nextInt(); int k=scanner.nextInt(); FindTheWinner findTheWinner=new
FindTheWinner(); System.out.println(findTheWinner.findTheWinner(n,k)); } }
The operation results are as follows :

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq