【C语言做题系列】Perfection

From the article Number Theory in the 1994 Microsoft Encarta: “If a, b, c are integers such that a = bc, a is called a multiple of b or of c, and b or c is called a divisor or factor of a. If c is not 1/-1, b is called a proper divisor of a. Even integers, which include 0, are multiples of 2, for example, -4, 0, 2, 10; an odd integer is an integer that is not even, for example, -5, 1, 3, 9. A perfect number is a positive integer that is equal to the sum of all its positive, proper divisors; for example, 6, which equals 1 + 2 + 3, and 28, which equals 1 + 2 + 4 + 7 + 14, are perfect numbers. A positive number that is not perfect is imperfect and is deficient or abundant according to whether the sum of its positive, proper divisors is smaller or larger than the number itself. Thus, 9, with proper divisors 1, 3, is deficient; 12, with proper divisors 1, 2, 3, 4, 6, is abundant.”
Given a number, determine if it is perfect, abundant, or deficient.
Input
A list of N positive integers (none greater than 60,000), with 1 < N < 100. A 0 will mark the end of the list.
Output
The first line of output should read PERFECTION OUTPUT. The next N lines of output should list for each input integer whether it is perfect, deficient, or abundant, as shown in the example below. Format counts: the echoed integers should be right justified within the first 5 spaces of the output line, followed by two blank spaces, followed by the description of the integer. The final line of output should read END OF OUTPUT.
Sample Input
15 28 6 56 60000 22 496 0
Sample Output
PERFECTION OUTPUT
15 DEFICIENT
28 PERFECT
6 PERFECT
56 ABUNDANT
60000 ABUNDANT
22 DEFICIENT
496 PERFECT
END OF OUTPUT
题意:
给定一个数,确定它是完美的、丰富的还是不足的。
例如:一个完全数是一个正整数,等于它的所有正的、适当的除数之和;例如,6等于1+2+3,28等于1+2+4+7+14是完全数。一个不完美的正数是不完美的,根据它的正数、真除数之和是否小于或大于这个数本身,它是有缺陷的或丰富的。因此,9加上适当的除数1,3是不足的;12加上适当的除数1,2,3,4,6是充足的。
注意:
1,本题为多个输入,且针对每一个输入的数字都进行判断说明。
2,输入“0”时,结束输入。
3,“PERFECTION OUTPUT”只输出一次。
4,注意输出的格式。
上代码:

#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int flag;                        //此处定义的全局变量flag 方便在主函数里面应用
int judge(int n)                 //编写一个判断该数是完美,缺陷还是丰富
{
    int i;
    int j=0;
    int sum = 0;
    for(i=1; i<=n/2; i++)          //注意此处的i<=n/2;
        if(n%i==0)
        {
            sum+=i;
        }
    if(sum==n)
        flag=1;
    else if(sum>n)
        flag=2;
    else
        flag=3;

}
int main()
{
    int n=1;
    while(scanf("%d",&a[n]),a[n])     //本题的注意点,本题需要一次性输入很多数,输入0时结束输入
    {
        n++;
    }
    printf("PERFECTION OUTPUT\n");     //此处的"PERFECTION OUTPUT\n"只需要输出一遍
    for(int i=1;i<n;i++)               //接下来需要特别针对每个数进行判断说明,故用数组循环表示
    {
            if(judge(a[i])&&flag==1)
        {
            printf("%5d  PERFECT\n", a[i]);         //字符宽度为5,且右对称 后面还需要加两个空格
        }
        if(judge(a[i])&&flag==2)
        {
            printf("%5d  ABUNDANT\n", a[i]);
        }
        if(judge(a[i])&&flag==3)
        {
            printf("%5d  DEFICIENT\n", a[i]);
        }

    }
    printf("END OF OUTPUT\n");                  //在输入“0”后结束输入,都会输出“END OF OUTPUT\n” 否则一直处在循环内不会输出“END OF OUTPUT\n”
    return 0;
}


心得:
本题的输入输出有很大考究,可以多学习学习。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值