관리 메뉴

有希

HackerRank/Time Conversion 본문

프로그래밍/알고리즘+코딩테스트

HackerRank/Time Conversion

有希. 2022. 5. 1. 14:02

12시간 체제를 24시간 체제로 바꾸는 문제.

주의해야 할 것은 h가 12일때 뿐이다. 오전 12시는 00시로 바꿔줘야 하고, 오후 12시는 그대로 둬야 한다. 오후 12보다 오후 11시가 더 늦은 시간임에 주의할 것.

PM처럼 AM에서 h-12를 해버리면 시간표기 양식인 00이 아니라 0이 나오므로 "00"으로 밀어준다는 점도 주의해야 한다

string timeConversion(string s) {
    string h = s.substr(0,2);
    string format = s.substr(8,2);
    
    if(format == "PM")
    {
        if(h != "12")
            h = to_string(stoi(h)+12);
    }
    //AM
    else {
        if(h == "12")
        {
            h = "00";
        }
    }
    
    string ret = s.substr(0, s.size()-2);
    ret[0] = h[0];
    ret[1] = h[1];
    
    return ret;
}

'프로그래밍 > 알고리즘+코딩테스트' 카테고리의 다른 글

HackerRank/Number Line Jumps  (0) 2022.05.02
HackerRank/Grading Students  (0) 2022.05.01
LeetCode/ Jump Game  (0) 2022.04.30
LeetCode/Find the Town Judge  (0) 2022.04.30
LeetCode/Spiral Matrix  (0) 2022.04.30