Python 在竞争性编码中获取测试用例的输入

Python getting input for test cases in competitive coding

如何为列表中具有 N 个值的 T 个测试用例获取输入。

其中 T 是测试用例的数量,N 是要纳入列表的输入的长度

3
3
2 2 2
3
1 2 3
4
2 3 4 5

我想拿出列表来处理它们,例如:

[2, 2, 2]
[1, 2, 3]
[2, 3, 4, 5]

请注意,此脚本不会对输入进行任何验证,并且实际上不需要测试用例的长度(即 l)。

T = int(input())

testCases = []
for i in range(T):
    l = int(input())
    testCase = [int(x) for x in  input().split(" ")]
    testCases.append(testCase)

print(testCases)

Python

kases = int(input())
for kase in range(kases):
    N = int(input())
    result = 1
    for i in range(1, N + 1):
        result = result * i
    print (result)
T=int(input("your question here")

虽然不知道怎么回答另一部分。

你可以简单地使用字符串的split方法通过' '(space)和map函数来分隔元素,将字符串输入值转换为整数。

for _ in range(int(input())):
    input()    # gets length of list, but doesn't store it
    input_list = list(map(int, input().split()))
    print(input_list)

您可以使用 Python 列表理解来获取输入。列表中具有 N 个值的 T 个测试用例的输入。我正在使用 Python 3.x.

T = int(input())  #Enter the No. of Testcases
input_list = [[j for j in input().split(' ')] for i in range(T)]