In a certain city, there is a special hotel called the Numeric Hotel. This hotel consists of three buildings, but the room numbers are not arranged in the usual sequential order. Instead, they follow the following special rules:
- If the room number is divisible by 3 and also divisible by 2, the room is located in Building 1.
- If the last digit of the room number is odd but not divisible by 3, the room is located in Building 2.
- If the room number is a perfect square or an even number not divisible by 7, the room is located in Building 3.
- If a room satisfies multiple conditions simultaneously, it is located in the building with the smallest number.
Now, please help the N newly arrived guests find out which building their rooms are in.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input consists of two lines. The first line contains a number N (1 ≤ N ≤ 100), indicating the number of guests arrived. The second line contains N numbers k_i (1 ≤ k_i ≤ 10^7), representing the room numbers of each guest, separated by spaces. | Output N numbers, indicating the building number each guest will stay in. If the room number does not belong to any building, output 0. |
4 12 16 31 9 | 1 3 2 3 |
5 23 1 600 909 44 | 2 2 1 0 3 |
Thought Process
Use if and else if statements to check conditions. This way, if a room number satisfies multiple conditions, the program will stop at the first matching condition.
Sample Code-ZeroJudge N361: Numeric Hotel
#include <iostream>
#include <math.h>
#include <map>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
map<int, int>MAP;
for (int i = 0; i<N; i++)
{
int tmp;
cin >> tmp;
if (MAP[tmp] == 0)
{
if (tmp % 3 == 0 && tmp % 2 == 0) MAP[tmp] = 1;
else if (tmp % 2 != 0 && tmp % 3 != 0) MAP[tmp] = 2;
else if (pow(int(sqrt(tmp)), 2) == tmp || (tmp % 7 != 0 && tmp % 2 == 0)) MAP[tmp] = 3;
else MAP[tmp] = -1;
}
if (MAP[tmp] == -1) cout << "0 ";
else cout << MAP[tmp] << " ";
}
}
//ZeroJudge N361
//Dr. SeanXD