首页 > 其他 > 详细

POJ1833 排列

时间:2014-02-25 14:51:55      阅读:325      评论:0      收藏:0      [点我收藏+]

好久不见的中文题目,排列,组合数学,对许多人都是很头疼的,这道题目算是应验了 计算机就是让人偷懒的  这句话,其实在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;
}


POJ1833 排列

原文:http://blog.csdn.net/yitiaodacaidog/article/details/19828855

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!