Baekjoon Algorithm | Problem 4673: Self Numbers
Source
https://www.acmicpc.net/problem/4673
Problem
Solve Baekjoon Online Judge problem 4673, Self Numbers.
Input
Follow the input format and constraints given in the original problem statement.
Output
Print the answer required by the problem.
Sample Input 1
Sample Output 1
1 3 5 7 9 20 31 42 53 64 | | <-- a lot more numbers | 9903 9914 9925 9927 9938 9949 9960 9971 9982 9993
Algorithm Classification
- Math
- Implementation
- Brute Force
Solution
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> notSelfNumber = new HashSet();
for(int i = 1; i <= 10000; i++) {
int sum = i;
if(i < 10){
sum += i;
} else {
int length = String.valueOf(i).length();
for(int j = 1; j <= length; j++){
if(j == 1){
sum += i % 10;
} else {
int y = 1;
for(int x = 1; x < j; x++) {
y = y * 10;
}
sum += (i / y) % 10;
}
}
}
notSelfNumber.add(sum);
}
for(int i = 1; i <= 10000; i++) {
if (notSelfNumber.contains(i)) {
continue;
}
System.out.println(i);
}
}
}
public class Main {
public static void main(String[] args) {
boolean[] notSelfNumber = new boolean[10000];
for (int i = 1; i <= 10000; i++) {
int sum = i;
int n = i;
while (n != 0) {
sum += (n % 10);
n /= 10;
}
if (sum - 1 < notSelfNumber.length) {
notSelfNumber[sum - 1] = true;
}
}
for (int i = 0; i < 10000; i++) {
if(notSelfNumber[i] == true){
continue;
}
System.out.println(i + 1);
}
}
}