시각 (Java)

박지훈·2021년 2월 26일
0

문제

정수 N이 입력되면 00시 00분 00초부터 N시 59분 59초까지의 모든 시각 중에서 3이 하나라도 포함되는 모든 경우의 수를 구하는 프로그램을 작성하시오. 예를 들어 1을 입력했을 때 다음은 3이 하나라도 포함되어 있으므로 세어야 하는 시각이다.

  • 00시 00분 03초
  • 00시 13분 30초

반면에 다음은 3이 하나도 포함되어 있지 않으므로 세면 안 되는 시각이다.

  • 00시 02분 55초
  • 01시 27분 45초

(입력 조건)

  • 첫째 줄에 정수 N이 입력된다. (0<=N<=23)

(출력조건)

  • 00시 00분 00초부터 N시 59분 59초까지의 모든 시각 중에서 3이 하나라도 포함되는 모든 경우의 수를 출력한다.

입력 예시
5

출력 예시
11475



풀이

  1. 시각을 흐르게 만든다. (시, 분, 초로 나누어 흐르게 한다.)

  2. 시각에 3이 하나라도 존재할 때 마다 답을 1개씩 추가한다.



코드

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());
        int answer = 0;

        for (int hour = 0; hour <= N; hour++) {
            for (int min = 0; min <= 59; min++) {
                for (int sec = 0; sec <= 59; sec++) {
                    if (hour % 10 == 3 || min / 10 == 3 || min % 10 == 3 || sec / 10 ==3 || sec % 10 ==3) answer++;
                }
            }
        }

        System.out.println(answer);
    }
}
profile
Computer Science!!

3개의 댓글

comment-user-thumbnail
2023년 7월 14일

This subway surfers program prompts the user to enter the value of N and calculates the number of times from 00:00:00 to N:59:59 that contain at least one digit 3. It uses nested loops to iterate through the time components and checks if any of them contain the digit 3. The count is incremented whenever a match is found. Finally, the program outputs the result.

답글 달기
comment-user-thumbnail
2024년 1월 2일

I'm feeling a bit apprehensive about this Buckshot Roulette section on time manipulation in Java. It seems like it might involve some tricky logic and calculations.

답글 달기
comment-user-thumbnail
2024년 2월 18일

Just went through your post on counting instances where the number 3 appears in the time from 00:00:00 to N:59:59 using Java. The approach of using nested loops to check every hour, minute, and second for the presence of a '3' is a clever way to tackle this problem. It's a neat example of how straightforward logic can solve what seems like a complex issue at first glance. Y9 Really enjoyed how you laid out the solution and the code snippet provided. Great job on breaking down the problem and offering a clear solution!

답글 달기