📜  当一个人最多可以配对时计算配对

📅  最后修改于: 2021-09-17 07:11:56             🧑  作者: Mango

考虑一下关于 geeksforgeeks 实践的编码竞赛。现在他们有n个不同的参与者参加比赛。一个参与者最多可以与另一个参与者配对。我们需要计算n 个参与者参与编码竞赛的方式数量。
例子 :

Input : n = 2
Output : 2
2 shows that either both participant 
can pair themselves in one way or both 
of them can remain single.

Input : n = 3 
Output : 4
One way : Three participants remain single
Three More Ways : [(1, 2)(3)], [(1), (2,3)]
and [(1,3)(2)]

1) 每个参与者都可以与另一个参与者配对或保持单身。
2) 让我们考虑第X 个参与者,他可以保持单身或
他可以与来自[1, x-1] 的人配对。

C++
// Number of ways in which participant can take part.
#include
using namespace std;
 
int numberOfWays(int x)
{
    // Base condition
    if (x==0 || x==1)    
        return 1;
 
    // A participant can choose to consider
    // (1) Remains single. Number of people
    //     reduce to (x-1)
    // (2) Pairs with one of the (x-1) others.
    //     For every pairing, number of people
    //     reduce to (x-2).
    else
        return numberOfWays(x-1) +
               (x-1)*numberOfWays(x-2);
}
 
// Driver code
int main()
{
    int x = 3;
    cout << numberOfWays(x) << endl;
    return 0;
}


Java
// Number of ways in which
// participant can take part.
import java.io.*;
 
class GFG {
 
static int numberOfWays(int x)
{
    // Base condition
    if (x==0 || x==1)    
        return 1;
 
    // A participant can choose to consider
    // (1) Remains single. Number of people
    //     reduce to (x-1)
    // (2) Pairs with one of the (x-1) others.
    //     For every pairing, number of people
    //     reduce to (x-2).
    else
        return numberOfWays(x-1) +
            (x-1)*numberOfWays(x-2);
}
 
// Driver code
public static void main (String[] args) {
int x = 3;
System.out.println( numberOfWays(x));
     
    }
}
 
// This code is contributed by vt_m.


Python3
# Python program to find Number of ways
# in which participant can take part.
 
# Function to calculate number of ways.
def numberOfWays (x):
 
    # Base condition
    if x == 0 or x == 1:
        return 1
         
    # A participant can choose to consider
    # (1) Remains single. Number of people
    # reduce to (x-1)
    # (2) Pairs with one of the (x-1) others.
    # For every pairing, number of people
    # reduce to (x-2).
    else:
        return (numberOfWays(x-1) +
              (x-1) * numberOfWays(x-2))
 
# Driver code
x = 3
print (numberOfWays(x))
 
# This code is contributed by "Sharad_Bhardwaj"


C#
// Number of ways in which
// participant can take part.
using System;
 
class GFG {
 
    static int numberOfWays(int x)
    {
         
        // Base condition
        if (x == 0 || x == 1)
            return 1;
     
        // A participant can choose to
        // consider (1) Remains single.
        // Number of people reduce to
        // (x-1) (2) Pairs with one of
        // the (x-1) others. For every
        // pairing, number of people
        // reduce to (x-2).
        else
            return numberOfWays(x - 1) +
            (x - 1) * numberOfWays(x - 2);
    }
     
    // Driver code
    public static void Main ()
    {
        int x = 3;
         
        Console.WriteLine(numberOfWays(x));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Number of ways in which participant can take part.
#include
using namespace std;
 
int numberOfWays(int x)
{
    int dp[x+1];
    dp[0] = dp[1] = 1;
 
    for (int i=2; i<=x; i++)
       dp[i] = dp[i-1] + (i-1)*dp[i-2];
 
    return dp[x];
}
 
// Driver code
int main()
{
    int x = 3;
    cout << numberOfWays(x) << endl;
    return 0;
}


Java
// Number of ways in which
// participant can take part.
import java.io.*;
class GFG {
 
static int numberOfWays(int x)
{
    int dp[] = new int[x+1];
    dp[0] = dp[1] = 1;
 
    for (int i=2; i<=x; i++)
    dp[i] = dp[i-1] + (i-1)*dp[i-2];
 
    return dp[x];
}
 
// Driver code
public static void main (String[] args) {
int x = 3;
System.out.println(numberOfWays(x));
     
    }
}
// This code is contributed by vipinyadav15799


Python3
# Python program to find Number of ways
# in which participant can take part.
 
# Function to calculate number of ways.
def numberOfWays (x):
 
    dp=[]
    dp.append(1)
    dp.append(1)
    for i in range(2,x+1):
        dp.append(dp[i-1]+(i-1)*dp[i-2])
    return(dp[x])
     
 
# Driver code
x = 3
print (numberOfWays(x))
 
# This code is contributed by "Sharad_Bhardwaj"


C#
// Number of ways in which
// participant can take part.
using System;
 
class GFG {
 
    static int numberOfWays(int x)
    {
        int []dp = new int[x+1];
        dp[0] = dp[1] = 1;
     
        for (int i = 2; i <= x; i++)
            dp[i] = dp[i - 1] +
                 (i - 1) * dp[i - 2];
     
        return dp[x];
    }
     
    // Driver code
    public static void Main ()
    {
        int x = 3;
         
        Console.WriteLine(numberOfWays(x));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

4

由于存在重叠子问题,我们可以使用动态规划对其进行优化。

C++

// Number of ways in which participant can take part.
#include
using namespace std;
 
int numberOfWays(int x)
{
    int dp[x+1];
    dp[0] = dp[1] = 1;
 
    for (int i=2; i<=x; i++)
       dp[i] = dp[i-1] + (i-1)*dp[i-2];
 
    return dp[x];
}
 
// Driver code
int main()
{
    int x = 3;
    cout << numberOfWays(x) << endl;
    return 0;
}

Java

// Number of ways in which
// participant can take part.
import java.io.*;
class GFG {
 
static int numberOfWays(int x)
{
    int dp[] = new int[x+1];
    dp[0] = dp[1] = 1;
 
    for (int i=2; i<=x; i++)
    dp[i] = dp[i-1] + (i-1)*dp[i-2];
 
    return dp[x];
}
 
// Driver code
public static void main (String[] args) {
int x = 3;
System.out.println(numberOfWays(x));
     
    }
}
// This code is contributed by vipinyadav15799

蟒蛇3

# Python program to find Number of ways
# in which participant can take part.
 
# Function to calculate number of ways.
def numberOfWays (x):
 
    dp=[]
    dp.append(1)
    dp.append(1)
    for i in range(2,x+1):
        dp.append(dp[i-1]+(i-1)*dp[i-2])
    return(dp[x])
     
 
# Driver code
x = 3
print (numberOfWays(x))
 
# This code is contributed by "Sharad_Bhardwaj"

C#

// Number of ways in which
// participant can take part.
using System;
 
class GFG {
 
    static int numberOfWays(int x)
    {
        int []dp = new int[x+1];
        dp[0] = dp[1] = 1;
     
        for (int i = 2; i <= x; i++)
            dp[i] = dp[i - 1] +
                 (i - 1) * dp[i - 2];
     
        return dp[x];
    }
     
    // Driver code
    public static void Main ()
    {
        int x = 3;
         
        Console.WriteLine(numberOfWays(x));
    }
}
 
// This code is contributed by vt_m. 

PHP


Javascript


输出:

4

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