- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试解决这个问题( https://open.kattis.com/problems/anotherbrick )。
当我提交最终代码时,我不断收到运行时错误。我不确定我的代码有什么问题。
# @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
请告诉我如何才能让 kattis.com 接受此代码。谢谢
最佳答案
运行时错误可能意味着:
问题 1:
您的代码不会增加高度:
if brick_width == width:
brick_width = 0
continue
如果你有像bricks = [1]*10000000
这样的数据,你的代码将会运行得很慢,因为你永远不会增加你所在的高度。您需要处理整个列表,直到得到“否”。
问题2:
您为此任务读取数据的方法是错误的 - 它不是基于文件。请参阅the documentation for python on Kattis -您需要从 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
<小时/>
用途:
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)
获取:
关于python - 不断收到 Kattis 问题的运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56498608/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!