📜  结对方式的数量

📅  最后修改于: 2021-06-25 16:28:05             🧑  作者: Mango

考虑到一个聚会中有p个人。每个人都可以作为一个人参加舞蹈,也可以与其他任何人一起参加舞蹈。任务是找到p人参加舞蹈的各种不同方式。
例子:

Input : p = 3
Output : 4
Let the three people be P1, P2 and P3
Different ways are: {P1, P2, P3}, {{P1, P2}, P3},
{{P1, P3}, P2} and {{P2, P3}, P1}.

Input : p = 2
Output : 2
The groups are: {P1, P2} and {{P1, P2}}.

方法:想法是使用动态编程来解决此问题。有两种情况:一个人作为一个人或一对参加舞蹈。对于第一种情况,问题减少到为p-1人找到解决方案。对于第二种情况,存在p-1个选项来选择要配对的个人,并且在选择了个人进行配对之后,由于p中的两个人已经配对,因此问题减少了为p-2人找到解决方案。
因此,dp的公式为:

dp[p] = dp[p-1] + (p-1) * dp[p-2].

下面是上述方法的实现:

C++
// CPP program to find number of ways to
// pair people in party
 
#include 
using namespace std;
 
// Function to find number of ways to
// pair people in party
int findWaysToPair(int p)
{
    // To store count of number of ways.
    int dp[p + 1];
 
    dp[1] = 1;
    dp[2] = 2;
 
    // Using the recurrence defined find
    // count for different values of p.
    for (int i = 3; i <= p; i++) {
        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
    }
 
    return dp[p];
}
 
// Driver code
int main()
{
    int p = 3;
    cout << findWaysToPair(p);
    return 0;
}


Java
// Java program to find number of ways to
// pair people in party
 
class GFG
{
     
// Function to find number of ways to
// pair people in party
static int findWaysToPair(int p)
{
    // To store count of number of ways.
    int dp[] = new int[p + 1];
 
    dp[1] = 1;
    dp[2] = 2;
 
    // Using the recurrence defined find
    // count for different values of p.
    for (int i = 3; i <= p; i++)
    {
        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
    }
 
    return dp[p];
}
 
// Driver code
public static void main(String args[])
{
    int p = 3;
    System.out.println(findWaysToPair(p));
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 program to find number of
# ways to pair people in party
 
# Function to find number of ways
# to pair people in party
def findWays(p):
 
    # To store count of number of ways.
    dp = [0] * (p + 1)
    dp[1] = 1
    dp[2] = 2
 
    # Using the recurrence defined find
    # count for different values of p.
    for i in range(3, p + 1):
        dp[i] = (dp[i - 1] +
                   (i - 1) * dp[i - 2])
    return dp[p]
 
# Driver code
p = 3
print(findWays(p))
 
# This code is contributed by Shrikant13


C#
// C# program to find number of ways to
// pair people in party
using System;
 
class GFG
{
 
// Function to find number of ways to
// pair people in party
public static int findWaysToPair(int p)
{
    // To store count of number of ways.
    int[] dp = new int[p + 1];
 
    dp[1] = 1;
    dp[2] = 2;
 
    // Using the recurrence defined find
    // count for different values of p.
    for (int i = 3; i <= p; i++)
    {
        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
    }
    return dp[p];
}
 
// Driver code
public static void Main(string[] args)
{
    int p = 3;
    Console.WriteLine(findWaysToPair(p));
}
}
 
// This code is contributed by shrikanth13


PHP


Javascript


输出:
4

时间复杂度: O(p)
辅助空间: O(p)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。