Although the title states that you are looking for a way to convert decimal numbers to integers, I will given an answer to the actual problem you're trying to solve: how to get the Kth permutation of an array of N elements.
Simply put, you need to go digit by digit in predicting the Kth permutation of the given array. The theoretical side of things are pretty simple. Assume that you have the elements in the array A and you store the information on whether each element is used in a second array S. S will be updated as you pick appropriate value for each digit. The result will be stored in an array R.
There are N! permutations of the elements in the given array A. For an array with N digits, let's consider how many permutations there are if the smallest element in A is picked to be the leftmost digit in the result, R[0]. It's (N-1)!, right? So permutations from #1 to #(N-1)! belong to the case where the leftmost element of the result is the smallest element in A. Permutations #((N-1)! + 1) to #(2 *(N-1)!) have second smallest value of A as R[0]. So permutations #((i-1) * (N-1)! + 1) to #(i * (N-1)!) uses ith unused and lexicographically-smallest digit in A as R[0].
In a more generalized sense, the value is being used in R[d] in the Kth lexicographically smallest permutation is A[i] such that A[i] is the ith lexicographically smallest element that is not used so far and such that (i * (N-1-d)! + 1) <= k and k <= ((i+1) * (N-1-d)!).
It will take O(N) time to find the suitable i value if you traverse the entire S. I am not sure as to how you may implement it exactly, but you may also be able to do binary search on S and achieve finding appropriate i in O(logN) time.
If you have large K values, I think that you will need to implement big integer multiplication in order to do the comparison, but I'll update this portion of the answer if I think of a clever way to get around this.
Once you pick the proper i, you can just assign A[i] as R[d] and go on to find the next digit.
Below is the piece of code that implements this solution. It is lengthy, but most of it is just the big integer implementation. The gist of the algorithm is actually less than 30 lines. I just wanted to provide a working code so that you may test it yourself if you'd like.
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#define NLIMIT 100
#define ASIZELIMIT 101
#define BIGINTBUCKETSLIMIT 100
#define BUCKETCAPACITY 1000000000
#define DIGITSPERBUCKET 9
using namespace std;
/* sufficient big integer implementation */
class BigInt
{
/*
* Note that BIGINTBUCKETSLIMIT should be high enough so that
* the values given as input does not cause overflow
* or access violation from the last bucket in operations
* multiply and subtract.
*/
public:
long long buckets[BIGINTBUCKETSLIMIT];
BigInt() {
for(int i=0; i < BIGINTBUCKETSLIMIT; ++i) {
buckets[i] = 0LL;
}
}
BigInt(int initialValue) {
for(int i=0; i < BIGINTBUCKETSLIMIT; ++i)
{
buckets[i] = initialValue % BUCKETCAPACITY;
initialValue /= BUCKETCAPACITY;
}
}
void multiply(int val) {
for(int i= BIGINTBUCKETSLIMIT - 1; i >= 0; --i)
buckets[i] = buckets[i] * val;
for(int i=0; i < BIGINTBUCKETSLIMIT - 1; ++i) {
buckets[i+1] += buckets[i] / BUCKETCAPACITY;
buckets[i] = buckets[i] % BUCKETCAPACITY;
}
}
void subtract(BigInt B) {
for(int i= 0; i < BIGINTBUCKETSLIMIT; ++i) {
buckets[i] = buckets[i] - B.buckets[i];
if(buckets[i] < 0LL) {
buckets[i] += BUCKETCAPACITY;
buckets[i+1]--;
}
}
}
const BigInt & operator=(const BigInt &B) {
for(int i=0; i < BIGINTBUCKETSLIMIT; ++i)
buckets[i] = B.buckets[i];
return *this;
}
bool operator<(const BigInt &B) {
for(int i=BIGINTBUCKETSLIMIT-1; i >= 0; --i)
if(buckets[i] != B.buckets[i])
return buckets[i] < B.buckets[i];
return false;
}
void importFromStr(string &src)
{
long long buffer = 0, j = 0;
for(int i=src.size() - 1; i >= 0; i -= DIGITSPERBUCKET) {
buffer = 0;
for(int k=max(0, i - DIGITSPERBUCKET + 1); k <= i; ++k) {
buffer = buffer * 10 + (src[k] - '0');
}
buckets[j++] = buffer;
}
}
};
BigInt factorials[ASIZELIMIT];
void preprocessFactorials(int n)
{
factorials[0] = BigInt(1);
for(int i=1; i <= n; ++i) {
factorials[i] = factorials[i-1];
factorials[i].multiply(i);
}
}
void findKthPermutation(int N, int A[], BigInt K, int result[]) {
BigInt tmpBigInt;
bool S[ASIZELIMIT];
for(int i=0; i < N; ++i)
S[i] = true;
K.subtract(BigInt(1));
preprocessFactorials(N);
for(int d=0; d < N; ++d) {
for(int i=0, j=0; i < N; ++i) {
if(S[i]) {
tmpBigInt = factorials[N-1-d];
tmpBigInt.multiply(j+1);
if(K < tmpBigInt) {
result[d] = A[i];
S[i] = 0;
tmpBigInt = factorials[N-1-d];
tmpBigInt.multiply(j);
K.subtract(tmpBigInt);
break;
}
++j;
}
}
}
}
int main() {
string k;
BigInt K;
int N;
int A[ASIZELIMIT], R[ASIZELIMIT];
cin >> N >> k;
for(int i=0; i < N; ++i)
cin >> A[i];
K.importFromStr(k);
sort(A, A+N);
findKthPermutation(N, A, K, R);
cout << R[0];
for(int i=1; i < N; ++i)
cout << " " << R[i];
cout << endl;
return 0;
}
As you may easily observe the 2 loops in function findKthPermutation and my BigInt class, the implementation works in O(N3), regardless of K. Although I don't know your exact performance needs, as N <= 100, it may be efficient enough. If it is not as efficient as you desire, my first suggestion would be be optimizing storing the information in S using some other data structure that may yield in O(logN) time the appropriate i value sought for each digit d.
Finally, note that this solution assumes that A contains no duplicate elements, as that meddles with the lexicographical enumeration of the possible permutations.