全排列

问题 A: 全排列

时间限制: 1 Sec  内存限制: 128 MB
提交: 6  解决: 3
[提交] [状态] [讨论版] [命题人:外部导入]

题目描述

给定一个数n,输出1~n这n个数所有排列的可能情况

输入

输入一个整数n

输出

输出所有排列(格式参照样例)

样例输入 Copy

3

样例输出 Copy

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



 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 int main(){
 6     vector<int> myVec;
 7     int n;
 8     cin >> n;
 9     for(int i = 1; i < n + 1; i++){
10         myVec.push_back(i);
11     }
12     do{
13         for(vector<int>::iterator it = myVec.begin(); it != myVec.end(); it++){
14             cout << *it << " ";
15         }
16         cout << endl;
17     }while(next_permutation(myVec.begin(), myVec.end()));
18     //next_permutation()取出当前排列,并重新排序为下一个排列
19     //prev_permutation()取出范围内的序列并将它重新排序为上一个序列 
20     return 0;
21 }

 

posted @ 2019-03-21 15:48  kakaluotedj  阅读(145)  评论(0编辑  收藏  举报