编程是一项实践性非常强的技能,入门阶段通过解决一些经典题目可以有效地提升编程能力。以下列举了50个经典编程题目,涵盖了数据结构、算法、逻辑思维等多个方面,帮助你从基础到进阶,逐步提升编程技能。
1. 打印 Floyd 数组
def print_floyd_triangle(n):
for i in range(n):
for j in range(i + 1):
print(j * j, end=' ')
print()
print_floyd_triangle(5)
2. 求最大公约数
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(54, 24))
3. 求阶乘
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5))
4. 判断素数
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
print(is_prime(29))
5. 冒泡排序
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print(arr)
6. 选择排序
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[min_idx] > arr[j]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
arr = [64, 34, 25, 12, 22, 11, 90]
selection_sort(arr)
print(arr)
7. 插入排序
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
arr = [64, 34, 25, 12, 22, 11, 90]
insertion_sort(arr)
print(arr)
8. 快速排序
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
arr = [64, 34, 25, 12, 22, 11, 90]
print(quick_sort(arr))
9. 合并排序
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
arr = [64, 34, 25, 12, 22, 11, 90]
print(merge_sort(arr))
10. 查找算法
def linear_search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = [64, 34, 25, 12, 22, 11, 90]
x = 25
print("Element is present at index", linear_search(arr, x))
def binary_search(arr, x):
low = 0
high = len(arr) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if arr[mid] < x:
low = mid + 1
elif arr[mid] > x:
high = mid - 1
else:
return mid
return -1
arr = [2, 3, 4, 10, 40]
x = 10
print("Element is present at index", binary_search(arr, x))
11. 链表反转
class Node:
def __init__(self, data):
self.data = data
self.next = None
def reverse_linked_list(head):
prev = None
current = head
while current is not None:
next_node = current.next
current.next = prev
prev = current
current = next_node
head = prev
return head
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
head = reverse_linked_list(head)
while head:
print(head.data, end=' ')
head = head.next
12. 二叉树遍历
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
def inorder_traversal(root):
if root:
inorder_traversal(root.left)
print(root.val, end=' ')
inorder_traversal(root.right)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
inorder_traversal(root)
13. 求二叉树深度
def max_depth(root):
if root is None:
return 0
return max(max_depth(root.left), max_depth(root.right)) + 1
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Depth of tree is", max_depth(root))
14. 求二叉树宽度
from collections import deque
def width_of_tree(root):
if root is None:
return 0
queue = deque([root])
max_width = 0
while queue:
level_size = len(queue)
max_width = max(max_width, level_size)
for _ in range(level_size):
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return max_width
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Width of tree is", width_of_tree(root))
15. 判断二叉树是否平衡
def is_balanced(root):
if root is None:
return True
left_height = height(root.left)
right_height = height(root.right)
if abs(left_height - right_height) <= 1 and is_balanced(root.left) and is_balanced(root.right):
return True
return False
def height(root):
if root is None:
return 0
return max(height(root.left), height(root.right)) + 1
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree balanced?", is_balanced(root))
16. 判断二叉树是否对称
def is_symmetric(root):
if root is None:
return True
return is_mirror(root.left, root.right)
def is_mirror(left, right):
if left is None and right is None:
return True
if left is not None and right is not None:
return (left.val == right.val) and is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
return False
root = Node(1)
root.left = Node(2)
root.right = Node(2)
root.left.left = Node(3)
root.left.right = Node(4)
root.right.left = Node(4)
root.right.right = Node(3)
print("Is tree symmetric?", is_symmetric(root))
17. 判断二叉树是否包含某个值
def contains_tree(root, x):
if root is None:
return False
if root.val == x:
return True
return contains_tree(root.left, x) or contains_tree(root.right, x)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Does tree contain 5?", contains_tree(root, 5))
18. 判断二叉树是否为完全二叉树
def is_complete_binary_tree(root):
if root is None:
return True
queue = deque([root])
is_leaf = False
while queue:
node = queue.popleft()
if node.left:
if is_leaf:
return False
queue.append(node.left)
else:
is_leaf = True
if node.right:
if is_leaf:
return False
queue.append(node.right)
return True
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree complete binary tree?", is_complete_binary_tree(root))
19. 判断二叉树是否为满二叉树
def is_full_binary_tree(root):
if root is None:
return True
if root.left is None and root.right is None:
return True
if root.left and root.right:
return is_full_binary_tree(root.left) and is_full_binary_tree(root.right)
return False
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree full binary tree?", is_full_binary_tree(root))
20. 判断二叉树是否为完美二叉树
def is_perfect_binary_tree(root):
if root is None:
return True
if root.left is None and root.right is None:
return True
if root.left and root.right:
return is_perfect_binary_tree(root.left) and is_perfect_binary_tree(root.right) and (2 ** height(root.left) == count_nodes(root)) and (2 ** height(root.right) == count_nodes(root.right))
return False
def count_nodes(root):
if root is None:
return 0
return 1 + count_nodes(root.left) + count_nodes(root.right)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree perfect binary tree?", is_perfect_binary_tree(root))
21. 判断二叉树是否为完全二叉树(层序遍历)
def is_complete_binary_tree_level_order(root):
if root is None:
return True
queue = deque([root])
is_leaf = False
while queue:
node = queue.popleft()
if node.left:
if is_leaf:
return False
queue.append(node.left)
else:
is_leaf = True
if node.right:
if is_leaf:
return False
queue.append(node.right)
return True
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree complete binary tree?", is_complete_binary_tree_level_order(root))
22. 判断二叉树是否为二叉搜索树
def is_bst(root):
if root is None:
return True
if root.left and root.left.val > root.val:
return False
if root.right and root.right.val < root.val:
return False
return is_bst(root.left) and is_bst(root.right)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree binary search tree?", is_bst(root))
23. 判断二叉树是否为平衡二叉搜索树
def is_balanced_bst(root):
if root is None:
return True
left_height = height(root.left)
right_height = height(root.right)
if abs(left_height - right_height) <= 1 and is_balanced_bst(root.left) and is_balanced_bst(root.right):
return True
return False
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree balanced binary search tree?", is_balanced_bst(root))
24. 判断二叉树是否为对称二叉搜索树
def is_symmetric_bst(root):
if root is None:
return True
return is_mirror(root.left, root.right)
def is_mirror(left, right):
if left is None and right is None:
return True
if left is not None and right is not None:
return (left.val == right.val) and is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
return False
root = Node(1)
root.left = Node(2)
root.right = Node(2)
root.left.left = Node(3)
root.left.right = Node(4)
root.right.left = Node(4)
root.right.right = Node(3)
print("Is tree symmetric binary search tree?", is_symmetric_bst(root))
25. 判断二叉树是否为完全二叉搜索树
def is_complete_bst(root):
if root is None:
return True
queue = deque([root])
is_leaf = False
while queue:
node = queue.popleft()
if node.left:
if is_leaf:
return False
queue.append(node.left)
else:
is_leaf = True
if node.right:
if is_leaf:
return False
queue.append(node.right)
return is_bst(root)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree complete binary search tree?", is_complete_bst(root))
26. 判断二叉树是否为满二叉搜索树
def is_full_bst(root):
if root is None:
return True
if root.left is None and root.right is None:
return True
if root.left and root.right:
return is_full_bst(root.left) and is_full_bst(root.right)
return False
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree full binary search tree?", is_full_bst(root))
27. 判断二叉树是否为完美二叉搜索树
def is_perfect_bst(root):
if root is None:
return True
if root.left is None and root.right is None:
return True
if root.left and root.right:
return is_perfect_bst(root.left) and is_perfect_bst(root.right) and (2 ** height(root.left) == count_nodes(root)) and (2 ** height(root.right) == count_nodes(root.right))
return False
def count_nodes(root):
if root is None:
return 0
return 1 + count_nodes(root.left) + count_nodes(root.right)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree perfect binary search tree?", is_perfect_bst(root))
28. 判断二叉树是否为完全二叉搜索树(层序遍历)
def is_complete_bst_level_order(root):
if root is None:
return True
queue = deque([root])
is_leaf = False
while queue:
node = queue.popleft()
if node.left:
if is_leaf:
return False
queue.append(node.left)
else:
is_leaf = True
if node.right:
if is_leaf:
return False
queue.append(node.right)
return is_bst(root)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Is tree complete binary search tree?", is_complete_bst_level_order(root))
29. 判断二叉树是否为满二叉搜索树(层序遍历)
”`python def is_full_bst_level_order(root):
if root is None:
return True
queue = deque([root])
is_leaf = False
while queue:
node = queue.popleft()
if node.left:
if is_leaf:
return False
queue.append(node.left)
else:
is_leaf = True
if node.right:
if is_leaf:
return False
queue.append(node.right)
return is_full_bst(root)
root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left
