[백준 1408] 24 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
현재 시각과 임무 시작 시각이 주어졌을 때,
그 사이에 남은 시간을 시:분:초 형식으로 출력하는 문제입니다.
접근법
- 현재 시각과 시작 시각을 초 단위로 환산합니다.
- 시작 시각이 더 늦다면 단순히 두 값을 뺀 차이만큼의 초가 답입니다.
- 현재 시각이 더 늦다면 자정(24시간 = 86400초)을 기준으로
다음 날 시작 시각까지의 차이를 구합니다:
- 계산된 초 값을 다시
hh:mm:ss
로 변환하여 출력합니다.- 이때 자릿수는 항상
2자리
로 맞춰주어어야 합니다.
- 이때 자릿수는 항상
Code
[ C# ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
namespace Solution {
class Program {
static void Main(string[] args) {
var now = Console.ReadLine()!.Split(':');
var target = Console.ReadLine()!.Split(':');
int nowSec = int.Parse(now[0]) * 3600 + int.Parse(now[1]) * 60 + int.Parse(now[2]);
int targetSec = int.Parse(target[0]) * 3600 + int.Parse(target[1]) * 60 + int.Parse(target[2]);
int diffSec = targetSec >= nowSec ? targetSec - nowSec : 86400 - (nowSec - targetSec);
Console.WriteLine($@"{diffSec / 3600:D2}:{(diffSec % 3600) / 60:D2}:{diffSec % 60:D2}");
}
}
}
[ C++ ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string now, sTime;
cin >> now >> sTime;
int nowSec = stoi(now.substr(0, 2)) * 3600 +
stoi(now.substr(3, 2)) * 60 +
stoi(now.substr(6, 2));
int sTimeSec = stoi(sTime.substr(0, 2)) * 3600 +
stoi(sTime.substr(3, 2)) * 60 +
stoi(sTime.substr(6, 2));
int diffSec = (sTimeSec >= nowSec) ? sTimeSec - nowSec : 86400 - (nowSec - sTimeSec);
cout << setw(2) << setfill('0') << diffSec / 3600 << ":"
<< setw(2) << setfill('0') << (diffSec % 3600) / 60 << ":"
<< setw(2) << setfill('0') << diffSec % 60 << "\n";
}