


n + n, 두 자리 수일때는 n + (n / 10) + (n % 10)과 같이 반복이 된다. 자리 수에 따른 index 값을 구한 뒤 생성되는 수의 자리에 0을 넣도록 했다.import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] arr = new int[10001];
arr[0] = 0;
for (int i = 1; i <= 10000; i++) {
arr[i] = i;
}
for (int i = 1; i <= 10000; i++) {
int index = 0;
if (i > 0 && i < 10) {
index = i + i;
if (index <= 1000) {
arr[index] = 0;
}
}
else if (i >= 10 && i < 100) {
index = i + (i / 10) + (i % 10);
if (index <= 10000) {
arr[index] = 0;
}
}
else if (i >= 100 && i < 1000) {
index = i + (i / 100) + (i % 100 / 10) + (i % 100 % 10);
if (index <= 10000) {
arr[index] = 0;
}
}
else if (i >= 1000 && i <= 10000) {
index = i + (i / 1000) + (i % 1000 / 100) + (i % 1000 % 100 / 10) + (i % 1000 % 100 % 10);
if (index <= 10000) {
arr[index] = 0;
}
}
}
for (int i = 1; i <= 10000; i++) {
if (arr[i] != 0) {
bfw.write(String.valueOf(arr[i] + "\n"));
}
}
bfw.flush();
bfw.close();
}
}
if-else문이 불필요하게 길어진다. 1부터 10000까지 for문을 돌리기 때문에 이중 for문을 쓰는 것은 실행 속도를 늦출까봐 사용하지 않았는데 다른 사람들의 코드를 참고해보니 그렇지 않은 것 같다. for문을 이용해서도 구현해보자.