ZeroJudge C203: DPA Numbers I

題目敘述

每筆測資第一行有一個正整數T,接下來會有T行,每行會有一個正整數N。要求輸出N是Perfect、Deficient、或Abundant。

Perfect:所有因數的和 (包含1,不包含自己) 等於自己

Deficient:所有因數的和 (包含1,不包含自己) 小於自己

Abundant:所有因數的和 (包含1,不包含自己) 大於自己


範例輸入 #1

10

5

6

16

18

21

28

29

30

40

43


範例輸出 #1

deficient

perfect

deficient

abundant

deficient

perfect

deficient

abundant

abundant

deficient


解題思路

使用For迴圈判斷每一個數字的因數,如果N除以目前迴圈跑到的數字 (i) 之餘數等於0,i 就是N的因數。進行加總之後判斷是小於、等於、還是大於。

解題程式碼如下 (僅供參考):

#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 M933: 邏輯電路

ZeroJudge A148: You Cannot Pass?!

ZeroJudge A263: 日期差幾天