文章目录

Question

一个整数,除了本身以外的其他所有约数的和如果等于该数,那么我们就称这个整数为完全数。

例如,6 就是一个完全数,因为它的除了本身以外的其他约数的和为 1+2+3=6。

现在,给定你 N 个整数,请你依次判断这些数是否是完全数。

输入格式
第一行包含整数 N,表示共有 N 个测试用例。

接下来 N 行,每行包含一个需要你进行判断的整数 X。

输出格式
每个测试用例输出一个结果,每个结果占一行。

如果测试数据是完全数,则输出 X is perfect,其中 X 是测试数据。

如果测试数据不是完全数,则输出 X is not perfect,其中 X 是测试数据。

数据范围
1≤N≤100,
1≤X≤108
输入样例:
3
6
5
28
输出样例:
6 is perfect
5 is not perfect
28 is perfect

Ideas

Code

#include <iostream>
#include <cstdio>
#include <cmath>


using namespace std;

int main()
{
int n,val;
int s;
cin >> n;
while (n)
{
n--;
s = 0;
cin >> val;
for (int i = 1; i <= sqrt(val); i++)
{
// 两个约数同时出现
if (val % i == 0)
{
if (i < val) s += i; // 除了本身 所以要限制大小
if (val / i < val && val / i != i) s += val / i;
}
}
if (s == val)
{
printf("%d is perfect\n", val);
}
else
{
printf("%d is not perfect\n", val);
}
}

return 0;
}