Python 中的 UVa 运行 时间误差
UVa Run time error in Python
所以我正在尝试解决UVa在线法官的以下问题:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2864
我在Python中写了下面的代码:
t = int(input())
for i in range(t):
highs = 0
lows = 0
walls = int(input())
heights = [0]*50
for h in range(walls):
heights[h] = (int(input()))
for j in range(1, walls):
if (heights[j] < heights[j - 1]):
highs += 1
elif (heights[j] > heights[j - 1]):
lows += 1
print("Case %d: %d %d" % (i + 1, highs, lows))
exit(0)
每次我用不同的测试用例尝试我的代码时,我都会得到预期的输出;它在我这边工作得很好,但是当我提交它时,我不断收到运行时错误。我现在很绝望,因为我已经尝试了一百万件事,但没有任何效果。请帮忙。
我认为错误在这里:
for h in range(walls):
heights[h] = (int(input()))
input()
读取一行,然后int()
尝试将该行转换为整数。但是 "1 4 2 2 3 5 3 4"
不能转换为整数,如果你读 8 行你可能会 运行 out input.
相反,尝试
heights = [int(i) for i in input().split()]
哪个应该 return [1, 4, 2, 2, 3, 5, 3, 4]
.
正如所建议的那样,运行时错误在您的这部分代码中:
for h in range(walls):
heights[h] = (int(input()))
您可以更改
heights = [int(i) for i in input().split()]
并省略行
heights = [0]*50
我发现的另一件事是您以相反的方式计算低点和高点。你应该这样做:
if (heights[j] < heights[j - 1]):
lows += 1
elif (heights[j] > heights[j - 1]):
highs += 1
应该可以。希望你得到解决方案 Accepted ;)
所以我正在尝试解决UVa在线法官的以下问题:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2864
我在Python中写了下面的代码:
t = int(input())
for i in range(t):
highs = 0
lows = 0
walls = int(input())
heights = [0]*50
for h in range(walls):
heights[h] = (int(input()))
for j in range(1, walls):
if (heights[j] < heights[j - 1]):
highs += 1
elif (heights[j] > heights[j - 1]):
lows += 1
print("Case %d: %d %d" % (i + 1, highs, lows))
exit(0)
每次我用不同的测试用例尝试我的代码时,我都会得到预期的输出;它在我这边工作得很好,但是当我提交它时,我不断收到运行时错误。我现在很绝望,因为我已经尝试了一百万件事,但没有任何效果。请帮忙。
我认为错误在这里:
for h in range(walls):
heights[h] = (int(input()))
input()
读取一行,然后int()
尝试将该行转换为整数。但是 "1 4 2 2 3 5 3 4"
不能转换为整数,如果你读 8 行你可能会 运行 out input.
相反,尝试
heights = [int(i) for i in input().split()]
哪个应该 return [1, 4, 2, 2, 3, 5, 3, 4]
.
正如所建议的那样,运行时错误在您的这部分代码中:
for h in range(walls):
heights[h] = (int(input()))
您可以更改
heights = [int(i) for i in input().split()]
并省略行
heights = [0]*50
我发现的另一件事是您以相反的方式计算低点和高点。你应该这样做:
if (heights[j] < heights[j - 1]):
lows += 1
elif (heights[j] > heights[j - 1]):
highs += 1
应该可以。希望你得到解决方案 Accepted ;)