题解 | #完全数计算#
完全数计算
https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
import java.util.Scanner; /** 找不到什么规律,只能暴力解法!! */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int n = in.nextInt(); int count = 0; for(int i = 2; i <= n; i++){ int sum = 0; for(int j = 1; j <= Math.sqrt(i); j++){ if(i % j == 0){ if(j == 1 || j == i / j){ sum += j; }else{ sum = sum + j + i / j; } } } if(sum == i){ count++; } } System.out.println(count); } } }