Descriptions:
对于数组[1, 2, 3],他们按照从小到大的全排列是
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
现在给你一个正整数n,n小于8,输出数组[1, 2, …,n]的从小到大的全排列。
Input
输入有多行,每行一个整数。当输入0时结束输入。
Output
对于每组输入,输出该组的全排列。每一行是一种可能的排列,共n个整数,每个整数用一个空格隔开,每行末尾没有空格。
Sample Input
2
3
0
Sample Output
1 2
2 1
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
题目链接:
https://vjudge.net/problem/OpenJ_Bailian-4070
讲一个STL函数:
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include <numeric> using namespace std; typedef long long ll; int n; int main() { while(cin >> n && n!=0) { int a[15]; for(int i=0; i<n; i++) a[i]=i+1; int j; do { for(int i=0; i<n; i++) { // 输出格式,特殊处理 if(i==n-1) cout<<a[i]; else cout <<a[i]<< " "; } cout << endl; } while(next_permutation(a,a+n)); } }
原文:https://www.cnblogs.com/sky-stars/p/10948384.html