题解 | #完全数计算#
完全数计算
http://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
规规矩矩用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;
}
}