ZOJ1331
这道题比较有意思,要我们计算a^3 = b^3 + c^3 + d^3(a<=200)所有的可能性,并按一定顺序输出出来。这题给的时间限制是10秒钟,对于Python来说10秒钟未必可以成功,因为Python是一种效率比较低的语言。算法倒是很简单,对a<=200逐一测试即可。我的代码提交上去也是超时,但我将所有结果提前算出来,然后单独提交,反正答案也是死的,这样也能AC。
计算结果的代码:
<span style="font-family:SimSun;font-size:14px;">#2015-02-19
# -*- coding: utf-8 -*-
f = open('test.txt','w') #创建一个txt文件,将结果输出进去
for a in range(2,201):
num = []
s = []
for b in range(2,a):
for c in range(2,b+1):
for d in range(2,c+1):
if a**3 == b**3 + c**3 + d**3:
num.append(b)
num.append(c)
num.append(d)
s.append(d)
break
if len(num)!=0:
s.sort()
for i in s:
for j in range(len(num)-1,-1,-3): #以从小到大的顺序输出结果
if num[j] == i:
print>>f, 'Cube = %d, Triple = (%d,%d,%d)' % (a,num[j],num[j-1],num[j-2])
break
f.close()
</span>
提交的代码:
<span style="font-family:SimSun;font-size:14px;">#2015-02-20
#-*- codig:utf-8 -*-
anwser = '''Cube = 6, Triple = (3,4,5) #三个引号的字符串,可以跨行
Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 18, Triple = (9,12,15)
Cube = 19, Triple = (3,10,18)
Cube = 20, Triple = (7,14,17)
Cube = 24, Triple = (12,16,20)
Cube = 25, Triple = (4,17,22)
Cube = 27, Triple = (3,18,24)
Cube = 28, Triple = (18,19,21)
Cube = 29, Triple = (11,15,27)
Cube = 30, Triple = (15,20,25)
Cube = 36, Triple = (4,24,32)
Cube = 36, Triple = (18,24,30)
Cube = 38, Triple = (6,20,36)
Cube = 40, Triple = (14,28,34)
Cube = 41, Triple = (2,17,40)
Cube = 41, Triple = (6,32,33)
Cube = 42, Triple = (21,28,35)
Cube = 44, Triple = (16,23,41)
Cube = 45, Triple = (5,30,40)
Cube = 46, Triple = (3,36,37)
Cube = 46, Triple = (27,30,37)
Cube = 48, Triple = (24,32,40)
Cube = 50, Triple = (8,34,44)
Cube = 53, Triple = (29,34,44)
Cube = 54, Triple = (6,36,48)
Cube = 54, Triple = (12,19,53)
Cube = 54, Triple = (27,36,45)
Cube = 56, Triple = (36,38,42)
Cube = 57, Triple = (9,30,54)
Cube = 58, Triple = (15,42,49)
Cube = 58, Triple = (22,30,54)
Cube = 60, Triple = (21,42,51)
Cube = 60, Triple = (30,40,50)
Cube = 63, Triple = (7,42,56)
Cube = 66, Triple = (33,44,55)
Cube = 67, Triple = (22,51,54)
Cube = 69, Triple = (36,38,61)
Cube = 70, Triple = (7,54,57)
Cube = 71, Triple = (14,23,70)
Cube = 72, Triple = (8,48,64)
Cube = 72, Triple = (34,39,65)
Cube = 72, Triple = (36,48,60)
Cube = 75, Triple = (12,51,66)
Cube = 75, Triple = (38,43,66)
Cube = 76, Triple = (12,40,72)
Cube = 76, Triple = (31,33,72)
Cube = 78, Triple = (39,52,65)
Cube = 80, Triple = (28,56,68)
Cube = 81, Triple = (9,54,72)
Cube = 81, Triple = (25,48,74)
Cube = 82, Triple = (4,34,80)
Cube = 82, Triple = (12,64,66)
Cube = 82, Triple = (19,60,69)
Cube = 84, Triple = (28,53,75)
Cube = 84, Triple = (42,56,70)
Cube = 84, Triple = (54,57,63)
Cube = 85, Triple = (50,61,64)
Cube = 87, Triple = (20,54,79)
Cube = 87, Triple = (26,55,78)
Cube = 87, Triple = (33,45,81)
Cube = 87, Triple = (38,48,79)
Cube = 88, Triple = (21,43,84)
Cube = 88, Triple = (25,31,86)
Cube = 88, Triple = (32,46,82)
Cube = 89, Triple = (17,40,86)
Cube = 90, Triple = (10,60,80)
Cube = 90, Triple = (25,38,87)
Cube = 90, Triple = (45,60,75)
Cube = 90, Triple = (58,59,69)
Cube = 92, Triple = (6,72,74)
Cube = 92, Triple = (54,60,74)
Cube = 93, Triple = (32,54,85)
Cube = 95, Triple = (15,50,90)
Cube = 96, Triple = (19,53,90)
Cube = 96, Triple = (48,64,80)
Cube = 97, Triple = (45,69,79)
Cube = 99, Triple = (11,66,88)
Cube = 100, Triple = (16,68,88)
Cube = 100, Triple = (35,70,85)
Cube = 102, Triple = (51,68,85)
Cube = 103, Triple = (12,31,102)
Cube = 105, Triple = (33,70,92)
Cube = 106, Triple = (58,68,88)
Cube = 108, Triple = (12,72,96)
Cube = 108, Triple = (13,51,104)
Cube = 108, Triple = (15,82,89)
Cube = 108, Triple = (24,38,106)
Cube = 108, Triple = (54,72,90)
Cube = 110, Triple = (29,75,96)
Cube = 111, Triple = (16,47,108)
Cube = 112, Triple = (72,76,84)
Cube = 113, Triple = (50,74,97)
Cube = 114, Triple = (18,60,108)
Cube = 114, Triple = (57,76,95)
Cube = 115, Triple = (3,34,114)
Cube = 116, Triple = (23,86,97)
Cube = 116, Triple = (30,84,98)
Cube = 116, Triple = (44,60,108)
Cube = 117, Triple = (13,78,104)
Cube = 120, Triple = (9,55,116)
Cube = 120, Triple = (42,84,102)
Cube = 120, Triple = (60,80,100)
Cube = 121, Triple = (49,84,102)
Cube = 122, Triple = (19,92,101)
Cube = 123, Triple = (6,51,120)
Cube = 123, Triple = (18,96,99)
Cube = 123, Triple = (44,51,118)
Cube = 125, Triple = (20,85,110)
Cube = 126, Triple = (14,84,112)
Cube = 126, Triple = (23,94,105)
Cube = 126, Triple = (63,84,105)
Cube = 127, Triple = (13,65,121)
Cube = 129, Triple = (38,57,124)
Cube = 132, Triple = (5,76,123)
Cube = 132, Triple = (48,69,123)
Cube = 132, Triple = (66,88,110)
Cube = 133, Triple = (21,70,126)
Cube = 134, Triple = (44,102,108)
Cube = 134, Triple = (86,95,97)
Cube = 135, Triple = (15,90,120)
Cube = 137, Triple = (44,73,128)
Cube = 138, Triple = (9,108,111)
Cube = 138, Triple = (69,92,115)
Cube = 138, Triple = (72,76,122)
Cube = 138, Triple = (81,90,111)
Cube = 139, Triple = (94,96,99)
Cube = 140, Triple = (14,108,114)
Cube = 140, Triple = (49,98,119)
Cube = 140, Triple = (90,95,105)
Cube = 141, Triple = (72,85,122)
Cube = 142, Triple = (28,46,140)
Cube = 142, Triple = (31,64,137)
Cube = 144, Triple = (16,96,128)
Cube = 144, Triple = (68,78,130)
Cube = 144, Triple = (72,96,120)
Cube = 145, Triple = (12,81,136)
Cube = 145, Triple = (55,75,135)
Cube = 147, Triple = (22,75,140)
Cube = 150, Triple = (24,102,132)
Cube = 150, Triple = (71,73,138)
Cube = 150, Triple = (75,100,125)
Cube = 150, Triple = (76,86,132)
Cube = 151, Triple = (46,47,148)
Cube = 152, Triple = (24,80,144)
Cube = 152, Triple = (62,66,144)
Cube = 153, Triple = (17,102,136)
Cube = 156, Triple = (65,87,142)
Cube = 156, Triple = (78,104,130)
Cube = 159, Triple = (3,121,131)
Cube = 159, Triple = (87,102,132)
Cube = 160, Triple = (56,112,136)
Cube = 160, Triple = (69,123,124)
Cube = 162, Triple = (18,108,144)
Cube = 162, Triple = (36,57,159)
Cube = 162, Triple = (50,96,148)
Cube = 162, Triple = (59,93,148)
Cube = 162, Triple = (81,108,135)
Cube = 164, Triple = (8,68,160)
Cube = 164, Triple = (24,128,132)
Cube = 164, Triple = (38,120,138)
Cube = 164, Triple = (69,99,146)
Cube = 167, Triple = (12,86,159)
Cube = 168, Triple = (56,106,150)
Cube = 168, Triple = (84,112,140)
Cube = 168, Triple = (108,114,126)
Cube = 170, Triple = (96,107,141)
Cube = 170, Triple = (100,122,128)
Cube = 171, Triple = (19,114,152)
Cube = 171, Triple = (27,90,162)
Cube = 171, Triple = (54,80,163)
Cube = 171, Triple = (107,108,136)
Cube = 174, Triple = (40,108,158)
Cube = 174, Triple = (45,126,147)
Cube = 174, Triple = (47,97,162)
Cube = 174, Triple = (52,110,156)
Cube = 174, Triple = (66,90,162)
Cube = 174, Triple = (76,96,158)
Cube = 174, Triple = (87,116,145)
Cube = 175, Triple = (28,119,154)
Cube = 176, Triple = (25,92,167)
Cube = 176, Triple = (42,86,168)
Cube = 176, Triple = (50,62,172)
Cube = 176, Triple = (64,92,164)
Cube = 177, Triple = (48,137,142)
Cube = 178, Triple = (34,80,172)
Cube = 178, Triple = (48,133,147)
Cube = 179, Triple = (17,57,177)
Cube = 180, Triple = (20,120,160)
Cube = 180, Triple = (50,76,174)
Cube = 180, Triple = (63,126,153)
Cube = 180, Triple = (90,120,150)
Cube = 180, Triple = (116,118,138)
Cube = 181, Triple = (108,109,150)
Cube = 184, Triple = (12,144,148)
Cube = 184, Triple = (108,120,148)
Cube = 185, Triple = (57,68,180)
Cube = 185, Triple = (68,113,166)
Cube = 186, Triple = (18,121,167)
Cube = 186, Triple = (64,108,170)
Cube = 186, Triple = (93,124,155)
Cube = 187, Triple = (58,131,160)
Cube = 188, Triple = (115,122,149)
Cube = 189, Triple = (21,46,188)
Cube = 189, Triple = (21,126,168)
Cube = 190, Triple = (30,100,180)
Cube = 190, Triple = (56,133,163)
Cube = 191, Triple = (39,146,156)
Cube = 192, Triple = (34,123,173)
Cube = 192, Triple = (38,106,180)
Cube = 192, Triple = (96,128,160)
Cube = 193, Triple = (25,68,190)
Cube = 194, Triple = (90,138,158)
Cube = 196, Triple = (126,133,147)
Cube = 197, Triple = (53,58,194)
Cube = 198, Triple = (22,132,176)
Cube = 198, Triple = (27,46,197)
Cube = 198, Triple = (73,135,170)
Cube = 198, Triple = (99,132,165)
Cube = 199, Triple = (6,127,180)
Cube = 200, Triple = (32,136,176)
Cube = 200, Triple = (70,140,170)'''
print anwser,</span>
也许这是投机取巧,也希望有大牛指点可以直接AC的代码。