Problem B. Perfect Numbers
描述
We consider a positive integer perfect, if and only if it is equal to the sum of its positive divisors
less than itself. For example, 6 is perfect because 6 = 1 + 2 + 3.
Could you write a program to determine if a given number is perfect or not?
输入
The first line of the input is T(1 ≤ T ≤ 100), which stands for the number of test cases you need
to solve.
Each test case contains a line with a positive integer N (2 ≤ N ≤ 10 5 ).
输出
For each test case, print the case number and determine whether or not the number is perfect. If
the number is perfect, display the sum of its positive divisors less than itself. The ordering of the
terms of the sum must be in ascending order. If a number is not perfect, print “Not perfect.”.
输入样例 1
3
6
8
28
输出样例 1
Case 1: 6 = 1 + 2 + 3
Case 2: Not perfect.
Case 3: 28 = 1 + 2 + 4 + 7 + 14
提示
来源
2018上海
题目大意:给出一个正整数,判断它是不是完美数。完美数定义:如果一个正整数N的所有因子之和等于N,那么N就是一个完美数字。
注意一下输出格式之间的空格就行
#include<bits/stdc++.h>
using namespace std;
int a[100000];
int main()
{
int t;
cin>>t;
int num=0;
while(t--)
{
num++;
int n,s=0;
cin>>n;
int i,j;
for(i=1,j=0;i<=n/2;i++)
{
if(n%i==0)
{
a[j]=i;
s=s+i;
j++;
}
}
if(s==n)
{
printf("Case %d: %d = ",num,n);
for(i=0;i<j-1;i++)
printf("%d + ",a[i]);
if(i=j-1)printf("%d\n",a[j-1]);
}
else
printf("Case %d: Not perfect.\n",num);
}
return 0;
}