작성일 :

문제 링크

6975번 - Deficient, Perfect, and Abundant

설명

입력으로 주어지는 양의 정수 n 이 부족한 수(deficient number) 인지, 완전한 수(perfect number) 인지,

아니면 풍부한 수(perfect number) 인지 판별하는 문제입니다.

문제의 조건에 따르면, 양의 정수 n 이 완전한 수라는 것은 자기 자신을 제외한 약수들의 합이 n 과 같다는 것을 의미합니다.

만약, 합이 n 보다 작으면 그 수는 부족한 수 이며, 합이 n 보다 크다면 그 수는 풍부한 수 입니다.

위 조건에 따라 구현을 진행한 후 n 이 어떤 수인지 판별하여 출력합니다.


Code

[ C# ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
namespace Solution {
  class Program {
    static void Main(string[] args) {

      var t = int.Parse(Console.ReadLine()!);

      for (int c = 0; c < t; c++) {
        var n = int.Parse(Console.ReadLine()!);

        int sum = 1;
        for (int i = 2; i * i <= n; i++) {
          if (n % i == 0) {
            if (i * i != n) sum += i + n / i;
            else sum += i;
          }
        }

        if (sum < n) Console.WriteLine($"{n} is a deficient number.");
        else if (sum == n) Console.WriteLine($"{n} is a perfect number.");
        else Console.WriteLine($"{n} is an abundant number.");

        if (c != t - 1) Console.WriteLine();
      }

    }
  }
}



[ C++ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;

  for (int c = 0; c < t; c++) {
    int n; cin >> n;

    int sum = 1;
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        if (i * i != n)
          sum += i + n / i;
        else
          sum += i;
      }
    }

    if (sum < n) cout << n << " is a deficient number.\n";
    else if (sum == n) cout << n << " is a perfect number.\n";
    else cout << n << " is an abundant number.\n";

    if (c != t - 1) cout << "\n";
  }

  return 0;
}

Tags: 6975, arithmetic, BOJ, C#, C++, 구현, 백준, 브루트포스, 수학, 알고리즘, 정수론

Categories: ,