Archive/Code_HackerRank
[HackerRank]Time Conversion
호리동
2018. 1. 17. 00:52
Given a time in -hour AM/PM format, convert it to military (-hour) time.
Note: Midnight is on a -hour clock, and on a -hour clock. Noon is on a -hour clock, and on a -hour clock.
Input Format
A single string containing a time in -hour clock format (i.e.: or ), where and .
Output Format
Convert and print the given time in -hour format, where .
Sample Input
07:05:45PM
Sample Output
19:05:45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <bits/stdc++.h> using namespace std; int main() { int hh, mm, ss ; char t12[2]; scanf("%d:%d:%d%s", &hh, &mm, &ss, t12) ;//입력받기 if (strcmp(t12,"PM")==0 && hh!=12) // PM일 경우 12를 더한다. hh += 12 ; if (strcmp(t12,"AM")==0 && hh==12) // AM일 경우 놔둔다. hh = 0 ; printf("%02d:%02d:%02d", hh, mm, ss) ; // 출력한다. 한자리수라면 앞에 0을 붙여서 출력한다. return 0; } | cs |
Comment
Warm Up 마지막 문제.
사실 이렇게 문자열을 이용하는 문제는 익숙하지가 않다. 저걸 시,분,초로 나눌생각은 했지만 어떻게 해야할지 막막했는데 scanf를 쓰니까 생각보다 쉬운 문제였다. 어차피 데이터와 데이터 사이에는 ":" 라는 문자로 나눠져있고, 그걸 기준으로 각각 값을 따로 받으면 되는거였다.
String문제.. 연습이 필요하다.