Hotel Prices In C++ hackerrank solution

Problem statement:

You are given the prices of hotel rooms for the upcoming season in a hotel. Each room has a specific price per night. You need to implement a Hotel class that has a function getPrice which takes the number of nights a guest is staying and returns the total price for their stay.

The Hotel class should have the following attributes and methods:

price: an integer array of size N, where N is the number of rooms. It stores the price of each room.

getPrice(int k): a method that takes an integer k as input and returns the total price for k nights. The total price is calculated by summing the prices of all the rooms for k nights.

      

Sample Input:

The input consists of two lines:

The first line contains an integer N, which represents the number of rooms.
The second line contains N space-separated integers, which represent the prices of the rooms.
3 100 200 150 2
      

Sample Output:

For each test case, print the total price for the given number of nights.
600

Explanation:

In this example, we have three rooms with prices 100, 200, and 150. The guest is staying for 2 nights.
The total price is calculated by summing the prices of all the rooms for 2 nights: 100 + 200 + 150 = 600.

      
  #include <iostream>
  #include <vector>  
  using namespace std;
  
  class Hotel {
  private:
      vector price;
  
  public:
      Hotel(vector p) {
          price = p;
      }
  
      int getPrice(int k) {
          int total = 0;
          for (int i = 0; i < price.size(); i++) {
              total += price[i];
          }
          return total * k;
      }
  };
  
  int main() {
      int N;
      cin >> N;
  
      vector prices(N);
      for (int i = 0; i < N; i++) {
          cin >> prices[i];
      }
  
      int nights;
      cin >> nights;
  
      Hotel hotel(prices);
      int total_price = hotel.getPrice(nights);
  
      cout << total_price << endl;
  
      return 0;
  }
  

Code Explanation


We start by including the necessary header files, including for input/output operations and for working with dynamic arrays.

The Hotel class is defined with a private attribute price of type vector to store the prices of the rooms.

The class has a constructor that takes a vector p as input and assigns it to the price attribute.

The getPrice method takes an integer k as input and calculates the total price for k nights by summing the prices of all the rooms and multiplying it by k.

In the main() function, we read the number of rooms (N) using cin.

We then create a vector prices of size N to store the prices of the rooms and read the prices using a loop.

Next, we read the number of nights (nights) using cin.

We create an instance of the Hotel class with the prices vector.

We call the getPrice method on the hotel object with the nights as input and store the result in total_price.

Finally, we output the total_price using cout.

Example Output:

For the sample input of 3 rooms with prices 100, 200, and 150, and staying for 2 nights, the output will be 600.

I hope this solution helps you understand the problem and provides a clear example of how to solve it using C++. Feel free to ask any further questions!