输入10个互不相同的数字并分成5对,问有多少种分法。

之前见到有人问输入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;
	}
}

  

转载于:https://www.cnblogs.com/bombless/p/3820965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值