这是产生选择出一组给定数字的最佳方式? [英] which is the best way to generate choices out of a given set of numbers?
本文介绍了这是产生选择出一组给定数字的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,如果它被赋予使1之间的所有选择,5,答案是这样的。
for example if it is given to make all the choices between 1 to 5 and the answer goes like this..
1,2,3,4,5,
1-2,1-3,1-4,1-5,2-3,2-4,2-5,3-4,3-5,4-5,
1-2-3,1-2-4,1-2-5,1-3-4,
.....,
1-2-3-4-5.
任何人都可以提出一个快速算法?
can anyone suggest a fast algorithm?
推荐答案
刚刚生成从一个所有的整数(或零,如果要包括空集)来的 2 ^ N - 1 的。你的集由在数组位指示。例如,如果你有5个元素{A,B,C,D,E} 6号= 00110将重新present子集{C,D}。
Just generate all the integers from one (or zero if you want to include the empty set) to 2^N - 1. Your sets are indicated by the set bits in the number. For example if you had 5 elements {A,B,C,D,E} the number 6 = 00110 would represent the subset {C,D}.
这篇关于这是产生选择出一组给定数字的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文