알고리즘/BOJ

백준 1942번 디지털시계

꾸준함. 2021. 4. 6. 02:41

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

 

1942번: 디지털시계

디지털시계는 일반적으로 시각을 “hh:mm:ss”의 형태로 표현한다. hh는 00 이상 23 이하의 값을, mm과 ss는 00 이상 59 이하의 값을 가질 수 있다. 이러한 형태의 시각에서 콜론(“:”)을 제거하면 “hhm

www.acmicpc.net

쉽지만 문자열 처리가 다소 까다로운 문제였습니다.


#include <iostream>
#include <string>
using namespace std;
const int MAX = 3;
const int MAX_SECOND = 60;
const int MAX_MINUTE = 60;
const int MAX_HOUR = 24;
typedef struct
{
int hour, minute, second;
}Time;
void getTimeStruct(string input, Time &startTime, Time &endTime)
{
startTime.hour = stoi(input.substr(0, 2));
startTime.minute = stoi(input.substr(3, 2));
startTime.second = stoi(input.substr(6, 2));
endTime.hour = stoi(input.substr(9, 2));
endTime.minute = stoi(input.substr(12, 2));
endTime.second = stoi(input.substr(15, 2));
}
bool isTimeEqual(Time startTime, Time endTime)
{
return startTime.hour == endTime.hour
&& startTime.minute == endTime.minute
&& startTime.second == endTime.second;
}
int convertTimeStructToInt(Time time)
{
return time.hour * 10000 + time.minute * 100 + time.second;
}
void incrementTime(Time &time)
{
time.second++;
if (time.second == MAX_SECOND)
{
time.second = 0;
time.minute++;
}
if (time.minute == MAX_MINUTE)
{
time.minute = 0;
time.hour++;
}
if (time.hour == MAX_HOUR)
{
time.hour = 0;
}
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 0; i < MAX; i++)
{
string input;
getline(cin, input);
Time startTime, endTime;
getTimeStruct(input, startTime, endTime);
int result = 0;
while (1)
{
int start = convertTimeStructToInt(startTime);
int end = convertTimeStructToInt(endTime);
if (start % 3 == 0)
{
result++;
}
if (isTimeEqual(startTime, endTime))
{
break;
}
incrementTime(startTime);
}
cout << result << "\n";
}
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경:Visual Studio 2017

 

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

반응형

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

백준 21300번 Bottle Return  (2) 2021.04.08
백준 1952번 달팽이2  (2) 2021.04.07
백준 1864번 문어 숫자  (0) 2021.04.06
백준 1837번 암호제작  (0) 2021.04.04
백준 1703번 생장점  (0) 2021.04.04