[英]Python Output Ambiguity
因此,我编写了以下代码来生成组合(n选择k) :
#!/usr/bin/env python3
combinations = []
used = []
def init(num):
for i in range(0, num+1):
combinations.append(0)
used.append(0)
def printSolution(coef):
for i in range(1, coef+1):
print(combinations[i], " ", end=' ')
print("\n")
def generateCombinations(which, what, num, coef):
combinations[which] = what
used[what] = 1
if which == coef:
printSolution(coef)
else:
for next in range(what+1, num+1):
if not used[next]:
generateCombinations(which+1, next, num, coef)
used[what] = 0
n = int(input("Give n:"))
k = int(input("Give k:"))
if k <= n:
init(n)
for i in range(1, n+1):
generateCombinations(1, i, n, k)
input()
它的问题在于,当它输出文本时,而不是像下面这样写一行:
Give n:4
Give k:3
1 2 3
1 2 4
1 3 4
2 3 4
它实际上是这样输出的:
Give n:4
Give k:3
1 2 3
1 2 4
1 3 4
2 3 4
我的问题是,为什么实际上会发生这种情况,以及如何解决? 我必须说我是python的新手,所以不要对我苛刻。 先感谢您。
print("\n")
打印换行符,并以换行符结尾。 因此,两个换行符
删除其中之一的两个选项:
print()
要么
print("\n", end="")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.