46. 全排列

题目描述

给定一个整数n,输出1-n的全排列。

解答要求时间限制:1000ms, 内存限制:100MB
输入

每个测试文件只有一个数据,输入一个整数n(0<n≤8)。

输出

输出全排列(每个排列中的数字用空格隔开),且每组排列注意按字典序输出所有排列(即要先输出123才能输出132,而不能先输出132在输出123)。

样例

输入样例 1 复制

3

输出样例 1

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
提示样例 1
 思路:借助C++的函数,
代码;
// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
#include <vector>
#include<algorithm>
int main()
{  
  // please define the C++ input here. For example: int a,b; cin>>a>>b;;  
  // please finish the function body here.  
  // please define the C++ output here. For example:cout<<____<<endl; 
   int N;
   cin>>N;
   vector<int>nums;
   for(int i = 1;i<=N;i++)
   {
       nums.push_back(i);
   }
   do{
       for(int i = 0;i<N;i++)
       {
           cout<<nums[i];
           if(i!=N-1)
               cout<<" ";
       }
      cout<<endl;
       //cout<<nums[N]<<endl;
   }while(next_permutation(nums.begin(),nums.end()));
  return 0;
}
以大多数人努力程度之低,根本轮不到去拼天赋~
原文地址:https://www.cnblogs.com/gcter/p/15472551.html