如何获取列表中具有 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]
您可以使用 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)]
您可以简单地使用字符串的
split
方法通过 ' '
(空格)分隔元素,并使用 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)
请注意,此脚本不会对输入进行任何验证,并且测试用例的长度(即
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")
不知道如何回答另一部分。
test_case=int(input())
while test_case!=0:
n=int(input())
a=list(map(int,input().split()))