자바/백준 알고리즘(자바)
[백준/자바] 4673번: 셀프 넘버 구하기
Heeyeon Choi
2021. 11. 10. 16:30
728x90
728x90
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String args[]) throws NumberFormatException, IOException{
int num=1, count=0, numResult;
//hastset을 이용하여 중복값을 제거하도록 한다.
HashSet<Integer> set1 = new HashSet<Integer>();
//set1에 1~10000까지의 수를 일단 저장한다.
for(int i=1; i<10001; i++) {
set1.add(i);
}
//set1에 셀프넘버를 구하기 위해 d()함수를 통해 얻은 수를 제거한다.
for(int i=1; i<10001; i++) {
set1.remove(d(i));
}
//출력한다.
for(int numA: set1)
{
System.out.println(numA);
}
}
//자신과 자신을 이루는 각 자리수를 더해주는 함수(셀프넘버의 반대개념)
public static int d(int n){
String s= Integer.toString(n);
int resN,sum=n;
for(int i=0; i<s.length();i++) {
resN = n%10;
sum += resN;
n=n/10;
}
return sum;
}
}
728x90