题目没有任何输入。
输出2到60之间所有“完数”和“盈数”,并以如下形式输出: E: e1 e2 e3 ......(ei为完数) G: g1 g2 g3 ......(gi为盈数) 其中两个数之间要有空格,行尾不加空格。
无
按题目要求进行输出即可。
import java.util.*; public class Main{ public static void main(String[]args){ StringBuffer w = new StringBuffer(); StringBuffer y = new StringBuffer(); for(int i = 1;i<=60;i++){ int sum = 0; for(int j = 1;2*j<=i;j++){ if(i%j==0){ sum+=j; } } if(i==sum){ w.append(" "+i); } else if(sum>i){ y.append(" "+i); } sum = 0; } System.out.println("E:"+w); System.out.println("G:"+y); } }
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> g = new ArrayList<>(); ArrayList<Integer> e = new ArrayList<>(); for (int i = 2; i <= 60; i++) { int sum=0; for (int j = 1; j < i; j++) if (i%j==0) sum+= j; if (sum==i) e.add(i); else if (sum>i) g.add(i); } System.out.print("E: "); for (Integer i : e) System.out.print(i+" "); System.out.println(); System.out.print("G: "); for (Integer i : g) System.out.print(i+" "); } }
public class Main{public static void main(String[] args){
import java.util.*; public class Main { public static void main(String[] args) { ArrayList<Integer> E = new ArrayList<>(); ArrayList<Integer> G = new ArrayList<>(); for (int i = 2; i < 61; ++i) { int sum = 1; for (int j = 2; j*j < i; ++j) { if (i%j == 0) { sum += j; sum += (i/j); } } if (sum == i) { E.add(i); } else if (sum > i) { G.add(i); } } StringBuilder sb = new StringBuilder(); for (int i: E) { sb.append(i+" "); } System.out.println("E: " + sb.substring(0, sb.length()-1).toString()); sb = new StringBuilder(); for (int j: G) { sb.append(j+" "); } System.out.println("G: " + sb.substring(0, sb.length()-1).toString()); } }
import java.util.ArrayList; /** * Created by fhqplzj on 17-1-26 at 下午6:30. */ public class My111 { private static int getSum(int n) { int result = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { result += i; } } return result; } public static void main(String[] args) { /*完数*/ ArrayList<Integer> perfect = new ArrayList<>(); /*盈数*/ ArrayList<Integer> over = new ArrayList<>(); for (int i = 2; i <= 60; i++) { int sum = getSum(i); if (sum == i) { perfect.add(i); } else if (sum > i) { over.add(i); } } System.out.print("E:"); for (Integer integer : perfect) { System.out.print(" " + integer); } System.out.println(); System.out.print("G:"); for (Integer integer : over) { System.out.print(" " + integer); } System.out.println(); } }