Doby's Lab

백준 1408번: 24 (C++) 본문

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;
}

 

728x90

'PS > BOJ' 카테고리의 다른 글

백준 1124번: 언더프라임 (C++)  (0) 2023.06.16
백준 1337번: 올바른 배열 (C++)  (0) 2023.05.29
백준 1297번: TV 크기 (C++)  (0) 2023.05.29
백준 1051번: 숫자 정사각형 (C++)  (0) 2023.03.01
백준 1755번: 숫자놀이 (C++)  (0) 2023.03.01