Loop-and-count problems (J2 pattern)
- Stage
- 3
- Module
- M3.2
- Lessons
- 1
In this module
- Read multiple items in a loop and accumulate a count or total.
- Initialize a counter before the loop and update it inside.
- Use the final count to make a decision or produce output.
Before this module
Lessons
Practice
Try these on the judges. Each link opens the problem on WMOJ or DMOJ.
- 2022 J2Fergusonball Ratings (opens on WMOJ in a new tab) WMOJ
Process two lines of data per player and flag those meeting a condition.
- 2020 J3Art (opens on DMOJ in a new tab) DMOJ
Scan a comma-separated line of numbers, tracking the minimum and maximum as you go.
Why DMOJ: A running-minimum-and-maximum scan, the same one-pass shape as this lesson's counters.
- 2019 J2Time to Decompress (opens on DMOJ in a new tab) DMOJ
Expand a run-length compressed string and process the result in a loop.
Why DMOJ: A one-pass scan with a running total, in the same style as this lesson's examples.