题解 | #完数VS盈数#

完数VS盈数

https://www.nowcoder.com/practice/ccc3d1e78014486fb7eed3c50e05c99d

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

std::vector<int> yinzi;
std::vector<int> wan;
std::vector<int> ying;

void getYinzi(int x) {
    yinzi.push_back(1);
    if (x/sqrt(x) == sqrt(x) && floor(sqrt(x))*floor(sqrt(x)) == x) {
        yinzi.push_back(sqrt(x));
    }
    for(int i = 2; i < sqrt(x); i++) {
        if (x%i == 0) {
            yinzi.push_back(i);
            yinzi.push_back((x/i));
        }
    }
    return;
}

bool isWan(int x) {
    int sum = 0;
    for(auto & number : yinzi) {
        sum += number;
    }
    return sum == x;
}
bool isYing(int x) {
    int sum = 0;
    for(auto & number : yinzi) {
        sum += number;
    }
    return sum > x;
}

int main() {
    for(int i = 2; i <= 60; i++){
        getYinzi(i);
        if(isWan(i)) {
            wan.push_back(i);
        }
        if(isYing(i)) {
            ying.push_back(i);
        }
        yinzi.clear();
    }
    std::cout << "E:";
    for(auto x : wan) {
        std::cout << ' ' << x;
    }
    std::cout << endl;
    std::cout << "G:";
    for(auto y : ying) {
        std::cout << ' ' << y;
    }
    std::cout << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-15 13:42
四川大学 Java
蹲蹲offerrr:快投吧,有点晚现在
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务