从长度为n的数组(元素互不相同)中任意选择m个数的所有组合。
1 //从长度为n的数组(元素互不相同)中任意选择m个数的所有组合。 2 3 #include <iostream> 4 #include <vector> 5 6 using namespace std; 7 8 int arr[] = {1,2,3,4,5,6,7,8,9,10}; 9 int len = 10, m = 3; 10 11 void subArray(int num, vector<int> &vec, int curnum, int id) 12 { 13 int i; 14 15 if(curnum == num) 16 { 17 static int k = 1; 18 cout << k++ << ": "; 19 20 for(i = 0; i < vec.size(); i++) 21 { 22 cout << vec[i] << " "; 23 } 24 cout << endl; 25 return; 26 } 27 28 for(i = id; i < len; i++) 29 { 30 vec.push_back(arr[i]); 31 subArray(num, vec, curnum+1, i+1); 32 vec.pop_back(); 33 } 34 } 35 36 int main() 37 { 38 vector<int> vec; 39 subArray(m, vec, 0, 0); 40 return 0; 41 }