参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int num = 2; num <= n; num++) { if (isPerfectNumber(num)) { System.out.print(num + " its factors are 1"); for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { System.out.print(" " + i); } } System.out.println(); } } } public static boolean isPerfectNumber(int num) { int sum = 1; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { sum += i; } } return sum == num; } }
0.0分
0 人评分
C二级辅导-计负均正 (C语言代码)浏览:652 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:627 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:545 |
九宫重排 (C++代码)浏览:2195 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:628 |
用筛法求之N内的素数。 (C++代码)浏览:754 |
DNA (C语言代码)浏览:440 |
【计算直线的交点数】 (C语言代码)浏览:986 |
统计立方数 (C语言代码)浏览:890 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:518 |