AcWing 725. 完全数
原题链接
中等
作者:
wasd233
,
2024-02-29 09:41:17
,
所有人可见
,
阅读 24
C++
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n; cin >> n;
for (int i = 0; i < n; i ++)
{
int x, s = 0; cin >> x;
for (int j = 1; j*j <= x; j ++)
{
if (x % j == 0)
{
if (j < x) s += j;
if (j != x && x/j <x) s += x/j;
}
}
if (s == x) printf("%d is perfect\n", x);
else printf("%d is not perfect\n", x);
}
return 0;
}
Python
import math
n = int(input())
for _ in range(n):
x = int(input())
res = 0
for i in range(1, int(math.sqrt(x))+1):
if x % i == 0:
if i < x:
res += i
if i != x / i and x / i < x:
res += x / i
if res == x:
print(f"{x} is perfect")
else:
print(f"{x} is not perfect")