生成大小为k的所有子集从一组

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

我要生成大小为k的所有子集从一组。

I want to generate all the subsets of size k from a set.

例如:-say我有一组6片,我必须列出所有的子集,其中的元素的基数是3

eg:-say I have a set of 6 elements, I have to list all the subsets in which the cardinality of elements is 3.

我试图寻找解决方案,但这些都是code片段。 它是长久以来我做了编码,所以我觉得很难理解code和围绕它建立一个可执行程序。

I tried looking for solution,but those are code snippets. Its been long since I have done coding,so I find it hard to understand the code and construct a executable program around it.

在C或C ++将是非常有帮助的一个完整的可执行程序。 希望最佳的解决方案使用递归的。

A complete executable program in C or C++ will be quite helpful. Hoping of an optimal solution using recursion.

推荐答案

初始化位阵列(1<< NBITS)-1 ,然后用这个算法:

Initialize a bit array with (1<<nbits)-1 and then use this algorithm:

graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation

有关套比的最大整数大小,你仍然可以使用相同的算法,以你自己的类型。

For sets larger than the maximum integer size, you can still apply the same algorithm to your own type.

更多推荐

生成大小为k的所有子集从一组

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

发布评论

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

>www.elefans.com

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