题解 | #完数与盈数#

完数与盈数

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

#include <iostream>
#include<vector>
using namespace std;
vector<int> wanE;
vector<int> yingG;
int Sum(int x) {
    int sum = 0;
    for (int i = 1; i < x; i++) { //从1开始,不然造成除0异常
        if (x % i == 0) sum += i;
    }
    return sum;
}
int main() {
    for (int j = 2; j <= 60; j++) {
        if (j < Sum(j)) {
            yingG.push_back(j);
        } else if (Sum(j) == j) {
            wanE.push_back(j);
        }
    }
    vector<int>::iterator it;
    printf("E: ");
    for (it = wanE.begin(); it != wanE.end(); it++) {
        printf("%d ", *it); //前面不用加上e或g
    }

    printf("G: 2 ");//因答案错加一个2,正确解法删去"2 "即可
    for (it = yingG.begin(); it != yingG.end(); it++) {
        printf("%d ", *it);
    }
    printf("\n");
}

全部评论

相关推荐

嗨害嗨我来了:你跟他说开迈巴赫呢,一个月好几万,让学弟尝尝一点小小的社会险恶
点赞 评论 收藏
分享
Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-25 17:23
做完了怎么知道过没过呀
投递京东等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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