您的位置:首页 > 其它

康托展开

2014-04-24 15:47 113 查看
http://zh.wikipedia.org/wiki/康托展开

http://www.nocow.cn/index.php/康托展开

http://blog.sina.com.cn/s/blog_4bf7b6580100l2zs.html

http://www.skymoon.biz/?p=86

/article/4717368.html

 

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int  fac[] = {1,1,2,6,24,120,720,5040,40320}; //i的阶乘为fac[i]

int cantor(int s[],int n)
{
int num=0;
for(int i=0;i<n;i++)
{
int temp=0;
for(int j=i+1;j<n;j++)
{
if(s[j]<s[i])temp++;//判断几个数小于它
}
num+=fac[n-1-i]*temp;
}
return(num+1);
}

void uncantor(int s[], int n, int k)
{
k--;
bool vis[8]={0};

for(int i=n-1;i>=0;i--)
{
int x=k/fac[i];
for(int j=0;j<=x;j++)
if(vis[j]) x++;
s[n-1-i]=x+1;
vis[x]=1;
k%=fac[i];
}
}

int main()
{
int s[]={1, 2, 3, 4};
int n=sizeof(s)/sizeof(int);
do
{
printf("%d\n", cantor(s, n));
}while(next_permutation(s, s+n));

for(int k=1;k<=24;k++)
{
uncantor(s, 4, k);
for(int i=0;i<4;i++)
{
printf("%d ", s[i]);
}
printf("\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: