生成项目的所有唯一组合

编程入门 行业动态 更新时间:2024-10-28 10:36:30
本文介绍了生成项目的所有唯一组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图生成所有可能的项目唯一组合.

I trying to generate all possible unique combination of items.

Ex: item1, item2, item3 Combinations: item1+item2+item3 item1+item2 item1+item3 item2+item3 item1 item2 item3

我不知道如何解决这个问题?

I am unable to get an idea on how to solve this?

for(int i=0;i<size;i++){ for(int j=i+1;j<size;j++){ System.out.println(list.item(i)+list.item(j)); } }

上面的代码当然适用于两个元素的所有唯一组合.但是不适用于3个元素对或更多..

The above code certainly works for all unique combination of two elements. But not for 3 element pair and more..

推荐答案

如果您有N个项目,则计数从1到2 ^ N-1.每个数字都表示一个组合,如下所示:如果设置了位0(最低有效位),则item1在组合中.如果设置了位1,则item2处于组合中,依此类推.

If you have N items, count from 1 to 2^N-1. Each number represents a combination, like so: if bit 0 (the least significant bit) is set, item1 is in the combination. If bit 1 is set, item2 is in the combination, and so on.

如果您不想要1个项目的组合,请从3开始计数,并忽略所有2的幂的组合(4、8、16等).

If you don't want 1-item combinations, start counting at 3, and ignore all the combinations that are a power of 2 (4, 8, 16, etc...).

更多推荐

生成项目的所有唯一组合

本文发布于:2023-10-19 18:30:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!