반응형
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;
}
반응형
'Devlog > Coding Practice' 카테고리의 다른 글
[C++][Implementation] Grading Students (0) | 2021.07.19 |
---|---|
[C++] 1193 분수찾기 (0) | 2021.03.09 |
[C++] 10996 별 찍기 - 21 (0) | 2020.08.10 |
[C++] 2446 별 찍기 - 9 (0) | 2020.08.10 |
[C++] 10809 알파벳 찾기 (0) | 2020.08.10 |