使用 C++ 查找配对方式的数量
为了解决一个问题,其中n-现在每个人的人数可以是单身或成对出现,因此我们需要找到这些人可以配对的方式总数。
Input : 3
Output: 4
Explanation : [ {1}, {2}, {3},], [{1, 2}, {3}], [{1}, {2, 3}], [{1, 3}, {2}] these four ways are the only ways we can pa up these 3 people.
Input : 6
Output : 76寻找解决方案的方法
在这种方法中,我们将使用YoungTableau的公式来计算这个问题,我们将使用的公式是-
A[n] = A[n-1] + (n-1) * A[n-2]
示例
上述方法的C++代码
#include <bits/stdc++.h>
using namespace std;
int Young_Tableau(int n){
    int A[n + 1];//存储答案。
    A[1] = 1; //初始值
    A[2] = 2; //初始值
    for (int i = 3; i <= n; i++) { // using the formula of "Young Tableau" to calculate our answer
        A[i] = A[i - 1] + (i - 1) * A[i - 2];
    }
    return A[n]; //返回答案
}
int main(){
    int n = 6;
    cout << Young_Tableau(n);
    return 0;
}输出结果76
以上代码说明
在上面的方法中,我们只是应用了YoungTableau的公式,我们需要在这里找到前两个数字。现在我们可以将这些数字存储在数组索引中。通过订阅,我们可以为我们的公式获得它们的值,因此我们可以计算出我们的答案。
结论
在本教程中,我们解决了一个问题,以找到几种配对人的方法。我们还学习了针对此问题的C++程序以及解决此问题的完整方法(Normal)。我们可以用其他语言编写相同的程序,例如C、java、python和其他语言。我们希望本教程对您有所帮助。
