CCCGuide
Warm-ups (J1–J2)

Practice Problems

very-frequent

This module works through three J1 and J2 problems from 2025 and 2024. Each one comes down to the same approach as the previous module: read carefully, work out what is being asked, and translate it directly into code.

J1 Walkthrough: Roller Coaster Ride2025 · J1 · Original problem · DMOJ

You are spending the day at the CEMC’s funfair. One of the rides at the funfair is a roller coaster which has one train with a number of cars. Each car holds the same number of people.

When you arrive at the roller coaster, you see that there is a line. Your job is to determine whether or not you will be on the next train ride, assuming that every car is fully occupied for every ride.

The first line of input contains a positive integer, NN, representing your place in line. For example, if NN is 55 then you are the fifth person in line.

The second line contains a positive integer, CC, representing the number of cars the train has.

The third line contains a positive integer, PP, representing the number of people a single car holds.

Output either yes or no, indicating whether or not you will be on the next train ride.

14
3
2

Output for Sample Input 1

no

Explanation of Output for Sample Input 1

The train has 33 cars and each car holds 22 people. Therefore, 66 people can ride the next train. Since you are the 14th person in line, you will not be on the next train ride.

12 
4 
3

Output for Sample Input 2

yes

Explanation of Output for Sample Input 2

The train has 44 cars and each car holds 33 people. Therefore, 1212 people can ride the next train. Since you are the 12th person in line, you will be on the next train ride.

Solution

We need to decide whether the person gets on the ride. The train holds C×PC \times P people, and the position in line is NN, so the rider boards when:

N≤C×P N \leq C \times P

Sample 2 checks the boundary: the train holds 4×3=124 \times 3 = 12 people and the person is 12th in line, so being exactly at the limit still counts.

Implementation

N = int(input())
C = int(input())
P = int(input())

if N <= C * P:
    print("yes")
else:
    print("no")

J2 Walkthrough: Donut Shop2025 · J2 · Original problem · DMOJ

The owner of a donut shop spends the day baking and selling donuts.

Given the events that happen over the course of the day, your job is to determine the number of donuts remaining when the shop closes.

The first line of input contains a non-negative integer, DD, representing the number of donuts available when the shop first opens.

The second line contains a positive integer, EE, representing the number of events that happen over the course of the day. The next EE pairs of input lines describe these events.

The first line in the pair contains either the + (plus) symbol, indicating that donuts have been baked, or the - (minus) symbol, indicating that donuts have been sold. The second line in the pair contains a positive integer, QQ, representing the quantity of donuts associated with the event.

For each sale of donuts, the value of QQ will be less than or equal to the number of donuts available at that time.

Output the non-negative integer, RR, which is the number of donuts remaining when the shop closes.

10
3
+
24
-
6
-

Output for Sample Input

16

Explanation of Output for Sample Input

The shop opened with 1010 donuts and there were 33 events during the day. The owner first baked 2424 donuts. Then the owner sold 66 donuts, followed by another 1212. The number of donuts remaining is 10+24−6−12=1610+24-6-12=16.

Solution

We keep a running total. Start with the DD donuts the shop opens with, then process the EE events one at a time: a + adds QQ donuts, a - removes them. When the events run out, the total is the answer.

The input specification guarantees that every sale has enough donuts in stock, so the total never goes negative. There is nothing to check and nothing to clamp.

Implementation

D = int(input())
E = int(input())

for _ in range(E):
    event = input()
    Q = int(input())

    if event == "+":
        D += Q
    else:
        D -= Q

print(D)

J2 Walkthrough: Dusa and the Yobis2024 · J2 · Original problem · DMOJ

Dusa eats Yobis, but only Yobis of a certain size.

If Dusa encounters a Yobi that is smaller than itself, it eats the Yobi, and absorbs its size. For example, if Dusa is of size 1010 and it encounters a Yobi of size 66, Dusa eats the Yobi and expands to size 10+6=1610+6=16.

If Dusa encounters a Yobi that is the same size as itself or larger, Dusa runs away without eating the Yobi.

Dusa is currently facing a line of Yobis and will encounter them in order. Dusa is guaranteed to eventually encounter a Yobi that causes it to run away. Your job is to determine Dusa’s size when this happens.

    

The first line of input contains a positive integer, DD, representing Dusa’s starting size.

The remaining lines of input contain positive integers representing the sizes of the Yobis in order.

Output the positive integer, RR, which is Dusa’s size when it eventually runs away.

5
3
2
9 
20
22
14

Output for Sample Input 1

19

Explanation of Output for Sample Input 1

Dusa is large enough to eat the Yobi of size 33. This brings Dusa’s size to 88. Dusa is large enough to eat the Yobi of size 22. This brings Dusa’s size to 1010. Dusa is large enough to eat the Yobi of size 99. This brings Dusa’s size to 1919. The Yobi of size 2020 causes Dusa to run away.

10
10
3
5

Output for Sample Input 2

10

Explanation of Output for Sample Input 2

The Yobi of size 1010 causes Dusa to run away, leaving its size unchanged.

Solution

This is the first problem where the input has no count — we are not told how many Yobis there are. We read sizes one at a time and react to each: if the Yobi is smaller than Dusa, Dusa eats it and grows; if it is the same size or bigger, Dusa runs away and the process ends.

The problem guarantees that a Yobi big enough to stop Dusa will eventually appear, so a loop that reads until it breaks is safe. That guarantee is also why there is no end-of-input handling to write.

Implementation

D = int(input())

while True:
    size = int(input())

    if size >= D:
        break

    D += size

print(D)

Takeaways

None of these problems needs anything beyond input, a condition, and a loop. What they reward is careful reading: being exactly at capacity still gets the rider on the ride, the donut spec promises sales never exceed stock, and Dusa guarantees the loop terminates.

The next module covers problems where the input is text instead of numbers.

On this page

Contributors