题解:UVA13185 DPA Numbers I

UVA13185 DPA Numbers I

基本思路

对于每个 \(n\),枚举 \(n\) 的因数,最后判断因数大小即可。

直接枚举到 \(n-1\) 有点浪费,所以可以只枚举到 \(\sqrt{n}\),加上因数与 \(n\) 除以此因数的商。

注意:最后要减去 \(n\),且 \(n\) 为完全平方数时要减去一个 \(\sqrt{n}\)

代码实现

#include <bits/stdc++.h>
using namespace std;
int t;
void isorisnt(int x){
    int sum = 0;
    for(int i = 1; i * i <= x; i++){
        if(x % i == 0){
            if(x / i == i) sum += i;
            else sum += x / i + i;
        }
    }
    sum -= x;
    if(sum > x) cout << "abundant\n";
    else if(sum == x) cout << "perfect\n";
    else cout << "deficient\n";
}
int main(){
    cin >> t;
    while(t--){
        int a;
        cin >> a;
        isorisnt(a);
    }
    return 0;
}
posted @ 2024-11-23 16:11  KukCair  阅读(23)  评论(0)    收藏  举报