看得懂吗

nit  •  1年前


#include <bits/stdc++.h>
using namespace std;

vector<int> getDivisors(int num) {
    // 获取真因子列表
    vector<int> divisors;
    for (int i = 1; i * i <= num; i++) {
        if (num % i == 0) {
            divisors.push_back(i);
            if (i != num / i) {
                divisors.push_back(num / i);
            }
        }
    }
    return divisors;
}

int countPerfectNumbers(int n, int m) {
    int count = 0;
    for (int num = n; num <= m; num++) {
        vector<int> divisors = getDivisors(num);
        int divisorsSum = 0;
        for (int divisor : divisors) {
            divisorsSum += divisor;
        }
        if (divisorsSum - num == num) {
            count++;
        }
    }
    return count;
}

int main() {
    int n, m;
    cin >> n >> m;

    int result = countPerfectNumbers(n, m);

    cout << result << endl;

    return 0;
}

评论:

请先登录,才能进行评论