题解 | HJ56 #完全数计算#
完全数计算
https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int nextInt = scanner.nextInt(); int i = nextInt; int flag = 1; ArrayList<Integer> list = new ArrayList<>(); while (flag <= i) { if (IsCompleteNum(flag)) { list.add(flag); } flag++; } System.out.println(list.size()); } public static boolean IsCompleteNum(int i) { int count = 0; int flag = 1; while (flag < i) { if (i % flag == 0) { count += flag; } flag++; } if (count == i) { return true; } else { return false; } } }