资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
0、1、2三个数字的全排列有六种,按照字母序排列如下:
012、021、102、120、201、210
输入一个数n
求0~9十个数的全排列中的第n个(第1个为0123456789)。
输入格式
一行,包含一个整数n
输出格式
一行,包含一组10个数字的全排列
样例输入
1
样例输出
0123456789
数据规模和约定
0 < n <= 10!
解题思路:
本题考察的是枚举和循环,但是如果真正枚举起来,循环将相当复杂,如果数更多,循环将可能超时,C++提供了一个枚举的函数next_permutation
,可以直接遍历整个枚举,且按照大小顺序进行枚举,代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
while(cin >> n){
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
if(n == 1){
cout << "0123456789" << endl;
}
int t = 1;
while(next_permutation(a, a + 10)){
t ++;
if(t == n){
for(int i = 0;i < 10;i ++){
cout << a[i];
}
cout << endl;
break;
}
}
}
return 0;
}