#include <iostream>
#include <vector>
class Person {
public:
Person(int id) : id(id) {}
int getId() const {
return id;
}
private:
int id;
};
class Building {
public:
Building(int levels) : levels(levels) {
constructBuilding();
}
void constructBuilding() {
int totalPeople = (1 << levels) - 1; // Calculate the total number of people in the building
people.resize(totalPeople);
// Create people with their respective IDs
for (int i = 0; i < totalPeople; ++i) {
people[i] = Person(i + 1);
}
}
void exitOrder() {
for (int i = levels - 1; i >= 0; --i) {
int levelOffset = (1 << i) - 1; // Offset for each level
int levelSize = (1 << i); // Number of people on each level
for (int j = 0; j < levelSize; ++j) {
int personIndex = levelOffset + j;
std::cout << people[personIndex].getId() << " ";
}
}
std::cout << std::endl;
}
void entryOrder() {
for (int i = 0; i < levels; ++i) {
int levelOffset = (1 << i) - 1; // Offset for each level
int levelSize = (1 << i); // Number of people on each level
for (int j = 0; j < levelSize; ++j) {
int personIndex = levelOffset + j;
std::cout << people[personIndex].getId() << " ";
}
}
std::cout << std::endl;
}
private:
int levels;
std::vector<Person> people;
};
int main() {
int levels;
std::cout << "Enter the number of levels in the building: ";
std::cin >> levels;
Building building(levels);
std::cout << "Exit Order: ";
building.exitOrder();
std::cout << "Entry Order: ";
building.entryOrder();
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter