之前见到有人问输入10个互不相同的数字并分成5对有多少种分法,感觉这个问题有点意思就写了一个。
#include <iostream>
#include <array>
#include <vector>
#include <cassert>
template<int N>
std::array<std::array<int, 2>, N + 1>
append(const std::array<int, 2>& add, const std::array<std::array<int, 2>, N>& to){
std::array<std::array<int, 2>, N + 1> ret;
for (int i = 0; i < N; ++i){
ret[i] = to[i];
}
ret[N] = add;
return ret;
}
template<int N>
std::array<int, N - 1> pickout(const std::array<int, N>& lhs, int item){
std::array<int, N - 1> ret;
for (int i = 0, j = 0; i < N - 1; ++i, ++j){
if (lhs[i] == item){
++j;
}
ret[i] = lhs[j];
}
return ret;
}
template<int N>
std::vector<std::array<std::array<int, 2>, N / 2> >
apply(
const std::vector<std::array<std::array<int, 2>, N / 2> >& tmpl,
const std::array<int, N>& data){
static_assert(N % 2 == 0, "should be multiply of 2");
std::vector<std::array<std::array<int, 2>, N / 2> > ret;
for (auto entry : tmpl){
std::array<std::array<int, 2>, N / 2> record;
for (int i = 0; i < N / 2; ++i){
record[i] = { { data[entry[i][0]], data[entry[i][1]] } };
}
ret.push_back(record);
}
return ret;
}
template<int N>
std::vector<std::array<std::array<int, 2>, N / 2> >
get(){
static_assert(N % 2 == 0, "should be multiply of 2");
std::vector<std::array<std::array<int, 2>, N / 2> > ret;
auto decay = get<N - 2>();
std::array<int, N> data;
for (int i = 0; i < N; ++i)data[i] = i;
for (int i = 1; i < N; ++i){
std::array<int, N - 2> d = pickout(pickout(data, 0), i);
for (auto tail : apply(decay, d)){
ret.push_back(append(std::array<int, 2>{{ 0, i }}, tail));
}
}
return ret;
}
template<>
std::vector<std::array<std::array<int, 2>, 1> >
get<2>(){
return std::vector<std::array<std::array<int, 2>, 1> >{
std::array<std::array<int, 2>, 1>{{
std::array<int, 2>{{0, 1}}
}}
};
}
template<typename T>
std::ostream& operator << (std::ostream& out, const std::vector<T>& rhs){
out << "[ ";
if (!rhs.empty())out << rhs[0];
for (int i = 1; i < rhs.size(); ++i){
out << ", " << rhs[i];
}
return out << " ]";
}
template<typename T, int N>
std::ostream& operator << (std::ostream& out, const std::array<T, N>& rhs){
out << "[ " << rhs[0];
for (int i = 1; i < N; ++i){
out << ", " << rhs[i];
}
return out << " ]";
}
int main(){
std::array<int, 10> arr;
std::cout << "input 10 numbers:" << std::endl;
for (int i = 0; i < 10; ++i){
std::cin >> arr[i];
}
for (auto i : apply(get<10>(), arr)){
std::cout << i << std::endl;
}
}