반응형
```
백준 6975번 Deficient, Perfect, and Abundant JAVA 구현해보기
```
이번 글을 통해 배워갈 내용
- 백준 6975 번 풀이
https://www.acmicpc.net/problem/6975
6975번: Deficient, Perfect, and Abundant
Write a program that repeatedly reads a positive integer, determines if the integer is deficient, perfect, or abundant, and outputs the number along with its classification. A positive integer, n, is said to be perfect if the sum of its proper diviso
www.acmicpc.net
난이도 브론즈 문제입니다.
양수 n이 주어질 때
n의 divisors(제수들 즉 n을 해당 수로 나눠서 0이 나오는 수)의 합이
n 과 같다면 perfect
n보다 작다면 deficient
n보다 크다면 abundant를 기준에 맞게 출력해주면 됩니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
합을 구해준다음 기준에 맞게 출력해주면 됩니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int testCase = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < testCase; i++) {
sb.append(findNumberType(Integer.parseInt(br.readLine()))).append("\n\n");
}
sb.setLength(sb.length()-2);
System.out.println(sb);
}
// is said to be perfect if the sum of its proper divisors equals the number itself
// If this sum is less that n, the number is deficient
// if the sum is greater than n, the number is abundant.
private static String findNumberType(int curNum) {
int sumOfDivisors = 0;
for (int i = 1; i < curNum; i++) {
if (curNum % i == 0) {
sumOfDivisors += i;
}
}
if (sumOfDivisors == curNum) {
return curNum + " is a perfect number.";
} else if (sumOfDivisors < curNum) {
return curNum + " is a deficient number.";
} else if (sumOfDivisors > curNum) {
return curNum + " is an abundant number.";
} else {
return "error";
}
}
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩하시길 바랍니다 ~ :)
728x90
반응형
'Java > Java 알고리즘' 카테고리의 다른 글
백준 11656번 접미사배열 JAVA 구현해보기 (0) | 2022.02.08 |
---|---|
백준 10866번 덱 JAVA 구현해보기 (0) | 2022.02.06 |
백준 2845번 파티가 끝나고 난 뒤JAVA 구현해보기 (0) | 2022.02.05 |
백준 4299번 축구점수 JAVA 구현해보기 (0) | 2022.02.05 |
백준 4153번 직각삼각형 JAVA 구현해보기 (0) | 2022.02.05 |