📜  随机播放一副纸牌并回答查询

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

给定一包2 ^ N张卡片(0…2 ^ N – 1),请按N步对其进行洗牌。在步骤k(0 例子:

Input : N = 3 (Size = 2^N), Key = 3
Output : 6
Explanation : 
Pack :      0 1 2 3 4 5 6 7
Shuffle 1 : 0 2 4 6|1 3 5 7
Shuffle 2 : 0 4|2 6|1 5|3 7
Card at index 3 : 6

方法1:我们可以简单地模拟整个过程,并在完成所有N次混洗后找到纸牌的确切顺序。
时间复杂度:O(N * 2 ^ N)
方法2:
让我们尝试找到Key的二进制表示形式和最终答案,并尝试基于它来发现一些观察结果。
令N = 3
下表是:
关键ANS
000 000
001 100
010 010
011110
100 001
101101
110011
111111
显而易见,答案与Key的二进制表示形式相反。

C++
// C++ program to find the card at given index
// after N shuffles
#include 
using namespace std;
 
// function to find card at given index
void shuffle(int N, int key)
{
 
    // Answer will be reversal of N bits from MSB
    unsigned int NO_OF_BITS = N;
    unsigned int reverse_num = 0, temp;
 
    // Calculating the reverse binary representation
    for (int i = 0; i < NO_OF_BITS; i++) {
        temp = (key & (1 << i));
        if (temp)
            reverse_num |= (1 << ((NO_OF_BITS - 1) - i));
    }
 
    // Printing the result
    cout << reverse_num;
}
 
// driver code
int main()
{
    // No. of Shuffle Steps
    int N = 3;
 
    // Key position
    unsigned int key = 3;
 
    shuffle(N, key);
    return 0;
}


Java
// Java program to find the card at given index
// after N shuffles
class GFG {
     
    // function to find card at given index
    static void shuffle(int N, int key)
    {
     
        // Answer will be reversal of N bits from MSB
        int NO_OF_BITS = N;
        int reverse_num = 0, temp;
     
        // Calculating the reverse binary representation
        for (int i = 0; i < NO_OF_BITS; i++) {
            temp = (key & (1 << i));
            if (temp>0)
                reverse_num |= (1 << ((NO_OF_BITS - 1) - i));
        }
     
        // Printing the result
        System.out.print(reverse_num);
    }
     
    //Driver code
    public static void main (String[] args)
    {
         
        // No. of Shuffle Steps
        int N = 3;
     
        // Key position
        int key = 3;
     
        shuffle(N, key);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find the card
# at given index after N shuffles
 
# Function to find card at given index
def shuffle(N, key):
 
    # Answer will be reversal
    # of N bits from MSB
    NO_OF_BITS = N
    reverse_num = 0
 
    # Calculating the reverse binary representation
    for i in range(NO_OF_BITS):
        temp = (key & (1 << i))
        if (temp):
            reverse_num |= (1 << ((NO_OF_BITS - 1) - i))
     
    # Printing the result
    print(reverse_num)
 
# Driver code
 
# No. of Shuffle Steps
N = 3
 
# Key position
key = 3
shuffle(N, key)
 
# This code is contributed by Anant Agarwal.


C#
// C# program to find the card at given index
// after N shuffles
using System;
 
class GFG {
     
    // function to find card at given index
    static void shuffle(int N, int key)
    {
      
        // Answer will be reversal of N bits from MSB
        int NO_OF_BITS = N;
        int reverse_num = 0, temp;
      
        // Calculating the reverse binary representation
        for (int i = 0; i < NO_OF_BITS; i++) {
            temp = (key & (1 << i));
            if (temp > 0)
                reverse_num |= (1 << ((NO_OF_BITS - 1) - i));
        }
      
        // Printing the result
        Console.Write(reverse_num);
    }
     
    //Driver code
    public static void Main()
    {
         
        // No. of Shuffle Steps
        int N = 3;
     
        // Key position
        int key = 3;
     
        shuffle(N, key);
    }
}
 
// This code is contributed by Anant Agarwal.


Javascript


输出:

6