您的答案為: (())
正確答案為:
用ideone跑 結果跟測資一樣
是換行的問題?
python code:
import sys
def dfs(step,n,a,left,right):
if step == n:
if left == right:
str1 = ''.join(a)
print(str1)
return
c = ['(',')']
if left > right:
for i in range(len(c)):
a.append(c[i])
if i == 0:
dfs(step + 1,n,a,left + 1,right)
else:
dfs(step + 1,n,a,left,right + 1)
a.pop()
return
else:
a.append('(')
dfs(step + 1,n,a,left + 1,right)
a.pop()
return
return
while True:
try:
n = int(input())
a = ['(']
left = 1
right = 0
dfs(1,n * 2,a,left,right)
print()
except: break