1027“Ignations and the Princess II”
问题描述
给定n个数字,从1到n,输出第m小的序列
代码如下
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int a[1001]; 6 //next_permutation(a+1,a+n+1),范围[first,last) 7 //排列组合生成多种序列,例如3个数,有6种组合 8 //借助循环实现 9 int main() 10 { 11 int n,m; 12 while(cin>>n>>m){ 13 for(int i = 1; i <=n;i++){ //生成一个字典序最小的序列1到n 14 a[i] = i; 15 } 16 int b = 1; 17 do{ 18 if(b==m) //等于1时停止 19 break; 20 b++; 21 }while(next_permutation(a+1,a+n+1)); //注意第一个是a+1,最后一个是a+n 22 23 for(int i = 1; i <n; i++){ 24 cout<<a[i]<<" "; 25 } 26 27 cout<<a[n]<<endl; 28 29 } 30 31 return 0; 32 }
总结
一、next_permutation()生成排列组合,一个一个的生成排列组合,需要借助循环实现,以最后一次生成的为准

浙公网安备 33010602011771号