我在获取输入并将其附加到数组时遇到代码错误
给你 N 根棍子,每根棍子的长度都是正整数。对木棍进行切割操作,使所有木棍都减少最小木棍的长度。 给定 N 根木棍的长度,打印在每次后续切割操作之前剩下的木棍数量。 注意:对于每次切割操作,您都必须重新计算最小木条的长度(不包括零长度木条)。
输入 第一行包含一个整数 N。
下一行包含由空格分隔的 N 个整数,其中每个整数代表第 i 根棍子的长度。
6
5 4 4 2 2 8
输出 对于每个操作,在单独的行上打印切割的木棍数量。
6
4
2
1
import array as arr
n = int(input())
a = arr.array('i',[1002])
for i in range(n):
c = [int(x) for x in input().split()]
a.append(c)
t=n
for i in range(0,1001):
if a[i] > 0:
print(t)
t=t-a[i]
You are given N sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick.
Given the length of N sticks, print the number of sticks that are left before each subsequent cut operations. Note: For each cut operation, you have to recalculate the length of smallest sticks (excluding zero-length sticks).
Input
The first line contains a single integer N.
The next line contains N integers separated by space, where each integer represents the length of the ith stick.
6
5 4 4 2 2 8
Output
For each operation, print the number of sticks that are cut, on separate lines.
6
4
2
1
import array as arr
n = int(input())
a = arr.array('i',[1002])
for i in range(n):
c = [int(x) for x in input().split()]
a.append(c)
t=n
for i in range(0,1001):
if a[i] > 0:
print(t)
t=t-a[i]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将列表附加到整数数组。如果要合并两个数组,可以使用extend方法。
You can't append a list to an integer array. If you want to merge two arrays you can use the
extend
method.如果 a 是列表,那么下面所有都满足,但这里 a 是数组,所以我们不能将列表附加到数组,
它在任何情况下都不会给你正确的结果,
在任何情况下都会给你正确的结果,
如果“a”是列表而“c”是,则满足list 或“a”是数组,“c”也是数组
if a is list then below all satisfies but here a is array so we cant append list with array
it won't give you correct result in any case
gives you correct result in any case
satisfies if "a" is list and "c" is list or "a" is array and "c" is also an array