蓝桥杯 算法提高 排列数

问题描述

0、1、2三个数字的全排列有六种,按照字母序排列如下:012、021、102、120、201、210,输入一个数n,求0~9十个数的全排列中的第n个(第1个为0123456789)。

输入格式

一行,包含一个整数n

输出格式

一行,包含一组10个数字的全排列

样例输入

1

样例输出

0123456789

数据规模和约定

0 < n <= 10!

分析:

这道题可以运用C++头文件algorithm下的next_permutation函数,遍历即可。

C++代码如下

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 using namespace std;
 5 int main()
 6 {
 7     string str="0123456789";    //用字符串类型来存储数据
 8     int n, cnt=1;
 9     cin>>n; 
10     do{
11         if(cnt==n){
12             cout<<str;
13             break;
14         }
15         cnt++;
16     }while(next_permutation(str.begin(),str.end()));    //使用next_permutation函数求全排列 
17     return 0;
18 } 

 

posted @ 2021-05-24 23:48  熊猫耳朵  阅读(137)  评论(0)    收藏  举报