无法证明 python 中的输出
Not able to let justify the output in python
在 Code Jam 2020 的一个问题中,我得到以下输出
Case #1: CJC
Case #2: IMPOSSIBLE
Case #3: CJJCC
Case #4: CC
同时,真正的输出应该是这样的:
Case #1: CJC
Case #2: IMPOSSIBLE
Case #3: JCCJJ
Case #4: CC
谁能告诉我如何纠正这个问题?我正在使用 python 3。
我的打印语句是这样的:print('Case #{}: {}'.format(count, output))
下面是我的代码。
def checkOverlap(task1,task2):
start1 = int(task1[0])
start2 = int(task2[0])
end1 = int(task1[1])
end2 = int(task2[1])
if(end2 > start1 and start2 < end1):
return True
else:
return False
T= input()
test = int(T)
countTest = 0
while(countTest<test):
A = input()
countA = int(A)
lim = 0
activity = []
while(lim<countA):
activity.append(input().split(" "))
lim += 1
flag = True
output=' '*countA
output+='C'
J=[]
C=[activity[0]]
impossible = False
for i in range(0,countA-1):
for j in range(i+1,countA):
if (C.__contains__(activity[i]) and J.__contains__(activity[j])) or \
(J.__contains__(activity[i]) and C.__contains__(activity[j])):
continue
check = checkOverlap(activity[i],activity[j])
if(check == False):
if C.__contains__(activity[i]):
if not C.__contains__(activity[j]):
C.append(activity[j])
else:
if not J.__contains__(activity[j]):
J.append(activity[j])
else:
if (C.__contains__(activity[i])):
if(C.__contains__(activity[j])):
impossible = True
else:
if not J.__contains__(activity[j]):
J.append(activity[j])
else:
if J.__contains__(activity[j]):
impossible = True
else:
if not C.__contains__(activity[j]):
C.append(activity[j])
if impossible == True:
output = 'IMPOSSIBLE'
else:
for i in range(1,countA):
if C.__contains__(activity[i]):
output += 'C'
else:
output += 'J'
count = (countTest + 1)
print('Case #{}: {}'.format(count, output))
countTest += 1
为什么你的代码中有这一行?
output=' '*countA
如果你摆脱它(切换到,比如说,output = ""
),你会更快乐。你所做的是以一堆空格开始字符串,然后打印出来。 (您添加的所有其他内容都会被追加,因此您的最终字符串将是 " CJJCC"
。)这会扰乱您想要的对齐方式。
一开始你有 output=' '*countA
,在 CJC
和类似的情况下你只做 output += 'C'
和 output += 'J'
所以你会发现 countA
该字符串开头的空格
在 Code Jam 2020 的一个问题中,我得到以下输出
Case #1: CJC
Case #2: IMPOSSIBLE
Case #3: CJJCC
Case #4: CC
同时,真正的输出应该是这样的:
Case #1: CJC
Case #2: IMPOSSIBLE
Case #3: JCCJJ
Case #4: CC
谁能告诉我如何纠正这个问题?我正在使用 python 3。
我的打印语句是这样的:print('Case #{}: {}'.format(count, output))
下面是我的代码。
def checkOverlap(task1,task2):
start1 = int(task1[0])
start2 = int(task2[0])
end1 = int(task1[1])
end2 = int(task2[1])
if(end2 > start1 and start2 < end1):
return True
else:
return False
T= input()
test = int(T)
countTest = 0
while(countTest<test):
A = input()
countA = int(A)
lim = 0
activity = []
while(lim<countA):
activity.append(input().split(" "))
lim += 1
flag = True
output=' '*countA
output+='C'
J=[]
C=[activity[0]]
impossible = False
for i in range(0,countA-1):
for j in range(i+1,countA):
if (C.__contains__(activity[i]) and J.__contains__(activity[j])) or \
(J.__contains__(activity[i]) and C.__contains__(activity[j])):
continue
check = checkOverlap(activity[i],activity[j])
if(check == False):
if C.__contains__(activity[i]):
if not C.__contains__(activity[j]):
C.append(activity[j])
else:
if not J.__contains__(activity[j]):
J.append(activity[j])
else:
if (C.__contains__(activity[i])):
if(C.__contains__(activity[j])):
impossible = True
else:
if not J.__contains__(activity[j]):
J.append(activity[j])
else:
if J.__contains__(activity[j]):
impossible = True
else:
if not C.__contains__(activity[j]):
C.append(activity[j])
if impossible == True:
output = 'IMPOSSIBLE'
else:
for i in range(1,countA):
if C.__contains__(activity[i]):
output += 'C'
else:
output += 'J'
count = (countTest + 1)
print('Case #{}: {}'.format(count, output))
countTest += 1
为什么你的代码中有这一行?
output=' '*countA
如果你摆脱它(切换到,比如说,output = ""
),你会更快乐。你所做的是以一堆空格开始字符串,然后打印出来。 (您添加的所有其他内容都会被追加,因此您的最终字符串将是 " CJJCC"
。)这会扰乱您想要的对齐方式。
一开始你有 output=' '*countA
,在 CJC
和类似的情况下你只做 output += 'C'
和 output += 'J'
所以你会发现 countA
该字符串开头的空格