์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step2] 2884 : ์•Œ๋žŒ ์‹œ๊ณ„ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 6. 18:02

www.acmicpc.net/problem/2884

 

2884๋ฒˆ: ์•Œ๋žŒ ์‹œ๊ณ„

์ƒ๊ทผ์ด๋Š” ๋งค์ผ ์•„์นจ ์•Œ๋žŒ์„ ๋“ฃ๊ณ  ์ผ์–ด๋‚œ๋‹ค. ์•Œ๋žŒ์„ ๋“ฃ๊ณ  ๋ฐ”๋กœ ์ผ์–ด๋‚˜๋ฉด ๋‹คํ–‰์ด๊ฒ ์ง€๋งŒ, ํ•ญ์ƒ ์กฐ๊ธˆ๋งŒ ๋” ์ž๋ ค๋Š” ๋งˆ์Œ ๋•Œ๋ฌธ์— ๋งค์ผ ํ•™๊ต๋ฅผ ์ง€๊ฐํ•˜๊ณ  ์žˆ๋‹ค. ์ƒ๊ทผ์ด๋Š” ๋ชจ๋“  ๋ฐฉ๋ฒ•์„ ๋™์›ํ•ด๋ณด์•˜์ง€๋งŒ,

www.acmicpc.net


๋ฌธ์ œ

์ƒ๊ทผ์ด๋Š” ๋งค์ผ ์•„์นจ ์•Œ๋žŒ์„ ๋“ฃ๊ณ  ์ผ์–ด๋‚œ๋‹ค. ์•Œ๋žŒ์„ ๋“ฃ๊ณ  ๋ฐ”๋กœ ์ผ์–ด๋‚˜๋ฉด ๋‹คํ–‰์ด๊ฒ ์ง€๋งŒ, ํ•ญ์ƒ ์กฐ๊ธˆ๋งŒ ๋” ์ž๋ ค๋Š” ๋งˆ์Œ ๋•Œ๋ฌธ์— ๋งค์ผ ํ•™๊ต๋ฅผ ์ง€๊ฐํ•˜๊ณ  ์žˆ๋‹ค.

์ƒ๊ทผ์ด๋Š” ๋ชจ๋“  ๋ฐฉ๋ฒ•์„ ๋™์›ํ•ด๋ณด์•˜์ง€๋งŒ, ์กฐ๊ธˆ๋งŒ ๋” ์ž๋ ค๋Š” ๋งˆ์Œ์€ ๊ทธ ์–ด๋–ค ๊ฒƒ๋„ ์—†์•จ ์ˆ˜๊ฐ€ ์—†์—ˆ๋‹ค.

์ด๋Ÿฐ ์ƒ๊ทผ์ด๋ฅผ ๋ถˆ์Œํ•˜๊ฒŒ ๋ณด๋˜, ์ฐฝ์˜์ด๋Š” ์ž์‹ ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ถ”์ฒœํ•ด ์ฃผ์—ˆ๋‹ค.

๋ฐ”๋กœ "45๋ถ„ ์ผ์ฐ ์•Œ๋žŒ ์„ค์ •ํ•˜๊ธฐ"์ด๋‹ค.

์ด ๋ฐฉ๋ฒ•์€ ๋‹จ์ˆœํ•˜๋‹ค. ์›๋ž˜ ์„ค์ •๋˜์–ด ์žˆ๋Š” ์•Œ๋žŒ์„ 45๋ถ„ ์•ž์„œ๋Š” ์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค. ์–ด์ฐจํ”ผ ์•Œ๋žŒ ์†Œ๋ฆฌ๋ฅผ ๋“ค์œผ๋ฉด, ์•Œ๋žŒ์„ ๋„๊ณ  ์กฐ๊ธˆ ๋” ์ž˜ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด, ๋งค์ผ ์•„์นจ ๋” ์žค๋‹ค๋Š” ๊ธฐ๋ถ„์„ ๋Š๋‚„ ์ˆ˜ ์žˆ๊ณ , ํ•™๊ต๋„ ์ง€๊ฐํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

ํ˜„์žฌ ์ƒ๊ทผ์ด๊ฐ€ ์„ค์ •ํ•œ ์•Œ๋žŒ ์‹œ๊ฐ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฐฝ์˜์ด์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์ด๋ฅผ ์–ธ์ œ๋กœ ๊ณ ์ณ์•ผ ํ•˜๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ H์™€ M์ด ์ฃผ์–ด์ง„๋‹ค. (0 ≤ H ≤ 23, 0 ≤ M ≤ 59) ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ์€ ํ˜„์žฌ ์ƒ๊ทผ์ด๊ฐ€ ์„ค์ •ํ•œ ๋†“์€ ์•Œ๋žŒ ์‹œ๊ฐ„ H์‹œ M๋ถ„์„ ์˜๋ฏธํ•œ๋‹ค.

์ž…๋ ฅ ์‹œ๊ฐ„์€ 24์‹œ๊ฐ„ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•œ๋‹ค. 24์‹œ๊ฐ„ ํ‘œํ˜„์—์„œ ํ•˜๋ฃจ์˜ ์‹œ์ž‘์€ 0:0(์ž์ •)์ด๊ณ , ๋์€ 23:59(๋‹ค์Œ๋‚  ์ž์ • 1๋ถ„ ์ „)์ด๋‹ค. ์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋‚ผ ๋•Œ, ๋ถˆํ•„์š”ํ•œ 0์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ƒ๊ทผ์ด๊ฐ€ ์ฐฝ์˜์ด์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๋•Œ, ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ์•Œ๋žŒ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค. (์ž…๋ ฅ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.)

 

์˜ˆ์ œ ์ž…๋ ฅ 1

10 10

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

9 25

 

์˜ˆ์ œ ์ž…๋ ฅ 2

0 30

 

์˜ˆ์ œ ์ถœ๋ ฅ 2

23 45

 

์˜ˆ์ œ ์ž…๋ ฅ 3

23 40

 

์˜ˆ์ œ ์ถœ๋ ฅ 3

22 55


ํ’€์ด

1. h์‹œ๊ฐ„ m๋ถ„์„ h * 60 + m๋ถ„์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค

2. h * 60 + m๋ถ„์—์„œ 45๋ถ„์„ ๋บ€๋‹ค == time

3. time์ด ์Œ์ˆ˜์ธ์ง€ ํ™•์ธํ•œ๋‹ค (0์‹œ์ผ ๊ฒฝ์šฐ 45๋ถ„์„ ๋นผ๋ฉด ์Œ์ˆ˜)

4. ๊ฒฐ๊ณผ ์ถœ๋ ฅ

   4-1. ์–‘์ˆ˜๋ฉด time์„ ์ด์šฉํ•ด์„œ time/60 ์‹œ time%60 ๋ถ„์œผ๋กœ ์ถœ๋ ฅ

   4-2. ์Œ์ˆ˜๋ฉด 23์‹œ m+15๋ถ„ ์œผ๋กœ ์ถœ๋ ฅ

 

import java.util.Scanner;

public class B2884 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int h = sc.nextInt();
		int m = sc.nextInt();
		
		int time = h * 60 + m - 45;
		
		if ( time >= 0 ) {
			System.out.println( (time/60) + " " + (time%60) );
		} else if ( time < 0 ) {
			System.out.println("23 " + (15+m) );
		}

	}

}

 

์ด๋ ‡๊ฒŒ ์ œ์ถœ ํ•ด์„œ ํ†ต๊ณผ๋Š” ํ–ˆ๋Š”๋ฐ

import java.util.Scanner;

public class B2884 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int h = sc.nextInt();
		int m = sc.nextInt();
		
		if ( m >= 45 && h != 0 ) {
			System.out.println( h + " " + ( m-45 ) );
		} else if ( m < 45 && h != 0 ) {
			System.out.println( (h-1) + " " + ( 15+m ) );
		} else if ( m >= 45 && h == 0 ) {
			System.out.println( 23 + " " + ( m-45 ) );
		} else if ( m < 45 && h == 0 ) {
			System.out.println( 23 + " " + ( 15+m ) );
		}

	}

}
import java.util.Scanner;

public class B2884 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int h = sc.nextInt();
		int m = sc.nextInt();
		
		if (h != 0) {
			if ( m >= 45) {
				System.out.println( h + " " + ( m-45 ) );
			} else if ( m < 45 ) {
				System.out.println( (h-1) + " " + ( 15+m ) );
			}
		} else {
			if ( m >= 45 ) {
				System.out.println( 23 + " " + ( m-45 ) );
			} else if ( m < 45 ) {
				System.out.println( 23 + " " + ( 15+m ) );
			}
		}
	}

}

์ด ๋‘ ๊ฐœ๋Š” ์–ด๋Š ๋ถ€๋ถ„๋•Œ๋ฌธ์— ์•ˆ๋˜๋Š”๊ฑด์ง€ ๋ชจ๋ฅด๊ฒ ๋”ฐ..ใ…œใ