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

 

코딩테스트 연습 - 주차 요금 계산

[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]

programmers.co.kr

 

 

[난이도] level2
[유형] 구현

[풀이]
주어진 조건에 맞게 잘 구현하는 문제입니다.
특별한 알고리즘은 없습니다..

 

#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
int t[10000];
vector<int> fees;
map<int,int> pay;
vector<string> split(string s){
    vector<string> ret;
    int idx;
    s+=' ';
    while((idx = s.find(' '))!=string::npos){
        ret.push_back(s.substr(0,idx));
        printf("%s ",s.substr(0,idx).c_str());
        s = s.substr(idx+1);
    }
    return ret;
}
vector<int> solution(vector<int> _fees, vector<string> records) {
    vector<int> answer;
    fees=_fees;
    memset(t,-1,sizeof(t));
    for(auto r : records){
        auto ret = split(r);
        int h,m,num=stoi(ret[1]);
        sscanf(ret[0].c_str(),"%d:%d",&h,&m);
        m+=h*60;
        if(t[num]==-1) {
            t[num]=m;
        }else{
            pay[num]+=m-t[num];
            t[num]=-1;
        }
    }
    for(int i=0;i<10000;i++){
        if(t[i]!=-1) pay[i]+=23*60+59-t[i];
    }
    for(auto [k,v] : pay){
        int total = fees[1];
        if(v>fees[0]) total+=ceil((double)(v-fees[0])/fees[2])*fees[3];
        answer.push_back(total);
    }
    return answer;
}


https://github.com/has2/Problem-Solving/blob/master/programmers/level2/주차_요금_계산.cpp

+ Recent posts