Project Euler problem 62

题目的大意很简单


做法的话。

我们就枚举1~10000的数的立方,

然后把这个立方中的有序重新排列,生成一个字符串s, 然后对于那些符合题目要求的肯定是生成同一个字符串的。

然后就可以用map来搞了


这里偷懒用了python

 

import string

dic = {}

def getstr(n):

	sn = str(n)

	arr = []

	for c in sn:

		arr.append(c)

	arr.sort()

	return ''.join(arr)

for i in xrange(1, 10000):

	s = getstr(i**3)

	if s in dic:

		dic[s] += 1

	else:

		dic[s] = 1

a = []

for d in dic:

	if dic.get(d) == 5:

		a.append(d)

k = min(a)

for i in xrange(1, 10000):

	s = getstr(i**3)

	if dic[s] == 5:

		print i, i**3


 

 

你可能感兴趣的:(project)