next_permutation函数是stl里的库函数,用来求排列。
Parameters
- first, last
- Bidirectional iterators to the initial and final positions of the sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. comp
- Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument is to be considered less than the second argument.
Return value
不用comp参数就是返回增排列- /*pku 1833
- Name: 排列
- Date: 28-07-08 10:56
- Description: use next_permutaion
- */
- // next_permutation
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #include<stdio.h>
- #define pr printf
- int n;
- int main(){
- int k,a[1025],t,tmp,i;
- scanf("%d",&t);
- while(t--){
- scanf("%d%d",&n,&k);
- if(n<=4){
- for(tmp=1,i=1;i<=n;i++)tmp*=i;
- k%=tmp;
- }
- for(int i=0;i<n;i++){
- scanf("%d",&a[i]);
- }
- while(k!=0){
- if(!next_permutation(a,a+n))
- {
- for(i=0;i<n;i++)a[i]=i+1;
- }
- k--;
- }
- for(i=0;i<n;i++)
- pr("%d ",a[i]);
- pr("/n");
- }
- }
本文介绍了一个使用STL库函数next_permutation实现求排列的C++程序实例。该程序能够处理特定输入,并通过next_permutation函数寻找下一个字典序排列。
4万+

被折叠的 条评论
为什么被折叠?



