zoukankan      html  css  js  c++  java
  • 输入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;
    	}
    }
    

      

  • 相关阅读:
    轻量化ViewControllers,读文章做的总结
    项目开发一点总结和吐槽
    简要解析XMPP框架及iOSObjectiveC的使用
    分享,iOS国家手机区号代码.plist
    Storyboard中使用UIscrollView添加约束的开发总结
    朋友由一道面试题想到的递归的解决办法
    最精简SQL教程,查漏补疑
    C#操作注册表的两种方法及引申出调用脚本的通用方法
    nunit的最精简教程
    一些基础知识
  • 原文地址:https://www.cnblogs.com/bombless/p/3820965.html
Copyright © 2011-2022 走看看