POJ1833 排列

好久不见的中文题目,排列,组合数学,对许多人都是很头疼的,这道题目算是应验了 计算机就是让人偷懒的  这句话,其实在STL里,我们用过了vector,queue,stack,map等等,都比较好用的,尽管有些耗时,这道题目可以用STL中的next_permutation(opt1,opt2)排列函数来轻松解决,pot1是指向需要数组头地址,opt2代表需要排列的长度,而且排列完全符合题目要求的:如果遇到最后一个排列,则下1排列为第1个排列



#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>

#define ll long long

#define eps 1e-8

#define inf 0xfffffff
//const ll INF = 1ll<<61;

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;

//#define IN freopen("c:\\Users\\linzuojun\\desktop\\input.txt", "r", stdin)  
//#define OUT freopen("c:\\Users\\linzuojun\\desktop\\output.txt", "w", stdout) 


/*对于next_permutation()排列函数的介绍*/
/*
void TestArray()
{
    char chs[] = {'a', 'd', 'c', 'e', 'b'};
    int count = sizeof(chs)/sizeof(char);
    
    next_permutation(chs+0, chs + count);
    
    printf("TestArray:\n");
    for(int i = 0; i < count; i++) {
            printf("%c\t", chs[i]);
    }
    
    printf("\n");
}

void TestVector()
{
     char chs[] = {'a', 'd', 'c', 'e', 'b'};
     int count = sizeof(chs)/sizeof(char);
     vector<char> vChs(chs, chs + count);
     
     next_permutation(vChs.begin(), vChs.end());
     
     printf("TestVector:\n");
     vector<char>::iterator itr;
     for(itr = vChs.begin(); itr != vChs.end(); itr++) {
             printf("%c\t", *itr);
     }
     printf("\n");
}

int main(int argc, char *argv[])
{
    TestArray();
    printf("\n");
    TestVector();
    
    system("PAUSE");

	return EXIT_SUCCESS;
}
*/


int num[2000 + 5];

void clear() {
	memset(num,0,sizeof(num));
}

int main() {
	int t;
	int n,k;
	cin>>t;
	while(t--) {
		clear();
		cin>>n>>k;
		for(int i=0;i<n;i++)
			scanf("%d",&num[i]);
		while(k--) {
			next_permutation(num,num+n);
		}
		for(int i=0;i<n-1;i++) 
			printf("%d ",num[i]);
		printf("%d\n",num[n-1]);
	}
	return EXIT_SUCCESS;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值