알고리즘/BOJ

백준 5074번 When Do We Finish?

꾸준함. 2021. 5. 18. 02:02

문제 링크입니다: https://www.acmicpc.net/problem/5074

 

5074번: When Do We Finish?

For each line of input, produce one line of output containing a single time, also in the format hh:mm, being the end time of the event as a legal 24 hour time. If this time is not on the same day as the start time, the time will be in the format hh:mm +n,

www.acmicpc.net

간단한 연산 문제였습니다.

 

#include <iostream>
#include <string>
using namespace std;
const int MAX_MINUTE = 60;
const int MAX_HOUR = 24;
typedef struct
{
int h, m;
} Time;
Time getTime(string s)
{
return { stoi(s.substr(0, 2)), stoi(s.substr(3, 2)) };
}
void printResult(Time startTime, Time durationTime)
{
int minute = startTime.m + durationTime.m;
int hour = startTime.h + durationTime.h;
if (minute >= MAX_MINUTE)
{
minute -= MAX_MINUTE;
hour++;
}
int days = 0;
while (hour >= MAX_HOUR)
{
hour -= MAX_HOUR;
days++;
}
if (hour < 10)
{
cout << 0;
}
cout << hour << ":";
if (minute < 10)
{
cout << 0;
}
cout << minute;
if (days)
{
cout << " +" << days;
}
cout << "\n";
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
while (1)
{
string start, duration;
cin >> start >> duration;
if (start == "00:00" && duration == "00:00")
{
break;
}
Time startTime = getTime(start);
Time durationTime = getTime(duration);
printResult(startTime, durationTime);
}
return 0;
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경:Visual Studio 2017

 

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

백준 5101번 Sequences  (0) 2021.05.19
백준 5086번 배수와 약수  (0) 2021.05.19
백준 5073번 삼각형과 세 변  (0) 2021.05.18
백준 5063번 TGN  (0) 2021.05.17
백준 5043번 정말 좋은 압축  (2) 2021.05.17