Hodustory/프로그래밍&DB

BeakJoon 4673 / 셀프넘버 (JAVA 코드)

호두밥 2019. 10. 23. 15:18
public class Main {
	public static void main(String[] args) {
		int cnt = 10000;
        //1~10000까지의 정수의 셀프넘버 확인 여부를 담을 배열을 선언합니다.
        
		boolean[] arr = new boolean[cnt];
        //셀프넘버 확인여부 배열을 모드 false로 초기화합니다.
		for(int b = 0; b<cnt;b++) {
			arr[b] = false;
		}
	
    	//1~1000까지의 정수를 계산하여 생성자를 도출한 뒤, 
        //생성자가 나오면, 확인여부 배열[생성자 값 위치]를 true로 변환합니다.
        
		for (int i = 0; i < cnt; i++) {			
			int dn = i+1;
			int temp = i+1;
      
			while (temp > 0) {
				dn += temp % 10;
				temp = temp / 10;
			}
            //생성자가 10000보다 작은 경우만 한해 변환합니다. 그렇지 않으면 indexOut오류 발생
			if (dn < cnt) {
				arr[dn-1] = true;	
			} else {
			}
		}
        //셀프넘버 배열을 확인해 출력
		for (int j = 0; j < cnt-1; j++) {
			if (!arr[j]) {
				System.out.println(j+1);
			}
		}
	}
}
반응형