Skip to content
CP4CPP logo
CP4CPP/EDU
App settings
Installation and offline settings

Preparing update checks...

Workspaces

12

Reading record

Reading statistics
Reading progress
-
Words
-
Reading estimate
-
Preset chosen
-
Code blocks
-
Images and diagrams
-
Preferences and widgets

    Shared across workspaces

    Heading markersPercentage markers

    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.

    Reader layout

    Col / Left

    Left column

    LEFT
    Width
    Rail
    ...
    CENTER
    Width
    Rail
    ...
    RIGHT
    Width
    Rail
    ...
    TOP
    NAV
    MID
    BOT

    Theme settings

    Preset 0 · Default · Locked

    Advanced colors · OKLCH

    Choose preset 1-9 to edit. Changes save on this device. Each color inherits in this order: cell → row → column → app → base theme.

    Editing All

    Selected scope

    Chroma 0 is neutral gray. Displayable colors depend on your screen; the browser maps colors outside its gamut. Grayscale also affects images and charts.

    Progress appearance

    Loading controls…

    Forest0.88 kB
    Slate0.88 kB

    Font settings

    Preset 0 · Default · Locked

    Font typography
    Advanced typography

    Auto uses the selected spacing mode. Variants and available weights depend on the font. The browser may synthesize missing weights or styles.

    a b c d e f g h i j k l m n o p q r s t u v w x y z

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    0 1 2 3 4 5 6 7 8 9

    ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

    Iosevka Extended10.76 MB
    Inter297.90 kB
    Source Sans 3197.90 kB
    Lora138.11 kB
    Source Serif 4216.02 kB
    JetBrains Mono130.64 kB
    Fira Code53.67 kB

    Keybinds

    Choose a binding, press up to 4 keys together, then release to save. Modifiers count as keys. Esc clears the selected binding. Unsupported keys show Unknown.

    Reader

    Forbidden key combinations

    Windows / Linux reference. These combinations are reserved by this app to avoid browser, system and editing conflicts. Some are intercepted before a page can receive them. Browser extensions and other operating systems may reserve additional keys.

    Fixed navigation: Ctrl + K opens Search. Alt + F6-F9 selects a row; Alt + F10-F12 selects a column. Combine both to select a cell. While selected, F1 captures a screenshot and F2 downloads PNG. Esc cancels dialogs and navigation when a binding is not being recorded.