Python 继续获取Kattis问题的运行时错误
我正在努力解决这个问题() 当我提交我的最终代码时,我不断得到一个运行时错误。我不确定我的代码出了什么问题Python 继续获取Kattis问题的运行时错误,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我正在努力解决这个问题() 当我提交我的最终代码时,我不断得到一个运行时错误。我不确定我的代码出了什么问题 # @Author Gansaikhan Shur # Another Brick in the Wall import sys # if len(sys.argv) < 2: # sys.exit("{}: needs an input file!".format(sys.argv[0])) # exit() sys_input = sys.argv[1] w
# @Author Gansaikhan Shur
# Another Brick in the Wall
import sys
# if len(sys.argv) < 2:
# sys.exit("{}: needs an input file!".format(sys.argv[0]))
# exit()
sys_input = sys.argv[1]
with open(sys_input, "r") as f:
data = [line.rstrip('\n') for line in f]
hw_num = data[0].split()
len_bricks = data[1].split()
# Assigning to variables
height = int(hw_num[0])
width = int(hw_num[1])
num_bricks = hw_num[2]
currHeight = 0
for i in range(height):
currHeight = i+1
brick_width = 0
for j in len_bricks:
brick_width += int(j)
if currHeight == height and brick_width == width:
print("YES")
exit()
if brick_width == width:
brick_width = 0
continue
elif brick_width > width:
print("NO")
exit()
else:
continue
currHeight += 1
#@作者甘赛汗·舒尔
#墙上的另一块砖
导入系统
#如果len(系统argv)<2:
#sys.exit(“{}:需要一个输入文件!”.format(sys.argv[0]))
#退出()
sys_input=sys.argv[1]
打开(系统输入,“r”)作为f:
数据=[line.rstrip('\n'),用于f中的行]
hw_num=数据[0]。拆分()
len_bricks=数据[1]。拆分()
#分配给变量
高度=整数(hw_num[0])
宽度=整数(hw_num[1])
num_bricks=hw_num[2]
currHeight=0
对于范围内的i(高度):
电流高度=i+1
砖块宽度=0
对于j in len_砖:
砖块宽度+=int(j)
如果currHeight==高度,brick_width==宽度:
打印(“是”)
退出()
如果砖块宽度==宽度:
砖块宽度=0
持续
elif砖宽度>宽度:
打印(“否”)
退出()
其他:
持续
电流高度+=1
请让我知道我能做些什么让kattis.com接受此代码。谢谢运行时错误可能意味着:
- 您得到的错误仅在运行时发生
- 您得到了错误的结果(请参见下面的代码块-您的数据收集错误)
- 您的代码运行太长
问题1: 您的代码不会增加高度: 如果你有像
bricks=[1]*10000000这样的数据,你的代码将运行looong,因为你永远不会增加你所处的高度。您需要处理整个列表,直到得到“否”
问题2:
您为此任务读入数据的方法是错误的-它不是基于文件的。请参阅-您需要从sys.stdin
(下面的代码)中读取
您可以简化它:
def can_he_do_it(h,w,bricks):
height = 0
cur_w = 0
# remove this line for kattis.com
print("Project: Height: {} Width: {} with {}".format(h,w,bricks))
# process all bricks - not the height or anythin else with range
# bricks is all you got, place them down one by one, check lenght/height
# and return False if you are too wide or not high enough
# return True if you are high enough
for brick in bricks:
cur_w += brick # add to current width and check
if cur_w > w: # too wide
return False
elif cur_w == w: # exaclty correct, next row
height += 1
cur_w = 0
if height == h: # reach target heigth
return True
return False # too few materials
def result(b):
print("YES" if b else "NO")
# replace with your number-read-code
result( can_he_do_it(2, 10, [5,5,5,5,5,5,]) )
result( can_he_do_it(2, 10, [5,5,5,3,5,5,]) )
result( can_he_do_it(2, 10, [5,5,5,]) )
输出:
Project: Height: 2 Width: 10 with [5, 5, 5, 5, 5, 5]
YES
Project: Height: 2 Width: 10 with [5, 5, 5, 3, 5, 5]
NO
Project: Height: 2 Width: 10 with [5, 5, 5]
NO
使用:
要获得:
谢谢您的评论!你的解决方案看起来比我的好。但我在提交给Kattis.com时仍然有错误。我正在使用sys.argv[1]读取文件。但是网站一直告诉我有一个运行时错误。错误是什么?@JenniferGoncalves当我将它提交到Kattis.com时,状态栏下会显示“运行时错误”
Project: Height: 2 Width: 10 with [5, 5, 5, 5, 5, 5]
YES
Project: Height: 2 Width: 10 with [5, 5, 5, 3, 5, 5]
NO
Project: Height: 2 Width: 10 with [5, 5, 5]
NO
import sys
data = []
for i in sys.stdin:
data.append(i)
data = [line.rstrip('\n') for line in data if line]
height, width, *_ = map(int,data[0].split()) # read 2 values, ignore rest, cast to int
bricks = list(map(int, data[1].split())) # use all bricks, cast to int
# replace with your number-read-code
can_he_do_it(height, width, bricks)