【发布时间】:2015-11-03 18:29:13
【问题描述】:
下面的程序要求用户输入“测试用例的数量”,然后输入数字对其进行操作。最后,我想打印循环中每个操作的结果。
这是代码:
test_case = int(raw_input()) # User enter here the number of test case
for x in range(test_case):
n = int(raw_input())
while n ! = 1: # this is the operation
print n,1, #
if n % 2 == 0:
n = n//2
else:
n = n*3+1
如果我在测试用例中输入“2”并在每种情况下输入 2 个数字,则以下是输出。比如 22 和 64 会是这样的:
2
22 # the first number of the test case
22 1 11 1 34 1 17 1 52 1 26 1 13 1 40 1 20 1 10 1 5 1 16 1 8 1 4 1 2 1 # it prints the result immediately
64 # second test case
64 1 32 1 16 1 8 1 4 1 2 1 # it prints it immediately as the first
下面是预期的输出:
2
22
64
用户输入测试用例和测试用例所有编号后的输出为:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
64 32 16 8 4 2 1
我该如何解决这个问题?
注意:我尝试将结果保存在列表中并打印,但它会将所有结果打印在一行中。
【问题讨论】:
-
for result in results: print result -
请你把它放在我的代码中并添加它吗?我不知道我会把它放在哪里,谢谢
标签: python python-2.7 loops