zoukankan      html  css  js  c++  java
  • 【递归】n个数的集合的所有子集

    输入:

    3

    1 2 3

    输出:

    1

    1 2

    1 2 3

    1 3

    2

    2 3

    3

    Delphi代码:

    program full_combination;

    const
    max_n = 10;

    var
    n: integer;
    rcd, num: array[0..max_n] of integer;

    procedure full_combination(index, p: integer);
    var
    i: integer;
    begin
    //每次都输出
    if index > 0 then
    begin
    for i := 0 to index - 2 do
    Write(rcd[i], ' ');
    writeln(rcd[index - 1]);
    end;

    for i := p to n - 1 do
    begin
    rcd[index] := num[i];
    full_combination(index + 1, i + 1);
    end;
    end;

    var
    i: integer;
    begin
    while not seekeof do
    begin
    readln(n);
    for i := 0 to n - 1 do
    Read(num[i]);
    full_combination(0, 0);
    end;
    end.

    C++代码:

    #include <stdio.h>
    #define MAX_N 10

    int rcd[MAX_N], num[MAX_N];
    int n;

    void full_combination(int index, int p)
    {
    int i;
    for (i=0; i<index; i++)
    {
    printf("%d", rcd[i]);
    if (i<index-1)
    {
    printf(" ");
    }
    }
    printf("\n");

    for (i=p; i<n; i++)
    {
    rcd[index] = num[i];
    full_combination(index+1, i+1);
    }
    }

    int read_data()
    {
    int i;
    if (scanf("%d", &n)==EOF)
    {
    return 0;
    }
    for (i=0; i<n; i++)
    {
    scanf("%d", &num[i]);
    }
    return 1;
    }

    void main()
    {
    while(read_data())
    {
    full_combination(0,0);
    }
    }




  • 相关阅读:
    CollectionView网格布局
    UICollectionView基础/UICollectionViewCell的四种创建方式
    shiro
    jquery添加属性的方法
    ssm+activiti+maven
    Activiti流程定义部署方式
    00--工作流
    04--activiti demo
    02--Activiti初始化表
    01--安装Activiti流程设计器eclipse插件
  • 原文地址:https://www.cnblogs.com/wouldguan/p/2435229.html
Copyright © 2011-2022 走看看