1294 全排列
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题目描述 Description
给出一个n, 请输出n的所有全排列
输入描述 Input Description
读入仅一个整数n (1<=n<=10)
输出描述 Output Description
一共n!行,每行n个用空格隔开的数,表示n的一个全排列。并且按全排列的字典序输出。
样例输入 Sample Input
3
样例输出 Sample Output
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
数据范围及提示 Data Size & Hint
//记得把下面的cout全部改成printf //不然会t的 //我是太懒了,就没改了 #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) const int maxn=20; int a[maxn]; int b[maxn]; int n; void init() {cin>>n;return; } int put() {int first=1;for(int i=1;i<=n;i++){if(first){printf("%d",a[i]);first=0;}elsecout<<" "<<a[i];}cout<<endl; } void work(int t) {if(t>n){put();return;}for(int i=1;i<=n;i++){if(!b[i]){b[i]=1;a[t]=i;work(t+1);b[i]=0;}}} int main() {//sspeed; init();work(1);return 0; }