Training/HackerRank

[C++][Warmup] Time Conversion

FATKITTY 2020. 9. 6. 15:26
반응형

www.hackerrank.com/challenges/time-conversion/problem

 

#include <bits/stdc++.h>

using namespace std;

/*
 * Complete the timeConversion function below.
 */
string timeConversion(string s) {
    int hour = stoi(s.substr(0, 2));
    string result;

    // AM: remove 'AM' and only consider 12 AM for conversion
    if (s[8] == 'A') {
        result.append(s, 0, 8);
        if (hour == 12) result.replace(0, 2, "00");
    }
    // PM: remove 'PM' and add 12 to hour except 12 PM
    else if (s[8] == 'P') {
        result.append(s, 0, 8);
        if (hour != 12) {
            hour += 12;
            result.replace(0, 2, to_string(hour));
        }
    }

    return result;
}

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

    string s;
    getline(cin, s);

    string result = timeConversion(s);

    fout << result << "\n";

    fout.close();

    return 0;
}
반응형

'Training > HackerRank' 카테고리의 다른 글

[C++][Implementation] Number Line Jumps  (0) 2021.07.30
[C++][Implementation] Apple and Orange  (0) 2021.07.28
[C++][Implementation] Grading Students  (0) 2021.07.19
[C++][Warmup] Mini-Max Sum  (0) 2020.08.03
[C++][Warmup] Diagonal Difference  (0) 2020.08.03