题解 | #完全数计算#

完全数计算

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

  1. 注意真因子的求法。以及最小完数是6,所以从6开始。
#include<bits/stdc++.h>

using namespace std;

int main(){

    int n;

    while(cin>>n){
        int count = 0;

        for(int i = 6; i<=n;i++){
            int temp = 0;
            for(int j = 1; j<i;j++){
                if(i%j==0){//找到自己的真因子(从一开始算)
                    temp+= j;
                }
            }

            if(temp==i){//如果是完数
                count++;
            }


        }


        cout<<count<<endl;


    }



    return 0;
}
算法解析 文章被收录于专栏

这里主要是算法岗的自我思路总结

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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