题解 | 【C语言】#完全数计算#

完全数计算

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

#include <stdio.h>
#include <math.h>
int IsPerfect(int n)
{
    int m = n - 1;
    for(int i = 2; i < sqrt(n); i++)
    {
        if(n % i == 0)
        {
            m -= i;
            m -= n/i;
        }
    }
    if(n / sqrt(n) == 0)
    {
        m -= sqrt(n);
    }
    return m == 0 ? 1 : 0;
}

int main() 
{
    int n = 0;
    scanf("%d", &n);
    int count = 0;
    for(int i = 6; i <= n; i++)
    {
        if(IsPerfect(i))
        {
            count++;
        }
    }
    printf("%d", count);
    return 0;
}

全部评论

相关推荐

球Offer上岸👑:可能是大环境太差了 太卷了 学历也很重要 hc也不是很多 所以很难
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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