Python Exercises - TA Version
Bài Tập 1: Đếm Ngược (Countdown)
Đề bài: Nhập một số n. Đếm ngược từ n về 0 và in mỗi số trên một dòng.
Problem: Enter a number n. Count down from n to 0 and print each number on a line.
Ví dụ:
Enter n: 5Output:
5
4
3
2
1
0Bài Tập 2: In Số Lẻ từ 1 đến n (Print Odd Numbers from 1 to n)
Đề bài: Nhập một số n. In tất cả các số lẻ từ 1 đến n.
Problem: Enter a number n. Print all odd numbers from 1 to n.
Ví dụ:
Enter n: 10Output:
1
3
5
7
9Bài Tập 3: Đếm từ a đến b (Count from a to b)
Đề bài: Nhập hai số a và b. In các số từ a đến b.
Problem: Enter two numbers a and b. Print numbers from a to b.
Ví dụ:
Enter a: 3
Enter b: 7Output:
3
4
5
6
7Bài Tập 4: Tính Lũy Thừa Bằng Vòng Lặp (Calculate Power with Loop)
Đề bài: Nhập hai số a và n. Tính a^n (a mũ n) bằng vòng lặp (không dùng **).
Problem: Enter two numbers a and n. Calculate a^n (a to power n) using loop (no **).
Ví dụ:
Enter a: 2
Enter n: 5Output:
Result: 32Bài Tập 5: Hình Chữ Nhật Ngang (Horizontal Rectangle)
Đề bài: Nhập độ dài n. In n dấu sao trên cùng một dòng.
Problem: Enter length n. Print n stars on the same line.
Ví dụ:
Enter n: 8Output:
* * * * * * * *Bài Tập 6: Đếm Số Chia Hết cho 5 (Count Numbers Divisible by 5)
Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số chia hết cho 5.
Problem: Enter n integers. Count how many are divisible by 5.
Ví dụ:
Enter count: 5
Numbers: 10, 13, 25, 7, 30Output:
Count: 3Bài Tập 7: Tổng Số Chẵn (Sum of Even Numbers)
Đề bài: Nhập n số nguyên. Tính tổng CHỈ các số chẵn.
Problem: Enter n integers. Calculate sum of ONLY even numbers.
Ví dụ:
Enter count: 5
Numbers: 10, 15, 22, 7, 8Output:
Sum: 40Bài Tập 8: Đếm Số Có Hai Chữ Số (Count Two-Digit Numbers)
Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số có hai chữ số (10-99 hoặc -99 đến -10).
Problem: Enter n integers. Count how many have two digits (10-99 or -99 to -10).
Ví dụ:
Enter count: 6
Numbers: 5, 25, 100, -50, 3, 99Output:
Count: 3Bài Tập 9: Đếm Số Chia Hết cho 2 HOẶC 3 (Count Numbers Divisible by 2 OR 3)
Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số chia hết cho 2 hoặc 3.
Problem: Enter n integers. Count how many are divisible by 2 or 3.
Ví dụ:
Enter count: 5
Numbers: 10, 13, 9, 7, 12Output:
Count: 3Bài Tập 10: Số Lớn Hơn 10 (Numbers Greater Than 10)
Đề bài: Nhập n số nguyên. Chỉ in ra các số lớn hơn 10.
Problem: Enter n integers. Print only numbers greater than 10.
Ví dụ:
Enter count: 5
Numbers: 5, 15, 8, 22, 3Output:
15
22Bài Tập 11: Bậc Thang Số (Number Staircase)
Đề bài: Nhập số n. In bậc thang số: dòng 1 là '1', dòng 2 là '22', dòng 3 là '333', v.v.
Problem: Enter n. Print number staircase: row 1 is '1', row 2 is '22', row 3 is '333', etc.
Ví dụ:
Enter n: 5Output:
1
22
333
4444
55555Bài Tập 12: Chuyển Đổi Giờ-Phút-Giây (Convert Hours-Minutes-Seconds)
Đề bài: Nhập n số giây. Chuyển mỗi số thành định dạng 'H giờ M phút S giây'.
Problem: Enter n seconds. Convert each to format 'H hours M minutes S seconds'.
Ví dụ:
Enter count: 2
Seconds: 3665, 7200Output:
1 hours 1 minutes 5 seconds
2 hours 0 minutes 0 secondsBài Tập 13: Bảng Nhân Tùy Chỉnh (Custom Multiplication Table)
Đề bài: Nhập hai số n và m. In bảng nhân của n từ 1 đến m. Nếu kết quả chẵn, thêm dấu * ở cuối.
Problem: Enter two numbers n and m. Print multiplication table of n from 1 to m. If result is even, add * at the end.
Ví dụ:
Enter n: 3
Enter m: 7Output:
3 x 1 = 3
3 x 2 = 6 *
3 x 3 = 9
3 x 4 = 12 *
3 x 5 = 15
3 x 6 = 18 *
3 x 7 = 21Bài Tập 14: Đếm Số Theo Điều Kiện Phức Tạp (Count with Complex Condition)
Đề bài: Nhập n số nguyên. Đếm số lượng số chia hết cho 3 NHƯNG KHÔNG chia hết cho 6.
Problem: Enter n integers. Count numbers divisible by 3 BUT NOT divisible by 6.
Ví dụ:
Enter count: 6
Numbers: 3, 6, 9, 12, 15, 18Output:
Count: 3Bài Tập 15: Tìm Khoảng Cách Lớn Nhất (Find Maximum Gap)
Đề bài: Nhập n số nguyên liên tiếp. Tìm khoảng cách lớn nhất giữa hai số liên tiếp.
Problem: Enter n consecutive integers. Find the maximum gap between two consecutive numbers.
Ví dụ:
Enter count: 5
Numbers: 3, 7, 10, 12, 20Output:
Max gap: 8Bài Tập 16: Đếm Chữ Số (Count Digits)
Đề bài: Nhập một số nguyên. Đếm số lượng chữ số của số đó (bao gồm cả số âm).
Problem: Enter an integer. Count the number of digits (including negatives).
Ví dụ:
Enter number: -4567Output:
Digits: 4Bài Tập 17: Tổng Các Chữ Số (Sum of Digits)
Đề bài: Nhập một số nguyên dương. Tính tổng các chữ số của số đó.
Problem: Enter a positive integer. Calculate sum of its digits.
Ví dụ:
Enter number: 12345Output:
Sum: 15Bài Tập 18: Số Hoàn Hảo (Perfect Number)
Đề bài: Nhập một số nguyên dương. Kiểm tra số đó có phải số hoàn hảo không (tổng các ước thực sự = chính nó). VD: 6 = 1+2+3.
Problem: Enter a positive integer. Check if it's a perfect number (sum of proper divisors equals itself). Ex: 6 = 1+2+3.
Ví dụ:
Enter number: 28Output:
PerfectBài Tập 19: Đoán Số Có Giới Hạn (Number Guessing with Limit)
Đề bài: Chương trình nghĩ ra một số từ 1-50 (người dùng nhập số bí mật). Người chơi có tối đa 7 lần đoán. Mỗi lần đoán sai, cho gợi ý 'cao hơn' hoặc 'thấp hơn'. Nếu đoán đúng hoặc hết lượt, kết thúc game.
Problem: Program thinks of a number from 1-50 (user enters secret number). Player has max 7 guesses. For each wrong guess, give hint 'higher' or 'lower'. If guessed correctly or out of attempts, end game.
Ví dụ:
Secret number: 25
Guess: 30
Lower!
Guess: 20
Higher!
Guess: 25
Correct!Output:
Lower!
Higher!
Correct! You won in 3 attempts.Bài Tập 20: Hình Kim Cương Số (Number Diamond)
Đề bài: Nhập n (lẻ). In hình kim cương bằng số: nửa trên tăng 1,3,5..., nửa dưới giảm.
Problem: Enter n (odd). Print diamond with numbers: upper half increases 1,3,5..., lower half decreases.
Ví dụ:
Enter n: 5Output:
1
123
12345
123
1ĐÁP ÁN / SOLUTIONS
Solution 1: Đếm Ngược
Giải thích: Vòng lặp for đếm ngược từ n về 0. Mỗi lần lặp in ra số i.
Explanation: For loop counts down from n to 0. Each iteration prints number i.
Code:
pythonn = int(input("Enter n: ")) for i in range(n, -1, -1): print(i)
Solution 2: In Số Lẻ từ 1 đến n
Giải thích: range(1, n+1, 2) tạo dãy số bắt đầu từ 1, tăng 2 mỗi lần (chỉ số lẻ).
Explanation: range(1, n+1, 2) creates sequence starting at 1, incrementing by 2 (odd numbers only).
Code:
pythonn = int(input("Enter n: ")) for i in range(1, n + 1, 2): print(i)
Solution 3: Đếm từ a đến b
Giải thích: range(a, b+1) tạo dãy từ a đến b.
Explanation: range(a, b+1) creates sequence from a to b.
Code:
pythona = int(input("Enter a: ")) b = int(input("Enter b: ")) for i in range(a, b + 1): print(i)
Solution 4: Tính Lũy Thừa Bằng Vòng Lặp
Giải thích: Nhân a với chính nó n lần.
Explanation: Multiply a by itself n times.
Code:
pythona = int(input("Enter a: ")) n = int(input("Enter n: ")) result = 1 for i in range(n): result *= a print(f"Result: {result}")
Solution 5: Hình Chữ Nhật Ngang
Giải thích: In dấu sao với end=' ' để ở cùng dòng.
Explanation: Print stars with end=' ' to stay on same line.
Code:
pythonn = int(input("Enter n: ")) for i in range(n): print("*", end=" ") print()
Solution 6: Đếm Số Chia Hết cho 5
Giải thích: Kiểm tra chia hết cho 5 bằng modulo.
Explanation: Check divisibility by 5 using modulo.
Code:
pythonn = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if number % 5 == 0: count += 1 print(f"Count: {count}")
Solution 7: Tổng Số Chẵn
Giải thích: Chỉ cộng số chẵn vào tổng.
Explanation: Only add even numbers to sum.
Code:
pythonn = int(input("Enter count: ")) total = 0 for i in range(n): number = int(input("Enter number: ")) if number % 2 == 0: total += number print(f"Sum: {total}")
Solution 8: Đếm Số Có Hai Chữ Số
Giải thích: Kiểm tra hai khoảng cho số dương và số âm.
Explanation: Check two ranges for positive and negative numbers.
Code:
pythonn = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if (10 <= number <= 99) or (-99 <= number <= -10): count += 1 print(f"Count: {count}")
Solution 9: Đếm Số Chia Hết cho 2 HOẶC 3
Giải thích: Dùng OR để kiểm tra một trong hai điều kiện.
Explanation: Use OR to check one of two conditions.
Code:
pythonn = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if number % 2 == 0 or number % 3 == 0: count += 1 print(f"Count: {count}")
Solution 10: Số Lớn Hơn 10
Giải thích: Nhập từng số, kiểm tra điều kiện lớn hơn 10, in ngay nếu đúng.
Explanation: Input each number, check if greater than 10, print immediately if true.
Code:
pythonn = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) if number > 10: print(number)
Solution 11: Bậc Thang Số
Giải thích: Vòng ngoài cho dòng, vòng trong in số của dòng đó nhiều lần.
Explanation: Outer loop for rows, inner loop prints row number multiple times.
Code:
pythonn = int(input("Enter n: ")) for i in range(1, n + 1): for j in range(i): print(i, end="") print()
Solution 12: Chuyển Đổi Giờ-Phút-Giây
Giải thích: Dùng phép chia và modulo để tách thành giờ, phút, giây.
Explanation: Use division and modulo to separate into hours, minutes, seconds.
Code:
pythonn = int(input("Enter count: ")) for i in range(n): total_seconds = int(input("Seconds: ")) hours = total_seconds // 3600 remaining = total_seconds % 3600 minutes = remaining // 60 seconds = remaining % 60 print(f"{hours} hours {minutes} minutes {seconds} seconds")
Solution 13: Bảng Nhân Tùy Chỉnh
Giải thích: Vòng lặp từ 1 đến m. Mỗi lần tính tích, kiểm tra chẵn lẻ, và in với format tương ứng.
Explanation: Loop from 1 to m. Each time calculate product, check even/odd, and print with corresponding format.
Code:
pythonn = int(input("Enter n: ")) m = int(input("Enter m: ")) for i in range(1, m + 1): result = n * i if result % 2 == 0: print(f"{n} x {i} = {result} *") else: print(f"{n} x {i} = {result}")
Solution 14: Đếm Số Theo Điều Kiện Phức Tạp
Giải thích: Kết hợp hai điều kiện bằng AND và NOT.
Explanation: Combine two conditions using AND and NOT.
Code:
pythonn = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if number % 3 == 0 and number % 6 != 0: count += 1 print(f"Count: {count}")
Solution 15: Tìm Khoảng Cách Lớn Nhất
Giải thích: So sánh khoảng cách giữa các số liên tiếp.
Explanation: Compare gaps between consecutive numbers.
Code:
pythonn = int(input("Enter count: ")) max_gap = 0 prev = int(input("Enter number: ")) for i in range(n - 1): current = int(input("Enter number: ")) gap = current - prev if gap > max_gap: max_gap = gap prev = current print(f"Max gap: {max_gap}")
Solution 16: Đếm Chữ Số
Giải thích: Lấy giá trị tuyệt đối, chia 10 cho đến hết và đếm.
Explanation: Take absolute value, divide by 10 until zero and count.
Code:
pythonnum = int(input("Enter number: ")) num = abs(num) # Take absolute value if num == 0: count = 1 else: count = 0 while num > 0: count += 1 num = num // 10 print(f"Digits: {count}")
Solution 17: Tổng Các Chữ Số
Giải thích: Lấy từng chữ số và cộng vào tổng.
Explanation: Extract each digit and add to sum.
Code:
pythonnum = int(input("Enter number: ")) digit_sum = 0 while num > 0: digit = num % 10 digit_sum += digit num = num // 10 print(f"Sum: {digit_sum}")
Solution 18: Số Hoàn Hảo
Giải thích: Tính tổng các ước thực sự rồi so sánh với số gốc.
Explanation: Calculate sum of proper divisors then compare with original number.
Code:
pythonnumber = int(input("Enter number: ")) sum_divisors = 0 for i in range(1, number): if number % i == 0: sum_divisors += i if sum_divisors == number: print("Perfect") else: print("Not perfect")
Solution 19: Đoán Số Có Giới Hạn
Giải thích: Vòng lặp tối đa 7 lần. Mỗi lần so sánh guess với secret, đưa gợi ý. Dùng break để thoát khi đoán đúng.
Explanation: Loop maximum 7 times. Each time compare guess with secret, give hint. Use break to exit when guessed correctly.
Code:
pythonsecret = int(input("Secret number (1-50): ")) max_attempts = 7 attempts = 0 for i in range(max_attempts): guess = int(input("Guess: ")) attempts += 1 if guess == secret: print(f"Correct! You won in {attempts} attempts.") break elif guess > secret: print("Lower!") else: print("Higher!") if attempts == max_attempts: print(f"Game over! The number was {secret}.")
Solution 20: Hình Kim Cương Số
Giải thích: In nửa trên tăng dần, nửa dưới giảm dần, mỗi dòng in số liên tiếp.
Explanation: Print upper half increasing, lower half decreasing, each row prints consecutive numbers.
Code:
pythonn = int(input("Enter n (odd): ")) mid = (n + 1) // 2 # Upper half for i in range(1, mid + 1): for j in range(1, 2 * i): print(j, end="") print() # Lower half for i in range(mid - 1, 0, -1): for j in range(1, 2 * i): print(j, end="") print()