새소식

인기 검색어

알고리즘/알고리즘 문제풀이

[HackerRank] Time Conversion

  • -

https://www.hackerrank.com/challenges/one-week-preparation-kit-time-conversion

 

Time Conversion | HackerRank

Convert time from an AM/PM format to a 24 hour format.

www.hackerrank.com

#include <bits/stdc++.h>

using namespace std;

/*
 * Complete the 'timeConversion' function below.
 *
 * The function is expected to return a STRING.
 * The function accepts STRING s as parameter.
 */

string timeConversion(string s) {
    string mil;
    
    int time = (s.at(0) - '0') * 10 + (s.at(1) - '0');
    
    if (s.find('P') != string::npos)
    {
        if (time >= 1 && time <= 11)
        {
            time += 12;
        }
    }
    else if (s.find('A') != string::npos)
    {
        if (time == 12)
        {
            time = 0;
        }
    }
    else
    {
        cout << "Wrong format" << endl;
    }
    
    string timeS = to_string(time);
    
    if (time < 10)
    {
        timeS = '0' + timeS;
    }
    mil = timeS + ":" + s.at(3)+s.at(4) + ":" + s.at(6)+s.at(7);
    
    return mil;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string s;
    getline(cin, s);

    string result = timeConversion(s);

    fout << result << "\n";

    fout.close();

    return 0;
}

'알고리즘 > 알고리즘 문제풀이' 카테고리의 다른 글

[HackerRank] Find Median  (0) 2023.10.19
[HackerRank] FizzBuzz  (0) 2023.10.19
[HackerRank] Mini-Max-Sum  (0) 2023.10.19
[HackerRank] Plus Minus  (0) 2023.10.19
[HackerRank] arrays-ds  (0) 2023.10.19
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.