使用 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解决方法
在这种方法中,我们将使用**杨氏图表**公式来计算这个问题,我们将使用的公式是 -
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];// To store the answer.
A[1] = 1; // initial values
A[2] = 2; // initial values
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]; // returning the answer
}
int main(){
int n = 6;
cout << Young_Tableau(n);
return 0;
}输出
76
上述代码的解释
**在上述方法中,我们简单地应用了杨氏图表的公式,其中我们需要找到**前两个数字。现在我们可以将这些数字存储在数组索引中。通过订阅,我们可以获得公式的值,从而计算出答案。
结论
在本教程中,我们解决了一个问题,即查找配对人员的多种方法。我们还学习了此问题的 C++ 程序以及我们解决此问题的完整方法(常规)。我们可以在其他语言(如 C、Java、Python 等)中编写相同的程序。希望本教程对您有所帮助。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP