poj2245

dfs

View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define maxn 20

int n;
int f[maxn];
int g[maxn];

void input()
{
    for (int i = 0; i < n; i++)
        scanf("%d", &f[i]);
}

void output()
{
    for (int i = 0; i < 5; i++)
        printf("%d ", g[i]);
    printf("%d\n", g[5]);
}

void dfs(int step, int p)
{
    if (step == 6)
    {
        output();
        return;
    }
    for (int i = p; i < n - 5 + step; i++)
    {
        g[step] = f[i];
        dfs(step + 1, i + 1);
    }
}

int main()
{
//    freopen("t.txt", "r", stdin);
    bool first = true;
    while (scanf("%d", &n), n)
    {
        if (first)
            first = false;
        else
            putchar('\n');
        input();
        dfs(0, 0);
    }
    return 0;
}

 

posted @ 2013-01-11 19:48  金海峰  阅读(111)  评论(0编辑  收藏  举报