Lesson 3 - First Judge-Style Arithmetic Task
Prerequisites
Section titled “Prerequisites”- Lesson 2. You can read integers, store them in named variables, compute, and print.
- A working C++ setup, as described in Lesson 1.
What You Will Learn
Section titled “What You Will Learn”- How to read a task whose input and output format is fixed in advance.
- How to check that a stated value range fits the type you choose.
- How to solve an arithmetic task using only input, variables, computation, and output.
Tasks With A Fixed Format
Section titled “Tasks With A Fixed Format”A judge-style task is written so that exactly one output is correct for each input. Rather than describing the result in prose, the task fixes three things:
- the input format, which states what the program is given and in what order
- the output format, which states exactly what to print
- the constraints, which give the range every input value stays inside
Nothing else is added. This lesson needs no conditions and no loops, so it uses none.
The constraints matter for one practical reason. They tell you whether the type you picked can hold the result.
Worked Example
Section titled “Worked Example”Task:
- Input: two integers, the length and the width of a rectangle
- Output: one integer, the perimeter of that rectangle
Constraints:
1 <= length <= 10001 <= width <= 1000The perimeter of a rectangle is 2 * (length + width).
Check the type before writing code. The largest result this task can produce is 2 * (1000 + 1000), which is 4000. That value fits safely in int. Making this check a habit pays off later, because a task that allows much larger values can need a wider type.
#include <iostream>
int main() { int length = 0; int width = 0;
std::cin >> length >> width;
int perimeter = 2 * (length + width);
std::cout << perimeter << "\n"; return 0;}Input:3 5Output:16The sample works out as 2 * (3 + 5), which is 2 * 8, which is 16.
Notes on the code:
- The parentheses are required.
2 * (length + width)gives16for the sample, while2 * length + widthgives11. 2 * length + 2 * widthis another correct form. Both correct forms give the same result for every allowed input.- The variable names come straight from the task. Naming them
lengthandwidthkeeps the formula readable.
Exercise
Section titled “Exercise”Read the side length of a square and print its perimeter.
- Input: one integer, the side length
- Output: one integer, the perimeter
Constraints:
1 <= side <= 1000Start from the worked example. These are all of the source changes required:
- Replace
int length = 0;withint side = 0;, because the task names one value rather than two. - Delete the line
int width = 0;, because a square has a single side length. - Change
std::cin >> length >> width;tostd::cin >> side;, so the program reads one integer. - Replace
int perimeter = 2 * (length + width);withint perimeter = 4 * side;, because a square has four equal sides.
Leave #include <iostream>, int main(), the printing line, and return 0; unchanged.
Exercise Check
Section titled “Exercise Check”Input:6Expected output:24Check these before finishing:
- The output is
24and nothing else - Input
1prints4, which is the smallest allowed case - Input
1000prints4000, which is the largest allowed case
Next Steps
Section titled “Next Steps”This is the last lesson in the current beginner path. You can now read a fixed input format, compute a result with named variables, and print it in a fixed output format.
To practice, rewrite all three exercises from an empty file without looking at the worked examples, and confirm that each one still produces its exact expected output.