[英]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.