본문 바로가기
Algorithm/Baekjoon

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

by unknownomad 2022. 5. 10.

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

 

1436번: 영화감독 숌

666은 종말을 나타내는 숫자라고 한다. 따라서, 많은 블록버스터 영화에서는 666이 들어간 제목을 많이 사용한다. 영화감독 숌은 세상의 종말 이라는 시리즈 영화의 감독이다. 조지 루카스는 스타

www.acmicpc.net



풀이

1. 브루트포스 + Scanner

  • 입력받은 N과 count가 같아질 때까지 num을 1씩 증가시켜 666이 포함되는지 계속 검사
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
		
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
 
        int num = 666;
        int count = 1;
        
        //범위 : 1 ~ N 까지 전체 구간 검사
        while(count != N) {
            num++;
            if(String.valueOf(num).contains("666")) {
                count++;
            }
        }
        System.out.println(num);
    }
}

 

2. 브루트포스 + BufferedReader

  • 입력받은 N과 count가 같아질 때까지 num을 1씩 증가시켜 666이 포함되는지 계속 검사
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
 
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 num = 666;
        int count = 1;
        
        //범위 : 1 ~ N 까지 전체 구간 검사
        while(count != N) {
            num++;
            if(String.valueOf(num).contains("666")) {
                count++;
            }
        }
        System.out.println(num);
    }
}

3. 범위 나누기 + Scanner

  • 각 선수 자릿수의 값에 따라 경우의 수 나누기
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();	
 
        if(N > 1) {
            func(N);
        } 
        else {
            System.out.println(666);
        }
    }
 
    public static void func(int n) {
        int count = 1;
        int prev_digit = 0; //선수 자릿수
        int num = 0; //선수 자릿수를 제외한 나머지 뒷 자릿수
		
        /*
         * '_'(언더바) 기준으로 표현 (ex. (prev_digit)_num)
         * 자릿수에 따라 num은 0 또는 600, 660, 666을 가짐
         */
        while (true) {
            /*
             * 선수 자릿수 = X...666X && 선수 자릿수 != X...6666
             * (ex. 6660_000, 6660_001, ...)
             */
            if(((prev_digit % 10000) / 10) == 666 && prev_digit % 10 != 6) {
                for(int i = 0; i < 1000; i++) {
                    if(count == n) {
                        System.out.print(prev_digit * 1000 + num);
                        return;
                    }
                    num++;
                    count++;
                }
                prev_digit++;
            }
            //선수 자릿수 = X...666 (ex. 666_000, 1666_004, ...)
            else if(prev_digit % 1000 == 666) {
                num = 0;
                for(int i = 0; i < 1000; i++) {
                    if(count == n) {
                        System.out.print(prev_digit * 1000 + num);
                        return;
                    }
                    num++;
                    count++;
                }
                prev_digit++;
            }
            //선수 자릿수 = X...66 (ex. 66_600, 166_600, ...)
            else if(prev_digit % 100 == 66) {
                num = 600;
                for(int i = 0; i < 100; i++) {
                    if(count == n) {
                        System.out.print(prev_digit * 1000 + num);
                        return;
                    }
                    num++;
                    count++;
                }
                prev_digit++;
            }
            //선수 자릿수 = X...6 (ex. 6_660, 16_663, ...) 
            else if(prev_digit % 10 == 6) {
                num = 660;
                for(int i = 0; i < 10; i++) {
                    if(count == n) {
                        System.out.print(prev_digit * 1000 + num);
                        return;
                    }
                    num++;
                    count++;
                }
                prev_digit++;
            } 
            //그 외의 경우 (ex. 241_666, 23_666 ...)
            else {
                num = 666;
                if(count == n) {
                    System.out.print(prev_digit * 1000 + num);
                    return;
                }
                count++;
                prev_digit++;
            }
        }
    }
}

 

4. 범위 나누기 + BufferedReader

  • 각 선수 자릿수의 값에 따라 경우의 수 나누기
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
 
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());
 
        if (N > 1) {
            func(N);
        } 
        else {
            System.out.println(666);
        }
    }
 
    public static void func(int n) {
        int count = 1;
        int prev_digit = 0; //선수 자릿수
        int num = 0; //선수 자릿수를 제외한 나머지 뒷 자릿수
		
        /*
         * '_'(언더바) 기준으로 표현 (ex. (prev_digit)_num)
         * 자릿수에 따라 num은 0 또는 600, 660, 666을 가짐
         */
        while (true) {
            /*
             * 선수 자릿수 = X...666X && 선수 자릿수 != X...6666
             * (ex. 6660_000, 6660_001, ...)
             */
            if(((prev_digit % 10000) / 10) == 666 && prev_digit % 10 != 6) {
                for(int i = 0; i < 1000; i++) {
                    if(count == n) {
                        System.out.print(prev_digit * 1000 + num);
                        return;
                    }
                    num++;
                    count++;
                }
                prev_digit++;
            }
            //선수 자릿수 = X...666 (ex. 666_000, 1666_004, ...)
            else if(prev_digit % 1000 == 666) {
                num = 0;
                for(int i = 0; i < 1000; i++) {
                    if(count == n) {
                        System.out.print(prev_digit * 1000 + num);
                        return;
                    }
                    num++;
                    count++;
                }
                prev_digit++;
            }
            //선수 자릿수 = X...66 (ex. 66_600, 166_600, ...)
            else if(prev_digit % 100 == 66) {
                num = 600;
                for(int i = 0; i < 100; i++) {
                    if(count == n) {
                        System.out.print(prev_digit * 1000 + num);
                        return;
                    }
                    num++;
                    count++;
                }
                prev_digit++;
            }
            //선수 자릿수 = X...6 (ex. 6_660, 16_663, ...) 
            else if(prev_digit % 10 == 6) {
                num = 660;
                for(int i = 0; i < 10; i++) {
                    if(count == n) {
                        System.out.print(prev_digit * 1000 + num);
                        return;
                    }
                    num++;
                    count++;
                }
                prev_digit++;
            } 
            //그 외의 경우 (ex. 241_666, 23_666 ...)
            else {
                num = 666;
                if(count == n) {
                    System.out.print(prev_digit * 1000 + num);
                    return;
                }
                count++;
                prev_digit++;
            }
        }
    }
}

 


출처 : https://st-lab.tistory.com/103

'Algorithm > Baekjoon' 카테고리의 다른 글

[백준] 25304번 : 영수증 - Java  (0) 2023.07.19
[백준] 11382번 : 꼬마 정민 - Java  (0) 2023.07.18
[백준] 25083번 : 새싹 - Java  (0) 2022.05.10
[백준] 7568번 : 덩치 - Java  (0) 2022.04.26
[백준] 2231번 : 분해합 - Java  (0) 2022.04.12

댓글