题解 | #完全数计算#

完全数计算

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

#include <stdio.h>

static int PerfectNum(int n)    //计算完全数
{
    int count = 0;
    for(int i = 1; i < n; i++)    //遍历
    {
        int base = i;
        int num = 0;
        for(int j = 1; j < i; j++)    //除了自身之外的数,能整除都是因子
        {
            if(i % j == 0)
            {
                num += j;
            }
        }
        if(num == i)    //判断所有因子之和
        {
            count++;
        }
    }
    return count;
}

int main()
{
    int n = 0;
    scanf("%d", &n);
    int ret = PerfectNum(n);
    if(ret >= 0)
    {
        printf("%d\n", ret);
    }
    return 0;
}
全部评论

相关推荐

05-20 21:57
已编辑
门头沟学院 Java
喜欢吃卤蛋的悲伤蛙在...:建信融通没消息吧,我2说有实习挂简历不理了
点赞 评论 收藏
分享
牛客84809583...:举报了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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