OJ 1334 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

看到这一大堆英文让人瞬间觉得挠头,然而仔细观察却发现其实这就是一个完数题,即判断输入的N是否为完数如果是则输出按格式他们的因数

#include <iostream>
#include <stdio.h>

using namespace std;
int a[10]= {};
void WANSHU()
{
    int sum,j=0;
    for(int i=1; i<=10000; i++)
    {
        sum=0;
        if(i==1)
        {
            a[j]=i;
            j++;
        }
        for(int j=1; j<i; j++)
        {
            if(i%j==0)
                sum+=j;
        }
        if(sum==i)
        {
            a[j]=sum;
            j++;
        }
    }
}
int main()
{
    WANSHU();
    int T,n,jug;
    while(cin>>T)
    {
        for(int j=1; j<=T; j++)
        {
            int b[36]= {0};
            jug=0;
            cin>>n;
            for(int i=0; i<20&&a[i]!=0; i++)
                if(a[i]==n)
                {
                    jug=1;
                    break;

                }
            if(jug==1)
            {
                printf("Case %d: %d = ",j,n);
                int c=0,i;
                for(int i=1; i<n; i++)
                    if(n%i==0)
                    {
                        b[c]=i;
                        c++;
                    }
                for(i=0; i<c-1; i++)
                    printf("%d + ",b[i]);
                printf("%d\n",b[c-1]);
            }
            else
                printf("Case %d: Not perfect.\n",j);


        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值