标签:复杂度 stream include 原理 class scanf sizeof tor 恰恰
处理何种问题:与康托展开恰恰相反,及已知元素个数和所要求的名次,可以输出该名次的排列方式。
性能:时间复杂度为 O(len^2),len 为字符串长度
原理:略
实现步骤:略
备注:名次从1开始,名次不再范围内的话会出现乱码
输入样例解释:
5 //元素个数
2 //字典序名称
输出样例解释:
1 2 3 5 4 //字典序为2的排列方式
#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
int fact[20]= {1,1,2,6,24,120,720,5040,40320,362880,3628800}; //阶乘
int vis[100];
void decantor(int x,int n,int ans[])
{
int ctor=0;
memset(vis,0,sizeof vis);
x--;
int j;
for(int i=1; i<=n; i++)
{
int t=x/fact[n-i];
for(j=1; j<=n; j++)
{
if(!vis[j])
{
if(!t) break;
t--;
}
}
ans[ctor++]=j;
vis[j]=1;
x%=fact[n-i];
}
}
int main()
{
int x,n;
int ans[100];
scanf("%d%d",&n,&x);//元素个数、需要的排序名次
decantor(x,n,ans);
for(int i=0;i<n;++i)
{
printf("%d ",ans[i]);
}
printf("\n");
return 0;
}
标签:复杂度 stream include 原理 class scanf sizeof tor 恰恰
原文地址:https://www.cnblogs.com/l1l1/p/9669700.html