组合数计算
1 #include <iostream> 2 #include <unordered_map> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 7 using namespace std; 8 9 class Solution 10 { 11 private: 12 int need_typesize; 13 int total_typesize = 43; 14 int result = 0; 15 public: 16 int DFS(int number) 17 { 18 need_typesize = number; 19 vector<int> tmp; 20 _DFS(0,tmp); 21 return result; 22 } 23 void _DFS(int start,vector<int>& tmp) 24 { 25 if(tmp.size()==need_typesize) 26 { 27 result ++; 28 } 29 else if(tmp.size()<need_typesize) 30 { 31 for(int i = start;i < total_typesize;i ++) 32 { 33 // vector<int> tmp2 = tmp; 34 // _DFS(start+1,tmp2); 35 tmp.emplace_back(i); 36 _DFS(start+1,tmp); 37 tmp.pop_back(); 38 } 39 } 40 } 41 }; 42 43 int main() 44 { 45 Solution obj; 46 int result = obj.DFS(3); 47 cout << result << endl; 48 // for(auto v:result) 49 // { 50 // for(auto s:v) 51 // { 52 // cout << s << " "; 53 // } 54 // cout << endl; 55 // } 56 return 0; 57 }
1 #include <iostream> 2 #include <unordered_map> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 7 using namespace std; 8 9 class Solution 10 { 11 private: 12 int need_typesize; 13 int total_typesize = 23; 14 int result = 0; 15 public: 16 int DFS(int number) 17 { 18 need_typesize = number; 19 vector<int> tmp; 20 _DFS(tmp); 21 return result; 22 } 23 void _DFS(vector<int>& tmp) 24 { 25 if(tmp.size()==need_typesize) 26 { 27 result ++; 28 } 29 else if(tmp.size()<need_typesize) 30 { 31 for(int i = 0;i < total_typesize;i ++) 32 { 33 // vector<int> tmp2 = tmp; 34 // _DFS(start+1,tmp2); 35 tmp.emplace_back(i); 36 _DFS(tmp); 37 tmp.pop_back(); 38 } 39 } 40 } 41 }; 42 43 int main() 44 { 45 Solution obj; 46 int result = obj.DFS(6); 47 cout << result << endl; 48 // for(auto v:result) 49 // { 50 // for(auto s:v) 51 // { 52 // cout << s << " "; 53 // } 54 // cout << endl; 55 // } 56 return 0; 57 }
1 #include <iostream> 2 #include <unordered_map> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 7 using namespace std; 8 9 class Solution 10 { 11 private: 12 int need_typesize; 13 int total_typesize = 19; 14 int result = 0; 15 public: 16 int DFS(int number) 17 { 18 need_typesize = number; 19 vector<int> tmp; 20 _DFS(0,tmp); 21 return result; 22 } 23 void _DFS(int start,vector<int>& tmp) 24 { 25 if(tmp.size()==need_typesize) 26 { 27 result ++; 28 // for(auto d:tmp) 29 // cout << d << " "; 30 // cout << endl; 31 } 32 else if(tmp.size()<need_typesize) 33 { 34 for(int i = start;i < total_typesize;i ++) 35 { 36 // vector<int> tmp2 = tmp; 37 // _DFS(start+1,tmp2); 38 tmp.emplace_back(i); 39 _DFS(i,tmp); 40 tmp.pop_back(); 41 } 42 } 43 } 44 }; 45 46 int main() 47 { 48 Solution obj; 49 int result = obj.DFS(6); 50 cout << result << endl; 51 // for(auto v:result) 52 // { 53 // for(auto s:v) 54 // { 55 // cout << s << " "; 56 // } 57 // cout << endl; 58 // } 59 return 0; 60 }
1 #include <iostream> 2 #include <unordered_map> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 7 using namespace std; 8 9 class Solution 10 { 11 private: 12 int need_typesize; 13 int total_typesize = 19; 14 int result = 0; 15 public: 16 int DFS(int number) 17 { 18 need_typesize = number; 19 vector<int> tmp; 20 _DFS(0,tmp); 21 return result; 22 } 23 void _DFS(int start,vector<int>& tmp) 24 { 25 if(tmp.size()==need_typesize) 26 { 27 result ++; 28 // for(auto d:tmp) 29 // cout << d << " "; 30 // cout << endl; 31 } 32 else if(tmp.size()<need_typesize) 33 { 34 for(int i = start;i < total_typesize;i ++) 35 { 36 // vector<int> tmp2 = tmp; 37 // _DFS(start+1,tmp2); 38 tmp.emplace_back(i); 39 _DFS(i+1,tmp); 40 tmp.pop_back(); 41 } 42 } 43 } 44 }; 45 46 int main() 47 { 48 Solution obj; 49 int result = obj.DFS(6); 50 cout << result << endl; 51 // for(auto v:result) 52 // { 53 // for(auto s:v) 54 // { 55 // cout << s << " "; 56 // } 57 // cout << endl; 58 // } 59 return 0; 60 }
Solution类里的语句
int total_typesize = 43;
表明的是组合数中的C n底m中的n,而主函数调用中的语句
int result = obj.DFS(3);
表明的是组合数中的C n底m中的m。顺序和重复参考网站 https://zh.numberempire.com/combinatorialcalculator.php
另程序运行慢的原因是过了一遍所有的组合,想要存储全部组合的话,把变量result的类型改为vector<vector<int>>类型后,再把在Soluton类的语句
if(tmp.size()==need_typesize) { result ++; }
改为
if(tmp.size()==need_typesize) { result.push_back(tmp); }
即可。