这是从给定的数字集合中生成选择的最佳方式? [英] which is the best way to generate choices out of a given set of numbers?

查看:107
本文介绍了这是从给定的数字集合中生成选择的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果给定所有的选择在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将表示子集{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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆