📜  Josephus Circle 使用 STL 列表实现

📅  最后修改于: 2022-05-13 01:57:45.453000             🧑  作者: Mango

Josephus Circle 使用 STL 列表实现

有n人围成一圈等待被处决。计数从圆圈中的某个点开始,并以固定方向绕圆圈进行。每一步都会跳过一定数量的人,然后执行下一个人。消除围绕圆圈进行(随着被处决的人被移除,圆圈越来越小),直到只剩下最后一个人,他获得了自由。给定总人数 n 和数字 k,表示跳过 k-1 个人,并在圈内杀死第 k 个人。任务是选择初始圈中的位置,以便您是最后一个剩余的并生存。 (基于 0 的索引)。
例子 :

Input : Length of circle : n = 4
        Count to choose next : k = 2
Output : 0

Input : n = 5
        k = 3
Output : 3

我们已经讨论了这个问题的不同解决方案(这里,这里和这里)。在这篇文章中,讨论了使用列表容器的基于 C++ STL 的解决方案,该解决方案使用循环列表的思想。

C++
// CPP program to find last man standing
#include 
using namespace std;
 
int josephusCircle(int n, int k){
    listl; //creates a doubly linked list using stl container//
    for(int i=0;i1){
         
        for(int i=1;i


Java
// Java Code to find the last man Standing
public class GFG {
     
    // Node class to store data
    static class Node
    {
        public int data ;
        public Node next;
        public Node( int data )
        {
            this.data = data;
        }
    }
     
    /* Function to find the only person left
    after one in every m-th node is killed
    in a circle of n nodes */
    static void getJosephusPosition(int m, int n)
    {
        // Create a circular linked list of
        // size N.
        Node head = new Node(1);
        Node prev = head;
        for(int i = 2; i <= n; i++)
        {
            prev.next = new Node(i);
            prev = prev.next;
        }
         
        // Connect last node to first
        prev.next = head;
         
        /* while only one node is left in the
        linked list*/
        Node ptr1 = head, ptr2 = head;
         
        while(ptr1.next != ptr1)
        {
             
            // Find m-th node
            int count = 1;
            while(count != m)
            {
                ptr2 = ptr1;
                ptr1 = ptr1.next;
                count++;
            }
             
            /* Remove the m-th node */
            ptr2.next = ptr1.next;
            ptr1 = ptr2.next;
        }
        System.out.println ("Last person left standing " +
                 "(Josephus Position) is " + ptr1.data);
    }
     
    /* Driver program to test above functions */
    public static void main(String args[])
    {
        int n = 14, m = 2;
        getJosephusPosition(m, n);
    }
}


Python3
# Python3 program to find last man standing
 
# /* structure for a node in circular
#    linked list */
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
 
# /* Function to find the only person left
#    after one in every m-th node is killed
#    in a circle of n nodes */
def getJosephusPosition(m, n):
   
    # Create a circular linked list of
    # size N.
    head = Node(1)
    prev = head
    for i in range(2, n + 1):
        prev.next = Node(i)
        prev = prev.next
    prev.next = head # Connect last
                       #node to first
 
    #/* while only one node is left in the
    #linked list*/
    ptr1 = head
    ptr2 = head
    while (ptr1.next != ptr1):
        # Find m-th node
        count = 1
        while (count != m):
            ptr2 = ptr1
            ptr1 = ptr1.next
            count += 1
 
        # /* Remove the m-th node */
        ptr2.next = ptr1.next
        # free(ptr1)
        ptr1 = ptr2.next
 
    print("Last person left standing (Josephus Position) is ", ptr1.data)
 
# /* Driver program to test above functions */
if __name__ == '__main__':
    n = 14
    m = 2
    getJosephusPosition(m, n)
 
# This code is contributed by mohit kumar 29


C#
// C# Code to find the last man Standing
using System;
public class GFG {
     
    // Node class to store data
    class Node
    {
        public int data ;
        public Node next;
        public Node( int data )
        {
            this.data = data;
        }
    }
     
    /* Function to find the only person left
    after one in every m-th node is killed
    in a circle of n nodes */
    static void getJosephusPosition(int m, int n)
    {
        // Create a circular linked list of
        // size N.
        Node head = new Node(1);
        Node prev = head;
        for(int i = 2; i <= n; i++)
        {
            prev.next = new Node(i);
            prev = prev.next;
        }
         
        // Connect last node to first
        prev.next = head;
         
        /* while only one node is left in the
        linked list*/
        Node ptr1 = head, ptr2 = head;
         
        while(ptr1.next != ptr1)
        {
             
            // Find m-th node
            int count = 1;
            while(count != m)
            {
                ptr2 = ptr1;
                ptr1 = ptr1.next;
                count++;
            }
             
            /* Remove the m-th node */
            ptr2.next = ptr1.next;
            ptr1 = ptr2.next;
        }
        Console.WriteLine ("Last person left standing " +
                "(Josephus Position) is " + ptr1.data);
    }
     
    /* Driver program to test above functions */
     static public void Main(String []args)
    {
        int n = 14, m = 2;
        getJosephusPosition(m, n);
    }
}
//contributed by Arnab Kundu


Javascript


输出
12

输出 :



Last person left standing (Josephus Position) is 12 ( 0 based indexing )

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程