14563번: 완전수
어떠한 자연수 N에 대해서 N을 제외한 약수(진약수)의 합이 N이 되는 자연수를 완전수라고 한다. 예를 들어, 6의 약수는 1, 2, 3, 6인데 1+2+3은 6이기 때문에 완전수이다. 또 진약수의 합이 자기 자신
www.acmicpc.net
문제
어떠한 자연수 N에 대해서 N을 제외한 약수(진약수)의 합이 N이 되는 자연수를 완전수라고 한다. 예를 들어, 6의 약수는 1, 2, 3, 6인데 1+2+3은 6이기 때문에 완전수이다. 또 진약수의 합이 자기 자신보다 작은 경우를 부족수, 진약수의 합이 자기 자신보다 큰 경우를 과잉수라고 한다.
이때, 어떤 수가 주어질 때 이 수가 완전수인지, 부족수인지, 과잉수인지를 구하는 프로그램을 작성하시오.
입력
첫째 줄에 자연수의 개수 T가 주어진다. T은 1000보다 작은 수이다.
둘째 줄에는 공백을 사이에 두고 완전수인지 구해야 되는 자연수 N이 주어진다.(N<10000)
출력
T개 줄에 걸쳐서 각 자연수에 대해서 완전수면 ‘Perfect’, 부족수면 ‘Deficient’, 과잉수면 ‘Abundant’를 출력한다.
예제 입력 | 예제 출력 |
3 28 21 36 |
Perfect Deficient Abundant |
[ JAVA ]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package baekjoon_Algorithm; | |
import java.util.*; | |
public class baekjoon_14563 { | |
public static void main(String [] args) { | |
Scanner scan = new Scanner(System.in); | |
scan.nextLine(); | |
int [] numArr = Arrays.asList(scan.nextLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray(); | |
for(int num : numArr) { | |
int result = solution(num); | |
if(num > result) | |
System.out.println("Deficient"); | |
else if(num == result) | |
System.out.println("Perfect"); | |
else | |
System.out.println("Abundant"); | |
} | |
} | |
public static int solution(int num) { | |
int sum = 0; | |
for(int i=1; i<num; i++) { | |
if(num%i == 0) | |
sum += i; | |
} | |
return sum; | |
} | |
} |

'알고리즘' 카테고리의 다른 글
[ 백준 10814 ] 나이순 정렬 (0) | 2021.03.24 |
---|---|
[ 백준 1181 ] 단어 정렬 (0) | 2021.03.24 |
[ 백준 2960 ] 에라토스테네스의 체 (0) | 2021.03.24 |
[ 백준 1182 ] 부분수열의 합 (2) | 2021.03.23 |
[ 백준 1193 ] 분수찾기 (0) | 2021.03.23 |
댓글