기록용 블로그

[boj13223] 소금폭탄 본문

개발/알고리즘 공부

[boj13223] 소금폭탄

andjane 2023. 4. 5. 14:13

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

 

13223번: 소금 폭탄

첫째 줄에는 현재 시각이 hh:mm:ss로 주어진다. 시간의 경우 0≤h≤23 이며, 분과 초는 각각 0≤m≤59, 0≤s≤59 이다. 두 번째 줄에는 소금 투하의 시간이 hh:mm:ss로 주어진다.

www.acmicpc.net

 

1. 계층적으로 표현되는 각 단위를 계산할 때,

가장 작은 단위로 통일하면 더 편할 수 있다.

//모두 초로 변환
int n_date = Integer.parseInt(n_date_array[0]) * 60 * 60
                    + Integer.parseInt(n_date_array[1]) * 60
                       + Integer.parseInt(n_date_array[2]);

int s_date = Integer.parseInt(s_date_array[0]) * 60 * 60
                    + Integer.parseInt(s_date_array[1]) * 60
                        + Integer.parseInt(s_date_array[2]);

int out_date = s_date - n_date;
if(out_date <= 0) out_date += 24 * 3600; //하루가 이미 지남

// 10000초 = 3600 * (2시간) + 2800초
//         = 2시간 + 60 * (46분) + 40초
//         = 2시간 46분 40초
int out_date_h = out_date / 3600;
int out_date_m = (out_date%3600) / 60;
int out_date_s = out_date%60;

 

2. 문자열 포맷코드

코드 설명
%s 문자열 (String)
%c 문자 1개 (character)
%d 정수 (Integer)
%f 부동소수 (floating-point)
%o 8진수 
%x 16진수
%% Literal % (문자 % 자체)
//표현방식 2가지
//(HH:MM:SS)
//1. String.format 사용
//String ans = String.format("%02d:%02d:%02d", out_date_h, out_date_m, out_date_s);
//System.out.println(ans)

//2.printf 사용
System.out.printf("%02d:%02d:%02d", out_date_h, out_date_m, out_date_s);