시각 (Java)

박지훈·2021년 2월 26일

문제

정수 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!!

7개의 댓글

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!

답글 달기
comment-user-thumbnail
2025년 5월 8일

오! Java로 시간에서 3이 들어간 횟수 세는 문제라니, 신박하네요! 코딩 테스트에 나올 법한 스타일인데, 꼼꼼하게 풀어놓으신 것 같아요. 저도 한번 풀어봐야겠어요! @chill guy clicker

답글 달기
comment-user-thumbnail
2025년 7월 25일

코딩에 대한 열정이 느껴져서 좋네요! Java 시간 계산 알고리즘은 정말 실무에서 자주 쓰이죠.
특히 날짜/시간 처리할 때는 LocalDateTime과 Duration 클래스 조합이 효과적이고, 시간대 처리가 필요하면 ZonedDateTime도 고려해보세요. 또한 성능이 중요한 상황에서는 Instant 클래스로 나노초 단위 계산도 가능합니다. 이런 기술적 역량을 개발자 취업에 활용하려면 AI Cover Letter Generator로 본인의 코딩 스킬과 프로젝트 경험을 효과적으로 어필할 수 있어요. 실제 프로젝트에서 이런 기법들을 적용해보시면 더욱 탄탄한 코드를 작성할 수 있을 거예요!

답글 달기
comment-user-thumbnail
2025년 8월 4일

깔끔하고 간단한 접근법이네요! 운이 좋을까요? '3'이 나올지 보려고 Random Tarot Card 를 뽑는 것 같아요! 이해하기 쉬운 코드입니다.

답글 달기
comment-user-thumbnail
2026년 3월 9일

완전 탐색 문제 풀이 깔끔하네요! 시간 내 '3' 포함 여부 찾는 아이디어가 신선해요. 혹시 AI Video Generator 활용해서 코딩 과정을 시각적으로 설명하는 영상 만들어봐도 재밌을 것 같아요!

답글 달기