[프로그래머스][C++]추석 트래픽

C++/알고리즘 문제|2019. 9. 14. 18:55

문제:https://programmers.co.kr/learn/courses/30/lessons/17676

#include <string>
#include <vector>
#include <iostream>
using namespace std;

struct mytime {
    int hour;
    int minute;
    double sec;
};

double convertDouble(const mytime& t) {
    double dt=t.sec;
    dt+=t.minute*60;
    dt+=t.hour*60*60;
    return dt;
}
mytime convertTime(const double& d) {
    mytime t{};
    t.sec=d;
    while (t.sec>=60) {
        t.sec-=60;
        t.minute++;
        if (t.minute>=60) {
            t.hour++;
            t.minute-=60;
        }
    }
    return t;
}

mytime getTime(const string& origin) {
    mytime t{};
    string text="";
    for (int i=0;i<origin.size();i++) {
        char c=origin[i];
        if (c==' ') break;
        if (('0'<=c && c<='9') || c=='.') {
            text+=c;
        }
    }
    t.hour=stoi(text.substr(0,2));
    t.minute=stoi(text.substr(2,2));
    t.sec=stod(text.substr(4));
    return t;
}


double getRes(const string& origin) {
    string t="";
    for (auto it=origin.begin()+13; it!=origin.end();it++) {
        char c=*it;
        if (c=='s') continue;
        t+=c;
    }
    return stod(t);
}


int solution(vector<string> lines) {
    //times 세팅완료
    vector<vector<double>> times;
    for (int i=0;i<lines.size();i++) {
        lines[i].erase(lines[i].begin(),lines[i].begin()+11);//2016-09-15 제거
        double endTime=convertDouble(getTime(lines[i]));
        double startTime=endTime-getRes(lines[i])+0.001f;
        vector<double> v(2);
        v[0]=startTime;
        v[1]=endTime;
        times.push_back(v);
    }
    
    
    
    int max=1;
    for (int i=0;i<times.size();i++) {
        int sum = 1;
        double backTime=times[i][1]+1;
        for (int j=i+1;j<times.size();j++) {
            double frontTime=times[j][0];
            if (frontTime<backTime) {
                sum++;
            }
        }
        if (sum>max) {
            max=sum;
        }
    }

    return max;
}

'C++ > 알고리즘 문제' 카테고리의 다른 글

[프로그래머스][C++]스킬트리  (0) 2019.09.14
[프로그래머스][C++] 기능개발  (0) 2019.09.14

댓글()