题解 | #完全数计算#
完全数计算
http://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* @Classname PerfectNumber
* @Description 完美数
* @Date 2022/2/13 17:36
* @Created by daihuhu
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
int count = 0;
for (int i = 6; i <= n ; i++) {
if(isPerfectNumber(i)) {
count++;
}
}
System.out.println(count);
}
}
private static boolean isPerfectNumber(int i) {
int sum = 0;
for (int j = 1; j < i; j++) {
if(i % j == 0) {
sum += j;
}
}
if(sum == i){
return true;
}
return false;
}
}