Project Euler 24 Lexicographic permutations( 康拓逆展开 )


题意:
排列指的是将一组物体进行有顺序的放置。例如,3124是数字1、2、3、4的一个排列。如果把所有排列按照数字大小或字母先后进行排序,我们称之为字典序排列。0、1、2的字典序排列是:012 021 102 120 201 210

数字0、1、2、3、4、5、6、7、8、9的字典序排列中第一百万位的排列是什么?


/*************************************************************************
    > File Name: euler024.c
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年06月28日 星期三 13时08分35秒
 ************************************************************************/

#include <stdio.h>
#include <inttypes.h>

#define MAX_N 10

int32_t main() {
	int32_t Target = 3;
	int32_t vis[MAX_N] = {0} , fac[MAX_N] = {0};
	int32_t ans[MAX_N + 1] = {0};
	int32_t n = 10;

	fac[0] = 1;
	for (int32_t i = 1 ; i < n ; i++) {
		fac[i] = fac[i - 1] * i;
	}

	Target--;
	for (int32_t i = n - 1 ; i >= 0 ; i--) {
		int32_t t = Target / fac[i];		
		int32_t j;
		for (j = 0 ; j < n ; j++) {
			if (vis[j])	continue;
			if (t == 0) break;
			t--;
		}
		vis[j] = 1;
		ans[ ++ans[0] ] = j;
		Target %= fac[i];
	}

	for (int32_t i = 1 ; i <= ans[0] ; i++) {
		printf("%d ",ans[i]);
	}
	puts("");
	return 0;
}
原文地址:https://www.cnblogs.com/WArobot/p/7089866.html