pku1833 排列(use next_permutation)

本文介绍了一个使用STL库函数next_permutation实现求排列的C++程序实例。该程序能够处理特定输入,并通过next_permutation函数寻找下一个字典序排列。
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参数就是返回增排列
  1. /*pku 1833
  2.   Name: 排列
  3.   Date: 28-07-08 10:56
  4.   Description: use next_permutaion
  5. */
  6. // next_permutation
  7. #include <iostream>
  8. #include <algorithm>
  9. using namespace std;
  10. #include<stdio.h>
  11. #define pr printf
  12. int n;
  13. int main(){
  14.     int k,a[1025],t,tmp,i;
  15.     scanf("%d",&t);
  16.     while(t--){
  17.         scanf("%d%d",&n,&k);
  18.         if(n<=4){
  19.             for(tmp=1,i=1;i<=n;i++)tmp*=i;   
  20.             k%=tmp;
  21.         }
  22.         for(int i=0;i<n;i++){
  23.             scanf("%d",&a[i]);
  24.         }
  25.         while(k!=0){
  26.             if(!next_permutation(a,a+n))
  27.                 {
  28.                     for(i=0;i<n;i++)a[i]=i+1;   
  29.                 }
  30.             k--;
  31.         }
  32.         for(i=0;i<n;i++)
  33.             pr("%d ",a[i]);
  34.                 pr("/n");
  35.         }   
  36. }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值