完全数计算

iNOC产品部--完全数计算_牛客网

https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84?tpId=37&tqId=21279&rp=0&ru=%2Fta%2Fhuawei&qru=%2Fta%2Fhuawei%2Fquestion-ranking&tPage=3

#include <iostream>
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++){//注意j<n,因为求和时不算这个数的自身
                if(i%j==0)
                    temp+=j;
            }
            if(temp==i)
                count++;
        }
        cout << count << endl;
    }
    return 0;
}
#一样的解法,python的劣势就凸显出来了,复杂度过大;看了其他人的解法......只能说很骚,但没有意义
while True:
    try:
        n=int(input())
        count=0
        for i in range(6,n+1):
            temp=0
            for j in range(1,i):
                if i%j==0:
                    temp+=j
            if temp==i:
                count+=1
        print(count)
            
    except:
        break


全部评论

相关推荐

10-09 16:12
门头沟学院 Java
帅宇殿下:佬,简历写的什么
点赞 评论 收藏
分享
评论
9
1
分享

创作者周榜

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