求从n个数组任意选取一个元素的所有组合,对于这个问题,我们在直观上感觉很容易,但是用程序实现时则发现用for循环解决不了问题,因为n是随意的。
在这里,我们用递归的思想,对于数据[1, 3, 4]; [2, 5]; [6, 7];我们可以
1.将1,2,6压入栈中,然后输出栈中所有元素,之后弹出6
2.压入7,输出栈中所有元素,之后弹出7
3.弹出2,压入5,再压入6,然后输出栈中所有元素,之后弹出6
4.压入7,输出栈中所有元素,之后弹出7
以此类推,即可得到所有组合。
具体程序实现如下:
#include
<
iostream
>
#include < vector >
#include < algorithm >
using namespace std;
void get_result_in_vector(vector < vector < int > > & vec, int N,vector < int > & tmp, vector < vector < int >>& tmp_result)
{
for ( int i = 0 ;i < vec[N].size(); ++ i)
{
tmp.push_back(vec[N][i]);
if (N < vec.size() - 1 )
{
get_result_in_vector(vec,N + 1 ,tmp, tmp_result);
}
else
{
vector < int > one_result;
for ( int i = 0 ;i < tmp.size(); ++ i)
{
one_result.push_back(tmp.at(i));
}
tmp_result.push_back(one_result);
}
tmp.pop_back();
}
}
void get_all_combination(vector < vector < int >>& vec, int result[][ 3 ])
{
vector < int > tmp_vec;
vector < vector < int >> tmp_result;
get_result_in_vector(vec, 0 ,tmp_vec, tmp_result);
// 将vector中的数据转化为数组保存
for ( int i = 0 ; i < tmp_result.size(); i ++ )
{
for ( int j = 0 ; j < tmp_result.at(i).size(); j ++ )
{
result[i][j] = tmp_result.at(i).at(j);
}
}
}
int main()
{
int arr1[] = { 1 , 3 , 4 };
int arr2[] = { 2 , 5 };
int arr3[] = { 6 , 7 };
const int first_dimension = sizeof (arr1) / sizeof (arr1[ 0 ]) * sizeof (arr2) / sizeof (arr2[ 0 ]) * sizeof (arr3) / sizeof (arr3[ 0 ]);
// 所有组合的结果
int result[first_dimension][ 3 ];
vector < vector < int >> vec;
vec.push_back(vector < int > (arr1,arr1 + sizeof (arr1) / sizeof (arr1[ 0 ])));
vec.push_back(vector < int > (arr2,arr2 + sizeof (arr2) / sizeof (arr2[ 0 ])));
vec.push_back(vector < int > (arr3,arr3 + sizeof (arr3) / sizeof (arr3[ 0 ])));
get_all_combination(vec, result);
// 输出结果
for ( int i = 0 ; i < first_dimension; i ++ )
{
for ( int j = 0 ; j < 3 ; j ++ )
{
cout << result[i][j] << " \t " ;
}
cout << endl;
}
return 0 ;
}
#include < vector >
#include < algorithm >
using namespace std;
void get_result_in_vector(vector < vector < int > > & vec, int N,vector < int > & tmp, vector < vector < int >>& tmp_result)
{
for ( int i = 0 ;i < vec[N].size(); ++ i)
{
tmp.push_back(vec[N][i]);
if (N < vec.size() - 1 )
{
get_result_in_vector(vec,N + 1 ,tmp, tmp_result);
}
else
{
vector < int > one_result;
for ( int i = 0 ;i < tmp.size(); ++ i)
{
one_result.push_back(tmp.at(i));
}
tmp_result.push_back(one_result);
}
tmp.pop_back();
}
}
void get_all_combination(vector < vector < int >>& vec, int result[][ 3 ])
{
vector < int > tmp_vec;
vector < vector < int >> tmp_result;
get_result_in_vector(vec, 0 ,tmp_vec, tmp_result);
// 将vector中的数据转化为数组保存
for ( int i = 0 ; i < tmp_result.size(); i ++ )
{
for ( int j = 0 ; j < tmp_result.at(i).size(); j ++ )
{
result[i][j] = tmp_result.at(i).at(j);
}
}
}
int main()
{
int arr1[] = { 1 , 3 , 4 };
int arr2[] = { 2 , 5 };
int arr3[] = { 6 , 7 };
const int first_dimension = sizeof (arr1) / sizeof (arr1[ 0 ]) * sizeof (arr2) / sizeof (arr2[ 0 ]) * sizeof (arr3) / sizeof (arr3[ 0 ]);
// 所有组合的结果
int result[first_dimension][ 3 ];
vector < vector < int >> vec;
vec.push_back(vector < int > (arr1,arr1 + sizeof (arr1) / sizeof (arr1[ 0 ])));
vec.push_back(vector < int > (arr2,arr2 + sizeof (arr2) / sizeof (arr2[ 0 ])));
vec.push_back(vector < int > (arr3,arr3 + sizeof (arr3) / sizeof (arr3[ 0 ])));
get_all_combination(vec, result);
// 输出结果
for ( int i = 0 ; i < first_dimension; i ++ )
{
for ( int j = 0 ; j < 3 ; j ++ )
{
cout << result[i][j] << " \t " ;
}
cout << endl;
}
return 0 ;
}