牛客网 Perfect Numbers(完美数)

链接:https://www.nowcoder.com/acm/contest/163/B
来源:牛客网
题目描述
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 ≤ 105).
输出描述:
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
输出
复制
Case 1: 6 = 1 + 2 + 3
Case 2: Not perfect.
Case 3: 28 = 1 + 2 + 4 + 7 + 14

#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL del(LL x)
{
    LL sum=0;
    while(x>=10)
    {
        sum+=x%10;
        x/=10;
    }
    sum=sum+x;
    return sum;
}
int main()
{
    int t; cin>>t;
    int k=0;
    while(t--)
    {
        k++;
        LL n;cin>>n;
        LL cnt=0;
        for(LL i=1;i<=n;i++)
        {
            LL sum=0;
            sum=del(i);
            if(i%sum==0){
                cnt++;
            }
        }
        printf("Case %d: %lld\n",k,cnt);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a17865569022/article/details/81434989