[프로그래머스] 주차 요금 계산 C++

semi·2022년 6월 28일
0

coding test

목록 보기
16/57

https://programmers.co.kr/learn/courses/30/lessons/92341

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <deque>
#include <cmath>
using namespace std;

int str2time(string str)
{
	int time = 0;
	time = ((str[0] - '0') * 10 + (str[1] - '0')) * 60 + (str[3] - '0') * 10 + (str[4] - '0');
	return time;
}

vector<int> solution(vector<int> fees, vector<string> records)
{
	vector<int> answer;
	int default_time = fees[0];
	int default_fee = fees[1];
	int unit_time = fees[2];
	int unit_fee = fees[3];
	vector<vector<string>> v;
	string tmp_str = "";
	map<string, int> m;
	map<string, int> all_time;
	for (int i = 0; i < records.size(); i++)
	{
		vector<string> tmp;
		tmp_str = "";
		for (int j = 0; j < records[i].size(); j++)
		{
			if (records[i][j] == ' ')
			{
				tmp.push_back(tmp_str);
				tmp_str = "";
			}
			else
			{
				tmp_str += records[i][j];
			}
		}
		tmp.push_back(tmp_str);
		v.push_back(tmp);
	}

	for (int i = 0; i < v.size(); i++)
	{
		if (v[i][2] == "IN")
		{
			m[v[i][1]]= str2time(v[i][0]);
		}
		else
		{
			all_time[v[i][1]] += str2time(v[i][0]) - m[v[i][1]];
			m.erase(v[i][1]);
		}
	}
	for (auto x : m)
	{
		all_time[x.first] += str2time("23:59") - m[x.first];
	}
	for (auto q : all_time)
	{
		if (q.second <= default_time)
		{
			answer.push_back(default_fee);
		}
		else
		{
			answer.push_back(default_fee + ceil((float)(q.second - default_time) / unit_time) * unit_fee);
		}
	}

	return answer;
}

int main(void)
{
	vector<int> fees1 = { 180, 5000, 10, 600 };
	vector<string> records1 = { "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" };
	vector<int> fees2 = { 120, 0, 60, 591 };
	vector<string> records2 = { "16:00 3961 IN","16:00 0202 IN","18:00 3961 OUT","18:00 0202 OUT","23:58 3961 IN" };
	vector<int> fees3 = { 1, 461, 1, 10 };
	vector<string> records3 = { "00:00 1234 IN" };

	vector<int> answer = solution(fees1, records1);

	return 0;
}

0개의 댓글