📌  相关文章
📜  执行给定的操作后,找出卡上最后剩下的数字

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

执行给定的操作后,找出卡上最后剩下的数字

给定一个整数N ,代表一副牌中的牌张数。一副牌从1N排序,其中1是最上面的卡片, N是底部。您从牌堆中取出最上面的牌并将其插入底部,然后扔出出现在牌堆顶部的下一张牌。你再次做同样的事情,直到剩下一张牌。任务是找到最后剩下的卡号。
例子:

Input: N = 4 
Output: 1
1 2 3 4
^     ^
Top   Bottom

Operation 1: 3 4 1 (1 got shifted to the bottom and 2 got removed)
Operation 2: 1 3 (3 got shifted and 4 got removed)
Operation 3: 1 (3 got removed after shifting 1)

Input: N = 10
Output: 5

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

方法:

  1. 首先在队列中插入从1N的数字。
  2. 现在,从队列中取出最前面的元素并在最后将其入队。
  3. 最后,弹出前面的元素。
  4. 打印队列中剩下的最后一个元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that will find the
// card number which is remaining
int remainingCard(int n)
{
    queue queCards;
 
    // Inserting all the numbers from 1 to n
    for (int i = 1; i <= n; i++)
        queCards.push(i);
 
    // While there are atleast two
    // elements in the queue
    while (((int)queCards.size()) >= 2) {
 
        // Push the front element at the back
        queCards.push(queCards.front());
 
        // Remove the front element
        queCards.pop();
 
        // Remove another element
        queCards.pop();
    }
 
    // Return the only element
    // left in the queue
    return queCards.front();
}
 
// Driver code
int main()
{
    int n = 10;
 
    cout << remainingCard(n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function that will find the
// card number which is remaining
static int remainingCard(int n)
{
    Queue queCards = new LinkedList<>();
 
    // Inserting all the numbers from 1 to n
    for (int i = 1; i <= n; i++)
    {
        queCards.add(i);
    }
 
    // While there are atleast two
    // elements in the queue
    while (((int) queCards.size()) >= 2)
    {
 
        // Push the front element at the back
        queCards.add(queCards.peek());
 
        // Remove the front element
        queCards.remove();
 
        // Remove another element
        queCards.remove();
    }
 
    // Return the only element
    // left in the queue
    return queCards.peek();
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
 
    System.out.println(remainingCard(n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
 
# Function that will find the
# card number which is remaining
def remainingCard(n):
 
    queCards = []
 
    # Inserting all the numbers from 1 to n
    for i in range(1, n + 1):
        queCards.append(i)
 
    # While there are atleast two
    # elements in the queue
    while (len(queCards) >= 2):
 
        # Push the front element at the back
        queCards.append(queCards[0]);
 
        # Remove the front element
        queCards.pop(0);
 
        # Remove another element
        queCards.pop(0);
 
    # Return the only element
    # left in the queue
    return queCards[0]
 
# Driver code
n = 10
print(remainingCard(n))
 
# This code is contributed by divyamohan123


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function that will find the
// card number which is remaining
static int remainingCard(int n)
{
    Queue queCards = new Queue();
 
    // Inserting all the numbers from 1 to n
    for (int i = 1; i <= n; i++)
    {
        queCards.Enqueue(i);
    }
 
    // While there are atleast two
    // elements in the queue
    while (((int) queCards.Count) >= 2)
    {
 
        // Push the front element at the back
        queCards.Enqueue(queCards.Peek());
 
        // Remove the front element
        queCards.Dequeue();
 
        // Remove another element
        queCards.Dequeue();
    }
 
    // Return the only element
    // left in the queue
    return queCards.Peek();
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 10;
 
    Console.WriteLine(remainingCard(n));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
5