24

https://www.acmicpc.net/problem/1408

#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
int main() {
int ch, cm, cs, sh, sm, ss, rh, rm, rs;
scanf("%d:%d:%d", &ch, &cm, &cs);
scanf("%d:%d:%d", &sh, &sm, &ss);
sh += 24;
if (cs > ss) {
sm--;
ss += 60;
}
rs = ss - cs;
if (cm > sm) {
sh--;
sm += 60;
}
rm = sm - cm;
rh = sh - ch;
if (rh >= 24) rh -= 24;
cout << setfill('0') << setw(2) << rh << ':';
cout << setfill('0') << setw(2) << rm << ':';
cout << setfill('0') << setw(2) << rs;
return 0;
}
view raw b01408.cpp hosted with ❤ by GitHub
using System;
class Program {
public static void Main() {
string[] s = Console.ReadLine().Split(':');
int ch = int.Parse(s[0]), cm = int.Parse(s[1]), cs = int.Parse(s[2]);
s = Console.ReadLine().Split(':');
int sh = int.Parse(s[0]) + 24, sm = int.Parse(s[1]), ss = int.Parse(s[2]), rh, rm, rs;
if (cs > ss) {
sm--;
ss += 60;
}
rs = ss - cs;
if (cm > sm) {
sh--;
sm += 60;
}
rm = sm - cm;
rh = sh - ch;
if (rh >= 24) rh -= 24;
Console.Write("{0:D2}:{1:D2}:{2:D2}", rh, rm, rs);
}
}
view raw b01408.cs hosted with ❤ by GitHub
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] s = sc.nextLine().split(":");
int ch = Integer.parseInt(s[0]), cm = Integer.parseInt(s[1]), cs = Integer.parseInt(s[2]);
s = sc.nextLine().split(":");
int sh = Integer.parseInt(s[0]) + 24, sm = Integer.parseInt(s[1]), ss = Integer.parseInt(s[2]), rh, rm, rs;
if (cs > ss) {
sm--;
ss += 60;
}
rs = ss - cs;
if (cm > sm) {
sh--;
sm += 60;
}
rm = sm - cm;
rh = sh - ch;
if (rh >= 24) rh -= 24;
System.out.format("%02d:%02d:%02d", rh, rm, rs);
}
}
view raw b01408.java hosted with ❤ by GitHub
ch, cm, cs = map(int, input().split(':'))
sh, sm, ss = map(int, input().split(':'))
rh = rm = rs = 0
sh += 24
if cs > ss:
sm -= 1
ss += 60
rs = ss - cs
if cm > sm:
sh -= 1
sm += 60
rm = sm - cm
rh = sh - ch
if rh >= 24: rh -= 24
print("%02d:%02d:%02d" % (rh, rm, rs))
view raw b01408.py hosted with ❤ by GitHub
import java.util.Scanner
import java.lang.String.format
fun main() {
val sc = Scanner(System.`in`)
var s = sc.nextLine().split(':')
val ch = Integer.parseInt(s[0])
val cm = Integer.parseInt(s[1])
val cs = Integer.parseInt(s[2])
s = sc.nextLine().split(':')
var sh = Integer.parseInt(s[0]) + 24
var sm = Integer.parseInt(s[1])
var ss = Integer.parseInt(s[2])
var rh: Int
var rm: Int
var rs: Int
if (cs > ss) {
sm--
ss += 60
}
rs = ss - cs
if (cm > sm) {
sh--
sm += 60
}
rm = sm - cm
rh = sh - ch
if (rh >= 24) rh -= 24
print(format("%02d:%02d:%02d", rh, rm, rs))
}
view raw b01408.kt hosted with ❤ by GitHub
const fs = require('fs')
let input = fs.readFileSync(0).toString().split('\n');
let s = input[0].split(':')
let ch = parseInt(s[0]), cm = parseInt(s[1]), cs = parseInt(s[2])
s = input[1].split(':')
let sh = parseInt(s[0]) + 24, sm = parseInt(s[1]), ss = parseInt(s[2])
let rh, rm, rs
if (cs > ss) {
sm--
ss += 60
}
rs = ss - cs
if (cm > sm) {
sh--
sm += 60
}
rm = sm - cm
rh = sh - ch
if (rh >= 24) rh -= 24
console.log(`${String(rh).padStart(2, '0')}:${String(rm).padStart(2, '0')}:${String(rs).padStart(2, '0')}`)
view raw b01408.js hosted with ❤ by GitHub
import 'dart:io';
void main() {
List<String> s = stdin.readLineSync()!.split(':');
int ch = int.parse(s[0]), cm = int.parse(s[1]), cs = int.parse(s[2]);
s = stdin.readLineSync()!.split(':');
int sh = int.parse(s[0]) + 24, sm = int.parse(s[1]), ss = int.parse(s[2]), rh, rm, rs;
if (cs > ss) {
sm--;
ss += 60;
}
rs = ss - cs;
if (cm > sm) {
sh--;
sm += 60;
}
rm = sm - cm;
rh = sh - ch;
if (rh >= 24) rh -= 24;
print("${rh.toString().padLeft(2, '0')}:${rm.toString().padLeft(2, '0')}:${rs.toString().padLeft(2, '0')}");
}
view raw b01408.dart hosted with ❤ by GitHub

풀이

첫째로 진수에 대한 생각을 곰곰히 해보면 쉽게 해결할 수 있는 문제이다.
시간은 24시간으로 하루가 지나고 분이나 초는 60을 기준으로 단위가 올라간다.
따라서 60을 기준으로 자리수를 계산한다.

둘째로 임무 시작 시간에서 지금 시간을 뺄 때
지금 시간이 다음날로 되기 전일 경우 마이너스가 값으로 나올 경우를 대비하여
먼저 24시를 더한 후 뺀 결과값이 24이상일 경우 다시 24를 빼주면 쉽게 결과값을 얻을 수 있다.