我在获取输入并将其附加到数组时遇到代码错误

发布于 2025-01-11 17:15:03 字数 686 浏览 0 评论 0原文

给你 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

Explanation

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风情万种。 2025-01-18 17:15:04

您不能将列表附加到整数数组。如果要合并两个数组,可以使用extend方法。

    a.extend(c)

You can't append a list to an integer array. If you want to merge two arrays you can use the extend method.

    a.extend(c)
野味少女 2025-01-18 17:15:04

如果 a 是列表,那么下面所有都满足,但这里 a 是数组,所以我们不能将列表附加到数组,

a = [1,2,3] # a is a list
c = [4] # c is list

它在任何情况下都不会给你正确的结果,

print(a.append(c)) # if we do a.append(c) result is like a = [1,2,3,[4]]

在任何情况下都会给你正确的结果,

print(a.extend(c)) # if we do a.extend(c) result is like a = [1,2,3,4]

如果“a”是列表而“c”是,则满足list 或“a”是数组,“c”也是数组

a += c # a += c result is same as a.extend(c)
print(a) # a = [1,2,3,4]

if a is list then below all satisfies but here a is array so we cant append list with array

a = [1,2,3] # a is a list
c = [4] # c is list

it won't give you correct result in any case

print(a.append(c)) # if we do a.append(c) result is like a = [1,2,3,[4]]

gives you correct result in any case

print(a.extend(c)) # if we do a.extend(c) result is like a = [1,2,3,4]

satisfies if "a" is list and "c" is list or "a" is array and "c" is also an array

a += c # a += c result is same as a.extend(c)
print(a) # a = [1,2,3,4]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文