poj1833

利用next_permutation,这个不需多余判断,直接就可处理由最后一个排列跳转到第一个排列的过程。用g++会tle,得用c++.

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

#define maxn 1028

int f[maxn];

int main()
{
//freopen("D:\\t.txt", "r", stdin);
int t;
scanf(
"%d", &t);
while (t--)
{
int n, k;
scanf(
"%d%d", &n, &k);
for (int i = 0; i < n; i++)
scanf(
"%d", &f[i]);
while (k--)
next_permutation(f, f
+ n);
printf(
"%d", f[0]);
for (int i = 1; i < n; i++)
printf(
" %d", f[i]);
printf(
"\n");
}
return 0;
}
posted @ 2011-04-08 19:48  金海峰  阅读(705)  评论(0编辑  收藏  举报