最好递归算法打印出来的子集 [英] Best recursive algorithm to print out subsets

查看:168
本文介绍了最好递归算法打印出来的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个很好的递归算法,打印出一系列的子集。 例如

I'm trying to find a good recursive algorithm to print out the subsets of a set. For example

5号:给出集合{1,2,3,4,5}和关闭长度为3的子集给出了这样的输出:

{5,4,3}
{5,4,2}
{5,4,1}
{5,3,2}
{5,3,1}
{5,2,1}
{4,3,2}
{4,3,1}
{4,2,1}
{3,2,1}

我试过很多东西,但它不工作。在互联网上所有的例子都是用套算法,但我想我自己写的,学习的目的。

I've tried many things but it doesn't work. On the internet all the examples are with sets algorithms but I want to write my own, for learning purposes.

可能有人帮助我?

亲切的问候,

推荐答案

最后,它的工作原理:

public static void Dvz(String s, int x, int y){
    int i;
    if(y > 0)
      for(i = x; i >= y; i--)
      Dvz(s+i, i-1, y-1);
    else
      System.out.println(s);

  }

这篇关于最好递归算法打印出来的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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