백준 1515

hong030·2023년 3월 8일
1
  • 실버 3단계 문제

풀이)

입력받은 숫자를 하나하나 비교하는 bruceforce 방식을 쓰면 된다. 입력값의 숫자는 3000개 이하기 때문에 시간초과가 나지 않는다.

주의할 점)

처음엔 bruceforce로 풀 생각을 하지 못하고, int result를 입력받은 숫자에 따라 if else 문으로 변환하는 식으로 풀고자 하였다.
이 경우 조건이 너무 많아져 오히려 복잡해지고 시간도 오래 걸린다.
조건을 잘 살피고 bruce force로 진행하는 방법을 생각하자.

내 코드)

import java.io.*;

public class Backjoon1515 {
    static void calc() throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String input = bf.readLine();        
        int result = 0;
        int index = 0;
        
        while(true) {
        	result++;
        	String resString = Integer.toString(result);
        	for(int i=0;i<resString.length();i++) {
        		if(resString.charAt(i) == input.charAt(index))
        			index++;
        		if(index == input.length()) {
        			System.out.println(result);
        			return;
        		}
        	}
        }
       
    }

    public static void main(String[] args) throws Exception {
        calc();
    }
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글