规规矩矩用set不香吗
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
for (int i = 1; i <= n; i++) {
if (perfectNumber(i)) {
count++;
}
}
System.out.println(count);
}
private static boolean perfectNumber(int n) {
//查找n的所有因子,存在set中
int sum = 0;
HashSet<Integer> set = new HashSet<>();
for (int i = 1; i < n; i++) {
if (n % i == 0) {
set.add(i);
sum += i;
}
}
return sum == n;
}
}