-
[프로그래머스] 주차 요금 계산 c++Coding Test/programmers 2022. 9. 27. 21:17728x90
문제 설명
주차장의 요금표와 차량이 들어오고(입차) 나간(출차) 기록이 주어졌을 때, 차량별로 주차 요금을 계산하려고 합니다. 아래는 하나의 예시를 나타냅니다.
- 요금표
기본 시간(분) 기본 요금(원) 단위 시간(분) 단위 요금(원) 180 5000 10 600 - 입/출차 기록
시각(시:분) 차량 번호 내역 05:34 5961 입차 06:00 0000 입차 06:34 0000 출차 07:59 5961 출차 07:59 0148 입차 18:59 0000 입차 19:09 0148 출차 22:59 5961 입차 23:00 5961 출차 - 자동차별 주차 요금
차량 번호 누적 주차 시간(분) 주차 요금(원) 0000 34 + 300 = 334 5000 + ⌈(334 - 180) / 10⌉ x 600 = 14600 0148 670 5000 +⌈(670 - 180) / 10⌉x 600 = 34400 5961 145 + 1 = 146 5000 제한 사항
- fees의 길이 = 4
- fees[0] = 기본 시간(분)
- 1 ≤ fees[0] ≤ 1,439
- fees[1] = 기본 요금(원)
- 0 ≤ fees[1] ≤ 100,000
- fees[2] = 단위 시간(분)
- 1 ≤ fees[2] ≤ 1,439
- fees[3] = 단위 요금(원)
- 1 ≤ fees[3] ≤ 10,000
- 1 ≤ records의 길이 ≤ 1,000
- records의 각 원소는 "시각 차량번호 내역" 형식의 문자열입니다.
- 시각, 차량번호, 내역은 하나의 공백으로 구분되어 있습니다.
- 시각은 차량이 입차되거나 출차된 시각을 나타내며, HH:MM 형식의 길이 5인 문자열입니다.
- HH:MM은 00:00부터 23:59까지 주어집니다.
- 잘못된 시각("25:22", "09:65" 등)은 입력으로 주어지지 않습니다.
- 차량번호는 자동차를 구분하기 위한, `0'~'9'로 구성된 길이 4인 문자열입니다.
- 내역은 길이 2 또는 3인 문자열로, IN 또는 OUT입니다. IN은 입차를, OUT은 출차를 의미합니다.
- records의 원소들은 시각을 기준으로 오름차순으로 정렬되어 주어집니다.
- records는 하루 동안의 입/출차된 기록만 담고 있으며, 입차된 차량이 다음날 출차되는 경우는 입력으로 주어지지 않습니다.
- 같은 시각에, 같은 차량번호의 내역이 2번 이상 나타내지 않습니다.
- 마지막 시각(23:59)에 입차되는 경우는 입력으로 주어지지 않습니다.
- 아래의 예를 포함하여, 잘못된 입력은 주어지지 않습니다.
- 주차장에 없는 차량이 출차되는 경우
- 주차장에 이미 있는 차량(차량번호가 같은 차량)이 다시 입차되는 경우
입출력 예
fees records result [180, 5000, 10, 600] ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"] [14600, 34400, 5000] [120, 0, 60, 591] ["16:00 3961 IN","16:00 0202 IN","18:00 3961 OUT","18:00 0202 OUT","23:58 3961 IN"] [0, 591] [1, 461, 1, 10] ["00:00 1234 IN"] [14841] 풀이
#include <string> #include <vector> #include <map> #include <cmath> using namespace std; void myparser(string& str, vector<string>& value, string& delimiter){ int pos = 0; string token(""); while( (pos = str.find(delimiter)) != string::npos){ token = str.substr(0, pos); value.push_back(token); str.erase(0, pos + delimiter.length()); } value.push_back(str); } int timeCalcul(string in, string out){ int h1 = stoi(in.substr(0 , 2)); int m1 = stoi(in.substr(3, 2)); int h2 = stoi(out.substr(0, 2)); int m2 = stoi(out.substr(3, 2)); int diff = (h2 - h1) * 60 + (m2 - m1); return diff; } vector<int> solution(vector<int> fees, vector<string> records) { map<string, vector<string>> hash; for(auto &it : records){ vector<string> value; string delimiter(" "); myparser(it, value, delimiter); hash[value[1]].push_back(value[0]); } vector<int> answer; for(auto &it : hash){ if(it.second.size() & 1){ it.second.push_back("23:59"); } int total = 0; for(int i = 0; i < it.second.size() - 1; i += 2){ total += timeCalcul(it.second[i] , it.second[i + 1]); } int price = fees[1]; if(total > fees[0]){ price += ceil( (total - fees[0]) / (double)fees[2]) * fees[3]; } answer.push_back(price); } return answer; }
다른 방법
int conv(string& t) { int h = (t[0] - 48) * 10 + t[1] - 48; int m = (t[3] - 48) * 10 + t[4] - 48; return h * 60 + m; } vector<int> solution(vector<int> fees, vector<string> records) { vector<int> vec[10000]; for (auto& record : records) { stringstream ss(record); string a, b, c; ss >> a >> b >> c; vec[stoi(b)].push_back(conv(a)); } vector<int> ans; for (int i = 0; i < 10000; ++i) if (!vec[i].empty()) { if (vec[i].size() & 1) vec[i].push_back(23 * 60 + 59); int sum = 0; for (int j = 1; j < vec[i].size(); j += 2) sum += vec[i][j] - vec[i][j - 1]; int val = fees[1]; if (sum > fees[0]) val += (sum - fees[0] + fees[2] - 1) / fees[2] * fees[3]; ans.push_back(val); } return ans; }
728x90'Coding Test > programmers' 카테고리의 다른 글
[프로그래머스] 교점에 별 만들기 c++ (2) 2022.09.29 [프로그래머스] 피로도 c++ (0) 2022.09.28 [프로그래머스] 양궁대회 c++ (2) 2022.09.26 [프로그래머스] 체육복 c++ (0) 2022.09.13 [프로그래머스] 두 개 뽑아서 더하기 c++ (0) 2022.09.13