package com.demo;
/**
* Created by 莫文龍 on 2018/3/27.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* 一個數如果剛好等於它的因子之和,這個數就稱爲"完數"。
* 例如6 = 1+2+3,編程實現求[a,b]之間的所有完數
*
*/
public class Demo9 {
//創建一個新的數組就是這樣的數組就可以了嗎
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String[] split = str.split(" ");
int a = Integer.parseInt(split[0]);
int b = Integer.parseInt(split[1]);
for (int i = a ; i <= b ; i ++) {
HashSet<Integer> set = new HashSet<>();
for (int j = 1 ; j < i ; j ++) {
if (i % j == 0) {
set.add(j);
}
}
int sum = 0;
for (Integer ii : set) {
sum += ii;
}
if (sum == i) {
System.out.println(i);
}
}
}
}
50道編程題之09:一個數如果剛好等於它的因子之和,這個數就稱爲"完數"。例如6 = 1+2+3,編程實現求[a,b]之間的所有完數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.