CP4CPPLesson 3 - First Judge-Style Arithmetic Task | CP4CPP⇩

Lesson 3 - First Judge-Style Arithmetic Task

  • Lesson 2. You can read integers, store them in named variables, compute, and print.
  • A working C++ setup, as described in Lesson 1.
  • 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.

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.

Task:

  • Input: two integers, the length and the width of a rectangle
  • Output: one integer, the perimeter of that rectangle

Constraints:

1 <= length <= 1000
1 <= width <= 1000

The 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 5
Output:
16

The 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) gives 16 for the sample, while 2 * length + width gives 11.
  • 2 * length + 2 * width is another correct form. Both correct forms give the same result for every allowed input.
  • The variable names come straight from the task. Naming them length and width keeps the formula readable.

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 <= 1000

Start from the worked example. These are all of the source changes required:

  1. Replace int length = 0; with int side = 0;, because the task names one value rather than two.
  2. Delete the line int width = 0;, because a square has a single side length.
  3. Change std::cin >> length >> width; to std::cin >> side;, so the program reads one integer.
  4. Replace int perimeter = 2 * (length + width); with int perimeter = 4 * side;, because a square has four equal sides.

Leave #include <iostream>, int main(), the printing line, and return 0; unchanged.

Input:
6
Expected output:
24

Check these before finishing:

  • The output is 24 and nothing else
  • Input 1 prints 4, which is the smallest allowed case
  • Input 1000 prints 4000, which is the largest allowed case

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.