cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2024 · EasyString parsing

Decode Hotel Keys

National Olympiad — Municipal round (2024 season)

Submission name: Hotel.cpp

Problem summary

Four hotel keys each contain two floor digits, two room digits and a letter: A for an apartment or D for a double room. Print each guest’s floor and the number staying in apartments.

Input

Four five-character key labels in guest order.

Output

Four floor numbers without leading zeros, followed by the apartment-guest count.

Constraints

Floors are 08 through 12; rooms are 01 through 99; the last character is A or D.

Worked example

An independently prepared example for this explanation.

Input

0812A
1007A
1289D
1288A

Output

8 10 12 12 3

Solution analysis

  1. Read every label as a string so that a floor such as 08 keeps its two-character position. The room digits do not affect either requested result.
  2. Convert the first two characters using 10·(key[0]−'0')+(key[1]−'0'). Count a guest whenever key[4] is A.
  3. For 0812A, the floor is 8 and the guest contributes one apartment. The sample floors are 8,10,12,12 and three final letters are A.

Why it works

The first two characters encode exactly the floor in decimal, and the last character exactly encodes the room type. Processing all four labels in input order therefore yields all requested values.

Complexity: O(1) time and O(1) extra space because exactly four fixed-length labels are read.

Watch out. Count guests, even if two labels describe the same apartment. Do not include the room number in the floor conversion.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
#include <string>
using namespace std;
int main() {
    int apartments = 0;
    for (int guest = 0; guest < 4; guest++) {
        string key;
        cin >> key;
        // Fixed character positions preserve the floor even with leading zeros.
        int floor = 10 * (key[0] - '0') + key[1] - '0';
        cout << floor << ' ';
        if (key[4] == 'A') {
            apartments++;
        }
    }
    cout << apartments << '\n';
}

Source: Arena Olimpiici ↗