1. 程式人生 > >洛谷 P1088 火星人 (全排列)

洛谷 P1088 火星人 (全排列)

直接呼叫next_permutation即可,向前的話可以呼叫prev_permutation

#include<cstdio>
#include<cctype>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;

const int MAXN = 11234;
int a[MAXN], n, m;
void read(int& x)
{
	int f = 1; x = 0; char ch = getchar();
	while(!isdigit(ch)) { if(ch == '-1') f = -1; ch = getchar(); }
	while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
	x *= f;
}

int main()
{
	read(n); read(m);
	REP(i, 0, n) read(a[i]);
	while(m--) next_permutation(a, a + n);
	printf("%d", a[0]); REP(i, 1, n) printf(" %d", a[i]);
	puts("");
	return 0;
}