同題:UVa 13185 – DPA Numbers I
一個整數 B 如果可以被另一個整數 A 整除 (在這裡 A > B),我們稱 B 是 A 的一個因數。
A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).
For example, 6 and 28 are both perfect numbers because 6 = 1 + 2 + 3, and 28 = 1 + 2 + 4 + 7 + 14.
如果一個正整數不是 Perfect,那他就是 Deficient 或者是 Abundant,根據其所有因數的和是小於或大於這個數本身。因此,9 是 Deficient 因為 1 + 3 < 9。而 12 是 Abundant 因為 1 + 2 + 3 + 4 + 6 > 12。
Please write a program to determine whether a given number is Perfect, Deficient, or Abundant.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line of input contains a positive integer t, representing the number of test cases to follow. (1 <= T <= 500) Each test case consists of a single line containing a positive integer N. (2 <= N <= 1000) | For each test case, output a single line indicating whether N is Perfect, Deficient, or Abundant. |
10 5 6 16 18 21 28 29 30 40 43 | deficient perfect deficient abundant deficient perfect deficient abundant abundant deficient |
Thought Process
Use a for loop to iterate over each number as a potential divisor. If the remainder of N divided by the current number (i) is 0, then i is a divisor of N. After summing up the divisors, determine whether it is less than, equal to, or greater than N.
Sample Code-ZeroJudge C203: DPA Numbers I
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for (int i = 0; i<T; i++)
{
int N;
cin >> N;
int count = 0;
for (int j = 1; j<N; j++)
{
if (N % j == 0)
{
count += j;
}
}
if (count == N) cout << "perfect\n";
else if (count > N) cout << "abundant\n";
else cout << "deficient\n";
}
}
//ZeroJudge C203
//Dr. SeanXD