全部博文(68)
分类: C/C++
2012-01-26 09:22:25
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
012 021 102 120 201 210
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
答案:2783915460
#include
int N(int n)
{
int sum = 1;
int i = 1;
for (i=1; i<=n; i++)
sum *= i;
return sum;
}
int main(void)
{
int a = 999999;
int r[9] = {0};
int s[10] = {0};
int t = 0;
int i = 9;
for (i=0; i<10; i++)
s[i] = i;
for (i=9; i>0; i--){
t = N(i);
r[9-i] = a / t;
a = a % t;
//printf("%d ", r[9-i]);
}
putchar('\n');
int j = 0, k = 0;
for (i=0; i<9; i++)
{
for (j=0, k=0; k
continue;
else
k++;
for (; j<10; j++)
if(s[j] != -1){
printf("%d", s[j]);
s[j] = -1;
break;
}
}
for (i=0; i<10; i++)
if (s[i] == -1)
continue;
else
printf("%d", s[i]);
return 0;
}