📜  幸运的活着的人围成一圈|剑之谜的代码解决方案

📅  最后修改于: 2021-04-29 08:28:16             🧑  作者: Mango

给定n个站在第一个拥有剑的圈子中的人,找到圈子中最幸运的人,如果从拥有一把剑的第一个士兵中每个人都必须杀死下一个士兵并将剑移交给下一个士兵,那么该士兵将杀死相邻的士兵,然后将剑移交给下一位士兵,以使一名士兵仍在这场战争中被任何人杀死。
先决条件:拼图81 |一圈带枪谜题的100人
例子 :

方法:想法是使用循环链表。根据士兵N的数量创建一个循环链表。按照规则,您必须杀死相邻的士兵并将剑移交给下一个士兵,而后者又将杀死他的相邻士兵并将剑移交给下一个士兵。因此,在循环链表中,相邻的士兵被打死,其余的士兵以循环的方式相互对抗,只有一名士兵被任何人杀害,幸存下来。

C++
// CPP code to find the luckiest person
#include 
using namespace std;
 
// Node structure
struct Node {
    int data;
    struct Node* next;
};
 
Node *newNode(int data)
{
   Node *node = new Node;
   node->data = data;
   node->next = NULL;
   return node;
}
 
// Function to find the luckiest person
int alivesol(int Num)
{
    if (Num == 1)
        return 1;
 
    // Create a single node circular
    // linked list.
    Node *last = newNode(1);
    last->next = last;
     
    for (int i = 2; i <= Num; i++) {
        Node *temp = newNode(i);
        temp->next = last->next;       
        last->next = temp;
        last = temp;    
    }
 
    // Starting from first soldier.
    Node *curr = last->next;
 
    // condition for evaluating the existence
    // of single soldier who is not killed.
    Node *temp;
    while (curr->next != curr) {
        temp = curr;
        curr = curr->next;
        temp->next = curr->next;
 
        // deleting soldier from the circular
        // list who is killed in the fight.
        delete curr;
        temp = temp->next;
        curr = temp;
    }
 
    // Returning the Luckiest soldier who
    // remains alive.
    int res = temp->data;
    delete temp;
     
    return res;
}
 
// Driver code
int main()
{
    int N = 100;
    cout << alivesol(N) << endl;
    return 0;
}


Java
// Java code to find the luckiest person
class GFG
{
     
// Node structure
static class Node
{
    int data;
    Node next;
};
 
static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.next = null;
    return node;
}
 
// Function to find the luckiest person
static int alivesol(int Num)
{
    if (Num == 1)
        return 1;
 
    // Create a single node circular
    // linked list.
    Node last = newNode(1);
    last.next = last;
     
    for (int i = 2; i <= Num; i++)
    {
        Node temp = newNode(i);
        temp.next = last.next;    
        last.next = temp;
        last = temp;    
    }
 
    // Starting from first soldier.
    Node curr = last.next;
 
    // condition for evaluating the existence
    // of single soldier who is not killed.
    Node temp = new Node();
    while (curr.next != curr)
    {
        temp = curr;
        curr = curr.next;
        temp.next = curr.next;
 
        // deleting soldier from the circular
        // list who is killed in the fight.
        temp = temp.next;
        curr = temp;
    }
 
    // Returning the Luckiest soldier who
    // remains alive.
    int res = temp.data;
     
    return res;
}
 
// Driver code
public static void main(String args[])
{
    int N = 100;
    System.out.println( alivesol(N) );
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 code to find the luckiest person
  
# Node structure
class Node:
     
    def __init__(self, data):
        self.data = data
        self.next = None
     
def newNode(data): 
    node = Node(data)
    return node
  
# Function to find the luckiest person
def alivesol( Num):
    if (Num == 1):
        return 1;
  
    # Create a single node circular
    # linked list.
    last = newNode(1);
    last.next = last;
     
    for i in range(2, Num + 1):       
        temp = newNode(i);
        temp.next = last.next;       
        last.next = temp;
        last = temp;    
      
    # Starting from first soldier.
    curr = last.next;
  
    # condition for evaluating the existence
    # of single soldier who is not killed.
    temp = None
     
    while (curr.next != curr):       
        temp = curr;
        curr = curr.next;
        temp.next = curr.next;
  
        # deleting soldier from the circular
        # list who is killed in the fight.
        del curr;
        temp = temp.next;
        curr = temp;
  
    # Returning the Luckiest soldier who
    # remains alive.
    res = temp.data;
    del temp;    
    return res;
  
# Driver code
if __name__=='__main__':
     
    N = 100;
    print(alivesol(N))
    
  # This code is contributed by rutvik_56


C#
// C# code to find the luckiest person
using System;
 
class GFG
{
     
// Node structure
public class Node
{
    public int data;
    public Node next;
};
 
static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.next = null;
    return node;
}
 
// Function to find the luckiest person
static int alivesol(int Num)
{
    if (Num == 1)
        return 1;
 
    // Create a single node circular
    // linked list.
    Node last = newNode(1);
    last.next = last;
     
    for (int i = 2; i <= Num; i++)
    {
        Node tem = newNode(i);
        tem.next = last.next;
        last.next = tem;
        last = tem;
    }
 
    // Starting from first soldier.
    Node curr = last.next;
 
    // condition for evaluating the existence
    // of single soldier who is not killed.
    Node tem1 = new Node();
    while (curr.next != curr)
    {
        tem1 = curr;
        curr = curr.next;
        tem1.next = curr.next;
 
        // deleting soldier from the circular
        // list who is killed in the fight.
        tem1 = tem1.next;
        curr = tem1;
    }
 
    // Returning the Luckiest soldier who
    // remains alive.
    int res = tem1.data;
     
    return res;
}
 
// Driver code
public static void Main(String []args)
{
    int N = 100;
    Console.WriteLine( alivesol(N) );
}
}
 
// This code is contributed by Arnab Kundu


输出:
73