http://zh.wikipedia.org/wiki/康托展开
http://www.nocow.cn/index.php/康托展开
http://blog.sina.com.cn/s/blog_4bf7b6580100l2zs.html
http://www.cnblogs.com/1-2-3/archive/2011/04/25/generate-permutation-part2.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 ", 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(" "); } return 0; }