import sys
# 接收输⼊
for line in sys.stdin:
a, b = line.split(' ')
# 输出
print(int(a) + int(b))
sys.stdin ------输入流,一行一行来
while 1:
try :
a, b = input().split(' ')
print(int(a) + int(b))
except:
break
总结
标准读取格式
while 1:
try:
a, b = input()
# print(int(a) + int(b))
except:
break
input()-----读取一行
input().split()--------将一行的 1 2 3 分开-------['1', 2', '3']
变成[1,2,3]:
res = list(map(int,input().split()))
链表结构
class LinkedNode:
def __init__(self, val = 0, next = None):
self.val = val
self.next = next
def reverseList(head: LinkedNode) -> LinkedNode:
temp = None # 保存cur的下一个节点
cur = head
pre = None
while cur:
temp = cur.next
cur.next = pre # 翻转操作
pre = cur # 更新pre 和 cur指针
cur = temp
return pre
def printLinkedList(head: LinkedNode):
cur = head
while cur:
print(cur.val, end = " ")
cur = cur.next
print()
if __name__ == "__main__":
while 1:
try:
s = list(map(int,input().split()))
n = s[0]
nums = s[1:]
except:
break
if s[0] == 0:
print("list is empty")
break
dummyHead = LinkedNode(0)
cur = dummyHead
for i in range(n): # 开始构造listedlist
cur.next = LinkedNode(nums[i])
cur = cur.next
printLinkedList(dummyHead.next) # 打印链表
printLinkedList(reverseList(dummyHead.next)) # 打印翻转后的链表
树的结构
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def preorder(root):
if not root:
return []
left = preorder(root.left)
right = preorder(root.right)
return [root.val] + left + right
def inorder(root):
if not root:
return []
left = inorder(root.left)
right = inorder(root.right)
return left + [root.val] + right
def postorder(root):
if not root:
return []
left = postorder(root.left)
right = postorder(root.right)
return left + right + [root.val]
n = int(input())
nodes = [None] * (n + 1)
line_in = []
for i in range(n):
line = input().split()
val, left, right = line[0], int(line[1]), int(line[2])
if not nodes[i+1]:
node = Node(val)
nodes[i + 1] = node
else:
# 更新val
nodes[i + 1].val = val
if left != 0:
# 创建一个node,放入nodes列表
node_left = Node('x')
nodes[left] = node_left
nodes[i + 1].left = node_left
if right != 0:
node_right = Node('x')
nodes[right] = node_right
nodes[i + 1].right = node_right
root = nodes[1]
pre = preorder(root)
ino = inorder(root)
post = postorder(root)
print(''.join(pre))
print(''.join(ino))
print(''.join(post))
class TreeNode:
def __init__(self, val=None, left=None, right=None):
self.val = val
self.left = left
self.right = right
def postorder_traversal(root):
if not root:
return []
left = postorder_traversal(root.left)
right = postorder_traversal(root.right)
return left + right + [root.val]
def buildTree(preorder, inorder):
# 1. 递归的终止条件
if len(preorder) == 0:
return None
# 2. root
root_val = preorder[0]
root = TreeNode(root_val)
# 3. find the index of the root
index = inorder.index(root_val)
# 4. cut the inorder
inorder_left = inorder[:index]
inorder_right = inorder[index + 1: ]
res = len(inorder_left)
preorder_left = preorder[1:res+1]
preorder_right = preorder[res+1:]
# 5. traversal
root.left = buildTree(preorder_left, inorder_left)
root.right = buildTree(preorder_right, inorder_right)
return root
while 1:
try:
preorder, inorder = map(str,input().split(" "))
if not preorder or not inorder:
break
root = buildTree(preorder, inorder)
postorder = postorder_traversal(root)
print(''.join(postorder))
except EOFError:
break