Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 13121 | Accepted: 5442 |
Description
Input
Output
Sample Input
3 3 1 2 3 1 3 1 3 2 1 10 2 1 2 3 4 5 6 7 8 9 10
Sample Output
3 1 2 1 2 3 1 2 3 4 5 6 7 9 8 10
Source
#include<stdio.h> #include<iostream> #include<algorithm> //next_permutation 在这个库函数里 using namespace std; int a[2000]; //将数据存储在这个数组中 int main() { int x,n,i,k,ji; scanf("%d",&x); while(x--) { scanf("%d %d",&n,&k); for(i=0;i<n;i++) scanf("%d",&a[i]); ji=0; while(1){ //因为很可能出现 // 3 100 1 2 3 这样的数据 调用一次 while(next_permutation(a,a+n ) ) // 根本不能到k=100 所以采用while(1)再循环调用while(next_permutation(a,a+n ) ) do{ if(k==ji) goto end ; ji++; }while(next_permutation(a,a+n ) ); // 在while中用这个 next_permutation(a,a+n ) 每调用一次就对他排一次序 按的是从小到大的顺序。 } end: for(i=0;i<n;i++) { printf("%d",a[i]); if(i!=n-1)printf(" "); else printf("\n"); } } }
/* 还可以自己定义 int cmp(int a,int b) { return a>b; } 调用 next_permutation(a,a+n ,cmp) 对其进行求解 可以按从大到小的顺序进行排列 */