Lotto
全排列问题
用dfs遍历所有情况,但这里需要考虑升序,类似洛谷的选数
代码
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int n, k;
int a[101];
bool vis[101];
int book[1001];
void dfs(int step, int sc)
{
if (step == 6)
{
for (int i = 0; i < 6; i++)
cout << book[i]<<" ";
cout << endl;
}
for (int i = sc; i < n; i++)
if (!vis[i])
{
vis[i] = 1;
book[step] = a[i];
dfs(step + 1, i + 1);
vis[i] = 0;
}
}
int main()
{
while (cin >> n)
{
memset(book, 0, sizeof(book));
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
dfs(0, 0);
}
return 0;
}