码迷,mamicode.com
首页 > 编程语言 > 详细

欧拉计划(python) problem 24

时间:2015-02-05 09:35:31      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

Lexicographic permutations

Problem 24

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?


Answer:
2783915460
Completed on Wed, 4 Feb 2015, 15:13

Go to the thread for problem 24 in the forum.

Python code:

a=[0,1,2,3,4,5,6,7,8,9]
def func(x):
    i=1
    data=1
    while data<x:
        i+=1
        data*=i
    step=int(data/i)
    k=1
    while x-step*k>0:
        k+=1
    return [i,k-1,x-step*(k-1)]


last=[];
temp=(func(1000000))
while temp[2]>1:
    if temp[0]==len(a):
        k=temp[1]
        last.append(a[k])
        del a[k]
    else:
        last.append(a[0:-temp[0]])
        del a[0:-temp[0]]
        last.append(a[temp[1]])
        del a[temp[1]]
    temp=func(temp[2])
if temp[2]==1:
    last.append(a[::-1])
else:
    last.append(a)
print(last)

time: <1s

欧拉计划(python) problem 24

标签:

原文地址:http://blog.csdn.net/zhangzhengyi03539/article/details/43501225

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!