题解 | #完全数计算#

完全数计算

https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84

#include <iostream>

using namespace std;

bool is_perfect_num(int& num) {
    int sum = 0;//用于进行约数加和
    for (int i = 1; i < num; i++) {
        if (num % i == 0) {
            sum += i;
        }
    }
    if (sum == num) {
        return true;
    } else {
        return false;
    }

}
int count_perfect_num(int& n) {
    int count = 0; //对完美数进行加和
    for (int i = 1; i < n; i++) {
        if (is_perfect_num(i)) {
            //cout << "perfect_num =" << i << endl;
            count++;
        }
    }
    return count;
}
int main() {
    int n;
    cin >> n;
    cout << count_perfect_num(n) << endl;
    return 0;
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务