PS/BOJ
백준 1408번: 24 (C++)
도비(Doby)
2023. 5. 29. 11:41
https://www.acmicpc.net/problem/1408
1408번: 24
도현이는 Counter Terror Unit (CTU)에서 일하는 특수요원이다. 도현이는 모든 사건을 정확하게 24시간이 되는 순간 해결하는 것으로 유명하다. 도현이는 1시간 만에 범인을 잡을 수 있어도 잡지 않는
www.acmicpc.net
Level: Bronze II
Solved By: String
string을 입력으로 받았기에 계산을 수월하게 하기 위해 string을 int로 바꾸는 작업을 했습니다.
그 뒤 출력을 string으로 해주고 싶었기 때문에 std::string에 있는 to_string을 사용하였습니다.
문제를 풀 때 생각해야 할 예외라면 임무를 시작하는 시간이 다음 날로 넘어가서 시간상으로는 현재시간이 임무를 시작하는 시간을 앞서있는 경우 또한 생각할 수 있어야 합니다.
#include <iostream>
#include <string>
using namespace std;
int gettime(string t){
int h = (t[0]-'0') * 10 + t[1]-'0';
int m = (t[3]-'0') * 10 + t[4]-'0';
int s = (t[6]-'0') * 10 + t[7]-'0';
return h*3600+m*60+s;
}
string inversetime(int t){
string ret, temp;
temp = to_string(t%60); if(temp.length()==1) temp='0'+temp;
ret = ':'+temp;
t /= 60;
temp = to_string(t%60); if(temp.length()==1) temp='0'+temp;
ret = ':'+temp+ret;
t /= 60;
temp = to_string(t%60); if(temp.length()==1) temp='0'+temp;
ret = temp+ret;
return ret;
}
int main(){
string st, en; cin >> st >> en;
int st_ = gettime(st);
int en_ = gettime(en);
int v = en_ - st_;
if(en_ < st_) v = 3600*24 - (st_-en_);
auto res = inversetime(v);
cout << res;
return 0;
}