[Java][백준] #1436 - 영화감독 숌

배수연·2024년 4월 5일

algorithm

목록 보기
21/45

🔗 백준 1436 - 영화감독 숌

문제

알고리즘 분류

  • 브루트포스 알고리즘

풀이

1. 입력

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

2. 구현

  • 1씩 증가시키면서, 666이 나올 때마다 count를 증가
    -> ex) 1번째 종말의 수(666)이 나올 때 count = 1
    -> ex) 2번째 종말의 수(1666)이 나올 때 count = 2
  • 따라서 count와 우리가 구하고자 하는 n과 같을 때, n번째 종말의 수를 알 수 있음.
        int i = 0;
        int count = 0;
        while(true){
            i++;
            String s = String.valueOf(i);
            if(s.contains("666")){
                count++;
            }
            if(count == n){
                break;
            }
        }
        System.out.println(i);

전체 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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 i = 0;
        int count = 0;
        while(true){
            i++;
            String s = String.valueOf(i);
            if(s.contains("666")){
                count++;
            }
            if(count == n){
                break;
            }
        }
        System.out.println(i);
    }
}

0개의 댓글