poj 2245 Lotto(dfs)

题目链接:http://poj.org/problem?id=2245

思路分析:无重复元素组合组合问题,使用暴力枚举法,注意剪枝条件。

 

代码如下:

#include <iostream>
using namespace std;

const int MAX_N = 15;
int n, k = 6;
int Set[MAX_N], A[MAX_N];

void Comnination( int deep, int index )
{
    if ( deep == k  )
    {
        int i;

        for ( i = 0; i < k - 1; ++i )
            cout << A[i] << " "; 
        cout << A[i] << endl;
    }
    else
    if ( index == n && deep != k )
        return;
    else
    {
        for ( int i = index; i < n; ++i )
        {
            A[deep] = Set[i];
            Comnination( deep + 1, i + 1 );
        }
    }
}

int main( )
{
    while ( cin >> n )
    {
        if ( n == 0 )
            break;

        for ( int i = 0; i < n; ++i )
            cin >> Set[i];

        Comnination( 0, 0 );
        cout << endl;
    }

    return 0;
}

 

你可能感兴趣的:(poj)