【NEEPU OJ】1024--Full Permutation

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34072526/article/details/88176003

Description

给出一个n, 请输出n的所有全排列

Input

读入仅一个整数n (1<=n<=10)

Output

一共n!行,每行n个用空格隔开的数,表示n的一个全排列。并且按全排列的字典序输出。

输入样例 1

3

输出样例 1

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

提示

从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列。当m=n时所有的排列情况叫全排列。

公式:全排列数f(n)=n!(定义0!=1),如1,2,3三个元素的全排列为:

1,2,3

1,3,2

2,1,3

2,3,1

3,1,2

3,2,1

共3*2*1=6种。

来源

Own


代码

#include <iostream>
#include <algorithm>
using namespace std;

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

猜你喜欢

转载自blog.csdn.net/qq_34072526/article/details/88176003
今日推荐