관리 메뉴

Mini

프로그래머스 주차요금계산 c++ // 구현, db설정하라 본문

Algorithm/배열

프로그래머스 주차요금계산 c++ // 구현, db설정하라

Mini_96 2023. 12. 8. 15:24

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1. DB설정

vector<A> cars(9999+1); //car[0000] : 차번호0000의 시작시간,끝시간
cnt[10000] //차i가 주차한 총시간
stored[10000] //직전에 입차한 시간
//바킹독

 

2.시행착오

1. 한 자동차가 여러번 출차가능한경우를 빼먹었다.

 

 

3.전체코드

파싱결과

res : 05:34 / 5761 / IN

times : 05 / 34

가 저장된다.

#include <bits/stdc++.h>

using namespace std;

struct A{
    int st=-1;
    int en=-1;
    int total;
};

int _24min=23*60+59;
vector<A> cars(9999+1); //car[0000] : 차번호0000의 시작시간,끝시간
int vis[9999+1]; 
vector<int> ret;


vector<string> split(string input, string deli) {
	long long pos=0;
	vector<string> ret;
	string token = "";
	while ((pos=input.find(deli)) != string::npos) {
		token = input.substr(0, pos);
		ret.push_back(token);
		input.erase(0, pos + deli.size());
	}
	//마지막 192.168.0.1 의 1저장
	ret.push_back(input);

	return ret;
}
int calc(int time, vector<int>& fees){
    if(time<=fees[0]) return fees[1];
    int ret=0;
    ret+=fees[1];
    int temp=time-fees[0];
    if(temp%fees[2]!=0){
        ret+=(temp/fees[2]+1)*fees[3];
    }
    else{
        ret+=(temp/fees[2])*fees[3];
    }
    //ret*=fees[3];
    return ret;
}
vector<int> solution(vector<int> fees, vector<string> records) {
    for(auto& record : records){
        vector<string> res = split(record," ");
        vector<string> times = split(res[0],":");
        vis[stoi(res[1])]=1;
        if(res[2]=="IN"){
            int num=stoi(res[1]);
            int st=stoi(times[0])*60+stoi(times[1]);
            //int en=_24min;
            cars[num].st=st;
        }
        if(res[2]=="OUT"){
            int num=stoi(res[1]);
            int st=cars[num].st;
            int en=stoi(times[0])*60+stoi(times[1]);
            cars[num].total+=en-st;
            cars[num].st=-1; //끝났다는 표시
            cars[num].en=-1;
        }
    }
    
    //입차는있는데 출차가 없는경우
    for(int i=0;i<cars.size();++i){
        if(vis[i]){
            if(cars[i].st!=-1 && cars[i].en==-1){
                cars[i].total+=_24min-cars[i].st;
            }
        }   
    }
    
    for(int i=0;i<cars.size();++i){
        if(vis[i]){
            //cout<<i<<": "<<cars[i].total<<"\n";
            ret.push_back(calc(cars[i].total,fees));
        }   
    }
    
    

    return ret;
}