Level 1 - Cơ Bản / Basics
Bài Tập 1: Bao Nhiêu Số Lớn Hơn Số Cuối (How Many Beat the Last One)
Đề bài: Đọc N số nguyên. In ra có bao nhiêu số LỚN HƠN số cuối cùng của dãy.
Problem: Read N integers. Print how many of them are LARGER than the last number in the list.
Ví dụ:
5
10 20 30 40 25Output:
Larger: 2Bài Tập 2: In Ngược Một Vector (Reverse a Vector)
Đề bài: Đọc N số nguyên vào một vector rồi in chúng theo thứ tự ngược lại trên một dòng.
Problem: Read N integers into a vector, then print them in reverse order on one line.
Ví dụ:
5
10 20 30 40 50Output:
50 40 30 20 10Bài Tập 3: Kiểm Tra Khóa Tồn Tại (Check if a Key Exists)
Đề bài: Cho sẵn map ages với Alice = 14 và Bob = 15. Đọc một tên; nếu có thì in tuổi, ngược lại in "Not found".
Problem: Given a map ages with Alice = 14 and Bob = 15, read a name. If it exists print the age, otherwise print "Not found".
Ví dụ:
DanOutput:
Not foundBài Tập 4: Ký Tự Đầu, Cuối Và Độ Dài (First, Last and Length)
Đề bài: Đọc một từ và in ký tự đầu, ký tự cuối và độ dài của nó.
Problem: Read a word and print its first character, its last character, and its length.
Ví dụ:
bananaOutput:
First: b
Last: a
Length: 6Bài Tập 5: Đảo Ngược Chuỗi (Reverse a String)
Đề bài: Đọc một từ và in nó theo thứ tự ngược lại.
Problem: Read a word and print it backwards.
Ví dụ:
PythonOutput:
nohtyPLevel 2 - Trung Bình / Intermediate
Bài Tập 6: Bỏ Điểm Thấp Nhất (Drop the Lowest Score)
Đề bài: Đọc N điểm số. Bỏ đi MỘT điểm thấp nhất, rồi in điểm trung bình của những điểm còn lại.
Problem: Read N scores. Drop ONE lowest score, then print the average of the scores that remain.
Ví dụ:
5
8 9 10 7 7Output:
Average: 8.5Bài Tập 7: Học Sinh Điểm Cao Nhất (Best Student)
Đề bài: Đọc 3 cặp tên và điểm, rồi in ra ai đạt điểm cao nhất và điểm là bao nhiêu.
Problem: Read 3 name-score pairs, then print who scored the highest and what that score was.
Ví dụ:
Linh 7
Nam 8
Chi 9Output:
Best: Chi (9)Bài Tập 8: Căn Thẳng Hóa Đơn (Align the Receipt)
Đề bài: Đọc N món, mỗi món gồm tên và giá (số nguyên). In mỗi món trên một dòng rộng ĐÚNG 20 ký tự: tên ở đầu dòng, giá ở cuối dòng, khoảng giữa lấp đầy bằng dấu chấm.
Problem: Read N items, each with a name and an integer price. Print each item on a line EXACTLY 20 characters wide: the name at the start, the price at the end, and the gap between them filled with dots.
Ví dụ:
3
Apple 25000
Bread 12000
Milk 5000Output:
Apple..........25000
Bread..........12000
Milk............5000Bài Tập 9: Đọc Bảng Giá Dạng tên:giá (Read a name:price List)
Đề bài: Đọc các mục viết dưới dạng tên:giá cho tới hết dữ liệu vào. In tổng số tiền và tên món đắt nhất.
Problem: Read items written as name:price until the input ends. Print the total price and the name of the most expensive item.
Ví dụ:
apple:5000 bread:12000 milk:20000Output:
Total: 37000
Priciest: milkBài Tập 10: Các Từ Bắt Đầu Bằng Một Chữ Cái (Words Starting With a Letter)
Đề bài: Dòng đầu là một chữ cái, các dòng sau là các từ. In những từ bắt đầu bằng chữ cái đó và tổng số từ tìm được.
Problem: The first input is a letter, the rest are words. Print every word starting with that letter and the total count found.
Ví dụ:
b
banana bread and butterOutput:
banana
bread
butter
Count: 3Level 3 - Nâng Cao / Advanced
Bài Tập 11: Danh Sách Từ Không Trùng (Unique Words in Order)
Đề bài: Đọc các từ có lặp lại; in các từ khác nhau theo đúng thứ tự xuất hiện lần đầu, ngăn cách bằng dấu phẩy và khoảng trắng.
Problem: Read words that may repeat; print the distinct words in the order they first appear, separated by a comma and a space.
Ví dụ:
the cat the dog the birdOutput:
the, cat, dog, birdBài Tập 12: Kiểm Tra Chuỗi Đối Xứng (Palindrome Check)
Đề bài: Đọc một dòng; bỏ khoảng trắng và không phân biệt hoa thường, in "Yes" nếu đọc xuôi ngược giống nhau, ngược lại in "No".
Problem: Read a line; ignoring spaces and letter case, print "Yes" if it reads the same forwards and backwards, otherwise print "No".
Ví dụ:
Never odd or evenOutput:
YesBài Tập 13: Tính Tiền Giỏ Hàng (Shopping Cart Total)
Đề bài: Cho bảng giá apple = 5000, bread = 12000, milk = 20000. Đọc số món cần mua, rồi mỗi món gồm tên và số lượng; in thành tiền từng dòng và tổng cộng.
Problem: Given prices apple = 5000, bread = 12000, milk = 20000, read how many items are bought, then each item's name and quantity; print each line total and the grand total.
Ví dụ:
2
apple 3
bread 1Output:
apple x3 = 15000
bread x1 = 12000
Total: 27000ĐÁP ÁN / SOLUTIONS
Level 1 - Đáp Án
Solution 1: Bao Nhiêu Số Lớn Hơn Số Cuối
Giải thích: Không thể vừa đọc vừa đếm vì mốc để so sánh mãi tới cuối mới xuất hiện — vector giữ dữ liệu lại cho lượt duyệt thứ hai.
Explanation: Counting while reading is impossible because the value being compared against only arrives at the very end — the vector holds the data for a second pass.
Code:
cpp#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> nums; for (int i = 0; i < n; i++) { int x; cin >> x; nums.push_back(x); } int last = nums[n - 1]; int count = 0; for (int x : nums) { if (x > last) { count++; } } cout << "Larger: " << count << endl; return 0; }
Solution 2: In Ngược Một Vector
Giải thích: Vòng lặp đi từ chỉ số cuối về 0; phần tử cuối là nums[nums.size() - 1].
Explanation: The loop walks from the last index down to 0; the last element is nums[nums.size() - 1].
Code:
cpp#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> nums; for (int i = 0; i < n; i++) { int x; cin >> x; nums.push_back(x); } for (int i = nums.size() - 1; i >= 0; i--) { cout << nums[i] << " "; } cout << endl; return 0; }
Solution 3: Kiểm Tra Khóa Tồn Tại
Giải thích: Khác Python, truy cập map[khóa-chưa-có] KHÔNG báo lỗi mà lặng lẽ tạo khóa với giá trị 0 — vì vậy phải dùng count.
Explanation: Unlike Python, map[missing-key] does NOT error — it silently creates the key with value 0, which is why count is used first.
Code:
cpp#include <iostream> #include <map> #include <string> using namespace std; int main() { map<string, int> ages = {{"Alice", 14}, {"Bob", 15}}; string name; cin >> name; if (ages.count(name)) { cout << name << ": " << ages[name] << endl; } else { cout << "Not found" << endl; } return 0; }
Solution 4: Ký Tự Đầu, Cuối Và Độ Dài
Giải thích: std::string truy cập như mảng ký tự, nhưng phải tự tính chỉ số cuối bằng size() - 1.
Explanation: A std::string is indexed like an array of characters, but the last index must be computed as size() - 1.
Code:
cpp#include <iostream> #include <string> using namespace std; int main() { string word; cin >> word; cout << "First: " << word[0] << endl; cout << "Last: " << word[word.size() - 1] << endl; cout << "Length: " << word.size() << endl; return 0; }
Solution 5: Đảo Ngược Chuỗi
Giải thích: Không có slicing trong C++, nên phải viết vòng lặp đi ngược qua từng chỉ số.
Explanation: C++ has no slicing, so a loop walking the indexes backwards does the job.
Code:
cpp#include <iostream> #include <string> using namespace std; int main() { string word; cin >> word; for (int i = word.size() - 1; i >= 0; i--) { cout << word[i]; } cout << endl; return 0; }
Level 2 - Đáp Án
Solution 6: Bỏ Điểm Thấp Nhất
Giải thích: Chỉ trừ MỘT lần giá trị nhỏ nhất: ví dụ trên có hai điểm 7, bỏ một điểm thì điểm 7 còn lại vẫn được tính.
Explanation: The lowest value is subtracted only ONCE: the example has two 7s, and dropping one still leaves the other 7 counting towards the average.
Code:
cpp#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> scores; for (int i = 0; i < n; i++) { int x; cin >> x; scores.push_back(x); } int sum = 0; int lowest = scores[0]; for (int x : scores) { sum += x; if (x < lowest) lowest = x; } double average = (double)(sum - lowest) / (n - 1); cout << "Average: " << average << endl; return 0; }
Solution 7: Học Sinh Điểm Cao Nhất
Giải thích: map sắp xếp theo KHÓA chứ không theo giá trị, nên muốn tìm điểm cao nhất vẫn phải duyệt hết.
Explanation: A map is sorted by KEY, not by value, so finding the highest score still needs a full pass.
Code:
cpp#include <iostream> #include <map> #include <string> using namespace std; int main() { map<string, int> scores; for (int i = 0; i < 3; i++) { string name; int score; cin >> name >> score; scores[name] = score; } string bestName = ""; int bestScore = -1; for (auto& [name, score] : scores) { if (score > bestScore) { bestScore = score; bestName = name; } } cout << "Best: " << bestName << " (" << bestScore << ")" << endl; return 0; }
Solution 8: Căn Thẳng Hóa Đơn
Giải thích: Muốn căn thẳng thì phải biết giá CHIẾM bao nhiêu ký tự — to_string biến số thành chuỗi để đo được bằng .size(), điều mà cout << price không cho biết.
Explanation: Aligning needs the WIDTH of the price, not its value — to_string turns the number into a string so .size() can measure it, which cout << price never reveals.
Code:
cpp#include <iostream> #include <string> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string name; int price; cin >> name >> price; string priceText = to_string(price); int dots = 20 - (int)name.size() - (int)priceText.size(); cout << name; for (int j = 0; j < dots; j++) { cout << "."; } cout << priceText << endl; } return 0; }
Solution 9: Đọc Bảng Giá Dạng tên:giá
Giải thích: Giá nằm LẪN bên trong chuỗi nên phải cắt ra bằng substr rồi mới đổi sang số bằng stoi — không thể cộng thẳng chuỗi "5000".
Explanation: The price arrives buried inside the text, so substr cuts it out and stoi turns it into a number — the string "5000" cannot be added directly.
Code:
cpp#include <iostream> #include <string> using namespace std; int main() { int total = 0; string priciest = ""; int highest = -1; string item; while (cin >> item) { int pos = item.find(':'); string name = item.substr(0, pos); int price = stoi(item.substr(pos + 1)); total += price; if (price > highest) { highest = price; priciest = name; } } cout << "Total: " << total << endl; cout << "Priciest: " << priciest << endl; return 0; }
Solution 10: Các Từ Bắt Đầu Bằng Một Chữ Cái
Giải thích: C++ không có startswith, nhưng so sánh word[0] là đủ khi chỉ cần kiểm tra một ký tự.
Explanation: C++ has no startswith, but comparing word[0] is enough when only one character matters.
Code:
cpp#include <iostream> #include <string> using namespace std; int main() { char letter; cin >> letter; int count = 0; string word; while (cin >> word) { if (word[0] == letter) { cout << word << endl; count++; } } cout << "Count: " << count << endl; return 0; }
Level 3 - Đáp Án
Solution 11: Danh Sách Từ Không Trùng
Giải thích: Không thể chỉ dùng map vì map sắp xếp theo khóa — cần thêm vector để giữ thứ tự xuất hiện.
Explanation: A map alone will not do, because it sorts by key — the vector is what preserves the original order.
Code:
cpp#include <iostream> #include <map> #include <string> #include <vector> using namespace std; int main() { map<string, bool> seen; vector<string> order; string word; while (cin >> word) { if (!seen.count(word)) { seen[word] = true; order.push_back(word); } } for (int i = 0; i < order.size(); i++) { if (i > 0) cout << ", "; cout << order[i]; } cout << endl; return 0; }
Solution 12: Kiểm Tra Chuỗi Đối Xứng
Giải thích: Chuẩn hóa trước (bỏ khoảng trắng, về chữ thường) rồi mới so sánh; toán tử == so sánh nội dung hai string.
Explanation: Normalize first (drop spaces, lowercase), then compare; == compares the contents of two strings.
Code:
cpp#include <iostream> #include <string> using namespace std; int main() { string line; getline(cin, line); string clean = ""; for (char c : line) { if (c != ' ') { clean += tolower(c); } } string reversed = ""; for (int i = clean.size() - 1; i >= 0; i--) { reversed += clean[i]; } if (clean == reversed) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
Solution 13: Tính Tiền Giỏ Hàng
Giải thích: map chỉ dùng để TRA giá; thứ tự in là thứ tự đọc vào vì ta in ngay trong vòng lặp.
Explanation: The map is only used to LOOK UP prices; the print order is the input order because printing happens inside the loop.
Code:
cpp#include <iostream> #include <map> #include <string> using namespace std; int main() { map<string, int> prices = {{"apple", 5000}, {"bread", 12000}, {"milk", 20000}}; int n; cin >> n; int total = 0; for (int i = 0; i < n; i++) { string item; int qty; cin >> item >> qty; int line = prices[item] * qty; total += line; cout << item << " x" << qty << " = " << line << endl; } cout << "Total: " << total << endl; return 0; }