'commit'
This commit is contained in:
44
TangDou/LuoGuBook/20_8.cpp
Normal file
44
TangDou/LuoGuBook/20_8.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//功能:计算组合数公式
|
||||
typedef long long LL;
|
||||
const int N = 30;
|
||||
LL C[N][N];
|
||||
|
||||
//组合数公式
|
||||
void getC() {
|
||||
for (int i = 0; i <= N; i++) {
|
||||
//base case
|
||||
C[i][0] = C[i][i] = 1; //组合数C(n,0)=1,组合数C(n,n)=c(n,0)=1
|
||||
//递推生成其它组合数
|
||||
for (int j = 1; j < i; j++)
|
||||
//这个记忆的过程想一想杨辉三角就明白了,头顶+左上
|
||||
C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
|
||||
}
|
||||
}
|
||||
|
||||
int n, m;
|
||||
|
||||
/**
|
||||
* 测试用例:
|
||||
* 4 2
|
||||
*
|
||||
* 答案:
|
||||
* 6
|
||||
*/
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
getC();
|
||||
/**
|
||||
* 输出杨辉三角
|
||||
*/
|
||||
for (int i = 0; i <= 20; i++) {
|
||||
for (int j = 0; j <= i; j++)
|
||||
cout << C[i][j] << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
cout << C[n][m] << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user