Problem Solving/Baekjoon
[백준] 1515 수 이어 쓰기 - Greedy / Java
graycode
2023. 8. 18. 20:52
• 문제 링크
1515번: 수 이어 쓰기
세준이는 1부터 N까지 모든 수를 차례대로 공백없이 한 줄에 다 썼다. 그리고 나서, 세준이가 저녁을 먹으러 나간 사이에 다솜이는 세준이가 쓴 수에서 마음에 드는 몇 개의 숫자를 지웠다. 세준
www.acmicpc.net
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
static String s;
static int idx;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
s = br.readLine();
int n = 0;
while (n++ < 30001) if (contains(String.valueOf(n))) break;
bw.write(String.valueOf(n));
bw.flush();
}
private static boolean contains(String n) {
for (int i = 0; i < n.length(); i++) {
if (n.charAt(i) == s.charAt(idx)) idx++;
if (idx == s.length()) return true;
}
return false;
}
}