其他分享
首页 > 其他分享> > 牛客华为机试HJ56

牛客华为机试HJ56

作者:互联网

原题传送门

1. 问题描述

2. Solution

1、思路分析
遍历,求所有的真因子,对所有真因子求和。
2、代码实现

import sys

if sys.platform != "linux":
    file_in = open("input/HJ56.txt")
    sys.stdin = file_in


def is_perfect_number(n):
    factors = [1]
    i = 2
    while i * i <= n:
        if n % i == 0:
            factors.append(i)
            if n // i == i:
                i += 1
                continue
            factors.append(n // i)
        i += 1
    return sum(factors) == n


def solve(n):
    if n < 3:
        print(0)
        return
    cnt = 0
    for i in range(3, n + 1):
        if is_perfect_number(i):
            cnt += 1
    print(cnt)


for line in sys.stdin:
    n = int(line.strip())
    solve(n)

标签:真因子,原题,传送门,sys,牛客,file,机试,HJ56
来源: https://www.cnblogs.com/junstat/p/16172582.html