[백준] 4673 : 셀프 넘버 - Java

길 잃은 까마귀·2022년 9월 14일
0

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


  • 문제

  • 풀이
    생성자를 구하는 함수를 만들어 생성자를 구하고 그 수에 맞는 boolean배열을 true로 만들어서 나중에 boolean배열을 판별했을때 거짓인것이 셀프 넘버이므로 이것들을 출력해주면 된다. 참고로 boolean은 true 나 false 둘중 하나의 값만 가지는 데이터 유형이다.

  • 코드
class Main {
	public static void main(String[] args) {
		boolean[] check = new boolean[10001];
		for (int i = 1; i <= 10000; i++) {
			int n = selfnum(i);
			if (n < 10001) {
				check[n] = true;
			}
		}
		for (int i = 1; i < 10001; i++) {
			if (!check[i]) {
				System.out.println(i);
			}
		}
	}

	public static int selfnum(int n) {
		int sum = n;
		while (n != 0) {
			sum += n % 10;
			n /= 10;
		}
		return sum;
	}
}
profile
코딩 고수가 될 사람

0개의 댓글