Given a time in12 -hour AM/PM format, convert it to military (24-hour) time.
Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
Example:
Input: s="07:05:45PM"
Output: 19:05:45
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){string s = "07:05:45PM";int i;int h1 = (int)s[1] - '0';int h2 = (int)s[0] - '0';int hh = (h2 * 10 + h1);if (s[8] == 'A'){if (hh == 12){cout << "00";for (int i = 2; i <= 7; i++)cout << s[i];}elsefor (i = 0; i <= 7; i++)cout << s[i];}if (s[8] == 'P'){if (hh == 12){cout << "12";for (int i = 2; i <= 7; i++)cout << s[i];}else{hh = hh + 12;cout << hh;for (int i = 2; i <= 7; i++)cout << s[i];}}return 0;}
No comments:
Post a Comment