这是产生选择出一组给定数字的最佳方式?这是、方式、数字

2023-09-12 23:34:57 作者:夜场、`嗨.

例如,如果它被赋予使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}.