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
- 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.
- Convert the first two characters using 10·(key[0]−'0')+(key[1]−'0'). Count a guest whenever key[4] is A.
- 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.
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';
}