康托展开
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
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; }
相关文章推荐
- 康托展开
- 康托展开式---我排第几+逆康托展开
- 康托展开(用于全排列与整数的转换)
- 康托展开及其逆运算 详解
- 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)
- 康托展开
- sicily 2011. Nine Digits(广搜,康托展开)
- 康托展开
- [HDOJ1043]Eight(康托展开 BFS 打表)
- 康托展开
- hdu 1043/poj 1077 Eight (八数码 经典搜索题 bfs + 康托展开)
- 康托展开
- nyoj 139 我排第几个(康托展开)
- (蓝桥杯练习)3.排列序数(康托展开)
- 康托展开
- Vijos1029[晴天小猪历险记之Number] 搜索+康托展开
- POJ 1077 八数码(康托展开+暴力bfs)
- 蓝桥 排列序数(康托展开)
- 康托展开 及其逆运算
- 康托展开和逆康托展开的实现