题目并不难,但第一种写法所占内存过大,所以想了第二种写法
第一种写法 内存13920kb
n = int(raw_input())
importance = []
for i in range(n):
importance.append(raw_input())
result = 0
for item in importance:
if item == "Tetrahedron":
result += 4
if item == "Cube":
result += 6
if item == "Octahedron":
result += 8
if item == "Dodecahedron":
result += 12
if item == "Icosahedron" :
result += 20
print result第二种写法 3408kb
def get_score(item):
if item == "Tetrahedron":
return 4
if item == "Cube":
return 6
if item == "Octahedron":
return 8
if item == "Dodecahedron":
return 12
if item == "Icosahedron" :
return 20
n = int(raw_input())
result = 0
for i in range(n):
result += get_score(raw_input())
print result
本文对比了两种Python编程方法:一种直接使用循环和条件判断计算几何体面数,另一种通过定义函数来处理相同任务。第二种方法不仅减少了代码重复,而且在实现相同功能的同时显著降低了内存占用。
6万+

被折叠的 条评论
为什么被折叠?



