使用STL输出组合序列 + UVa 441 Lotto

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=382


/*0.009s*/

#include
using namespace std;
const int mx = 15;

int a[15];
bool ok[15];

/*
输出从n个数中选m个数的组合字典序
以n=7,m=3为例
首先构造ok=1110000
然后ok的前一个排列为1101000
再前一个排列为1100100...1100001
然后是1011000...直到0000111
*/
void printfC(int n, int m)
{
	int i;
	for (i = 0; i < m; ++i) ok[i] = true;
	for (; i < n; ++i) ok[i] = false;
	do
	{
		for (i = 0; i < n; ++i)
			if (ok[i]) {printf("%d", a[i]); break;}
		for (++i; i < n; ++i)
			if (ok[i]) printf(" %d", a[i]);
		putchar(10);
	}
	while (prev_permutation(ok, ok + n));
}

int main()
{
	int n, i;
	bool ok = false;
	while (scanf("%d", &n), n)
	{
		if (ok) putchar(10);
		else ok = true;
		for (i = 0; i < n; ++i) scanf("%d", &a[i]);
		printfC(n, 6);
	}
	return 0;
}

你可能感兴趣的:(算法详解&模板,UVa,acm之路--数学,组合数学)