코딩/SWEA

[SWEA] D3. 7732. 시간 개념 C++

mjCodingLife 2024. 9. 1. 18:25

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWrDLM0aRA8DFARG

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

간단한 구현문제.

 

#include <iostream>
#include <string>

using namespace std;

int strToSec(string &s) {
	int hour = stoi(s.substr(0, 2));
	int min = stoi(s.substr(3, 2));
	int sec = stoi(s.substr(6, 2));

	return hour * 60 * 60 + min * 60 + sec;
}

string secToStr(int &n) {
	string result;
	int sec = n % 60;
	result = to_string(sec);
	if (sec < 10) result = "0" + result;
	result = ":" + result;

	n /= 60;
	int min = n % 60;
	result = to_string(min) + result;
	if (min < 10) result = "0" + result;
	result = ":" + result;

	n /= 60;
	int hour = n % 60;
	result = to_string(hour) + result;
	if (hour < 10) result = "0" + result;

	return result;
}

int main() {
	int T;
	cin >> T;
	for (int tc = 1; tc <= T; tc++) {
		string now, next;
		cin >> now >> next;

		int nowSec = strToSec(now);
		int nextSec = strToSec(next);
		
		int timeLeft = nextSec - nowSec;
		if (timeLeft < 0) timeLeft += 24 * 60 * 60;

		cout << "#" << tc << " " << secToStr(timeLeft) << endl;
	}

	return 0;
}