康托展开and逆展开c++实现
康托展开:
#include <iostream>
using namespace std;
int board[10]{1,1,2,6,24,120,720,5040,40320,362880};//0-9的阶乘
bool book[10]; //标记数是否已经出现过
int main(){
string s;
cin>>s;
int sum = 1;
for(int i=0 ; i<s.length() ; i++){
int mid = s[i]-'0';
int t = 0;
for(int j=1 ; j<mid ; j++){
if(book[j] == false)t++;
}
sum += t*board[s.length()-i-1];
book[mid] = true;
}
cout<<sum<<endl;
return 0;
}
康托逆展开:
#include<iostream>
using namespace std;
int board[10]{1,1,2,6,24,120,720,5040,40320,362880};//0-9的阶乘
bool book[10];//标记数是否已经出现过
int main(){
string s;
cin>>s;
int N;
cin>>N;
N--;
for(int i=s.length() ; i>=1 ; i--){
int mid = N/board[i-1]+1;
for(int i=1 ; i<=mid ; i++){
if(book[i])mid++;
}
book[mid] = true;
cout<<mid;
N %= board[i-1];
}
return 0;
}