题解 | #完全数计算#
完全数计算
http://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
解题思路】: 本题可以通过遍历每一个约数,求和,判断完全数。约数计算可以遍历sqrt(n)的范围。
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i < n; i++) {
if(is_num(i)){
sum++;
}
}
System.out.println(sum);
}
private static boolean is_num(int x) {
int[] nums = new int[x];
int count = 0;
for (int i = 1; i < x; i++) {
if(x%i==0){
nums[count++]=i;
}
}
if(is_(nums,x)){
return true;
}
else return false;
}
private static boolean is_(int[] nums, int x) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum+=nums[i];
}
if(sum==x) return true;
else return false;
}
}