Skip to content

Instantly share code, notes, and snippets.

@OriLiMu
Created January 28, 2025 14:01
Show Gist options
  • Save OriLiMu/902d85da026b7fb154fa328666d52ae6 to your computer and use it in GitHub Desktop.
Save OriLiMu/902d85da026b7fb154fa328666d52ae6 to your computer and use it in GitHub Desktop.
组合数
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstddef>
#include <iostream>
#include <type_traits>
#include <vector>
using namespace std;
class Solution {
public:
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
int result = 1;
while (n > 0) {
result *= n--;
}
return result;
}
int combinatorial(int k, int n) {
return factorial(n) / (factorial(k) * factorial(n - k));
}
vector<int> getRow(int rowIndex) {
vector<int> result;
if (rowIndex == 0) {
return {1};
}
for (int i = 0; i < rowIndex + 1; i++) {
result.push_back(combinatorial(i, rowIndex));
}
return result;
}
};
int main() {
Solution s;
vector<int> result = s.getRow(3);
cout << "this is the test" << endl;
for (int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
}
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment