今天我们要实现的就是在C++中获取所有可能的组合,还是直接上代码吧! 01.// zuhe.cpp : 定义控制台应用程序的入口点。 02.// 03. 04.#include "stdio.h" 05.#include "stdlib.h" 06.#include "iostream" 07.#include "vector" 08. 09.using namespace std; 10. 11.vector<int> team; 12.vector<vector<int》 Teams; 13. 14.void getAll(int * arr, int i, int k,int n); 15. 16.int main(int argc, char * argv[]) 17.{ 18. int arr[] = {1,2,3,4}; 19. //getAll(arr, 0, 2, 4); 20. 21. int n = 4; 22. for(int k = 1; k <= 4; k++) 23. { 24. getAll(arr, 0, k, n); 25. } 26. 27. system("pause"); 28. return 0; 29.} 30. 31.void getAll(int * arr, int i, int k,int n) 32.{ 33. if(team.size() == k) 34. { 35. Teams.push_back(team); 36. return; 37. } 38. 39. for(int j = i; j < n; j++) 40. { 41. team.push_back(arr[j]); 42. getAll(arr, j + 1, k, n); 43. team.pop_back(); 44. } 45.} |