Parking Charges
Calculate the number of vehicles parked beyond a specific time and those charged above a certain amount based on complex parking fee rules.
Understand the Problem
Problem Statement
Parking Charges Program: In a parking space, N vehicles are parked. The parking charge for each vehicle is calculated based on how long it has been parked. The parking charge per hour may vary depending on the type of vehicle. The parking charge of a vehicle is calculated based on the following conditions.
- If a vehicle is parked for H hours and M minutes and the parking charge for the vehicle is P rupees per hour, then the total parking charge is H * P if M is less than or equal to 30. Else the total parking charge is (H + 1) * P.
- If a vehicle is parked for less than or equal to 30 minutes, then the total parking charge is P.
The parking time (Hours:Minutes) and the parking charge per hour for each vehicle are passed as the input to the program. The program must print the number of vehicles parked for more than X hours and Y minutes. Then the program must print the number of vehicles charged above Z rupees as the output.
Constraints
- 2 <= N <= 100
- 0 <= H, X <= 23
- 0 <= M, Y <= 59
- 1 <= P, Z <= 1000
Examples
4
02:45 10
03:20 25
02:55 15
05:30 35
3 15 502
2Here X = 3, Y = 15 and Z = 50. For the 1st vehicle (02:45, P=10): hours=2, minutes=45, charge=(2+1)*10=30. For the 2nd vehicle (03:20, P=25): hours=3, minutes=20, charge=3*25=75. For the 3rd vehicle (02:55, P=15): hours=2, minutes=55, charge=(2+1)*15=45. For the 4th vehicle (05:30, P=35): hours=5, minutes=30, charge=5*35=175. Vehicles parked > 3 hours 15 minutes: 4th vehicle (5:30) and 2nd vehicle (3:20) = 2 vehicles. Vehicles charged > 50 rupees: 2nd vehicle (75) and 4th vehicle (175) = 2 vehicles.
6
00:30 15
04:45 20
01:00 10
05:30 20
02:31 12
00:45 30
0 59 304
3Here X = 0, Y = 59 and Z = 30. Vehicles parked > 0 hours 59 minutes: All except 1st vehicle (00:30) = 5 vehicles. Vehicles charged > 30 rupees: 2nd vehicle (100), 4th vehicle (100), 6th vehicle (60) = 3 vehicles.
Solution
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
char parkingTime[20];
int parkingChargePerHour;
int totalCharges[100];
int totalMinutes[100];
for (int i = 0; i < n; i++) {
scanf("%s %d", parkingTime, &parkingChargePerHour);
// Parse HH:MM
int hours = (parkingTime[0] - '0') * 10 + (parkingTime[1] - '0');
int minutes = (parkingTime[3] - '0') * 10 + (parkingTime[4] - '0');
totalMinutes[i] = hours * 60 + minutes;
// Calculate charge
int charge;
if (totalMinutes[i] <= 30) {
charge = parkingChargePerHour;
} else {
if (minutes <= 30) {
charge = hours * parkingChargePerHour;
} else {
charge = (hours + 1) * parkingChargePerHour;
}
}
totalCharges[i] = charge;
}
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
int thresholdMinutes = x * 60 + y;
int countMoreThanTime = 0;
int countMoreThanCharge = 0;
for (int i = 0; i < n; i++) {
if (totalMinutes[i] > thresholdMinutes) {
countMoreThanTime++;
}
if (totalCharges[i] > z) {
countMoreThanCharge++;
}
}
printf("%d\n%d\n", countMoreThanTime, countMoreThanCharge);
return 0;
}Step-by-step C Solution:
- Read the number of vehicles N.
- For each vehicle, read the parking time string and parking charge per hour.
- Parse the HH:MM format by extracting characters at positions 0,1,3,4.
- Convert hours and minutes to total minutes for easier comparison.
- Calculate the parking charge based on the conditions:
- If total minutes <= 30: charge = P
- Else if minutes <= 30: charge = hours * P
- Else: charge = (hours + 1) * P
- Store charges and total minutes in arrays.
- Read threshold values X, Y, Z and convert X:Y to minutes.
- Count vehicles parked longer than threshold and charged more than Z.
- Print both counts.