2430: 【普及-】【P1706】全排列问题

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:5 解决:5

题目描述

按照字典序输出自然数 lns="http://www.w3.org/1998/Math/MathML">1 到 lns="http://www.w3.org/1998/Math/MathML"> 所有不重复的排列,即 lns="http://www.w3.org/1998/Math/MathML"> 的全排列,要求所产生的任一数字序列中不允许出现重复的数字。

输入

一个整数 lns="http://www.w3.org/1998/Math/MathML">

输出

由 lns="http://www.w3.org/1998/Math/MathML">1 组成的所有不重复的数字序列,每行一个序列。

每个数字保留 lns="http://www.w3.org/1998/Math/MathML">5 个场宽。

样例输入 复制

3

样例输出 复制

    1    2    3
    1    3    2
    2    1    3
    2    3    1
    3    1    2
    3    2    1

提示



#include<bits/stdc++.h>
using namespace std;
int a[10];

int main(){
    int n;
    cin>>n;
    for (int i=1;i<=n;i++) a[i]=i;
    do{
    	for (int i=1;i<=n;i++) printf("%5d",a[i]);
    	cout<<endl;
	}while (next_permutation(a+1,a+n+1));
	return 0;
}