poj解题报告——poj 1528 Perfection

原题入口

poj 1528 Perfection

题目描述

Perfection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12561 Accepted: 5870

Description
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

Source
Mid-Atlantic 1996

解题代码

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    cout << "PERFECTION OUTPUT" << endl;
    int nNum;
    while((cin >> nNum) && nNum)
    {
        cout << setw(5) << setfill(' ') << right << nNum << "  ";// 先输出数字
        const int nLoopLimit = (int)sqrt((double)nNum);
        int nSum = 0;

        if (nNum == 1)
        {
            cout << "DEFICIENT" << endl;
            continue;
        }

        for (int nDivisor = 1; nDivisor <= nLoopLimit; ++nDivisor)
        {
            if (nNum % nDivisor != 0)
                continue;

            nSum += nDivisor;

            if (nDivisor * nDivisor != nNum && nDivisor != 1)
                nSum += (nNum / nDivisor);

            if (nSum > nNum)
                break;
        }

        if (nSum == nNum)
            cout << "PERFECT" << endl;
        else if (nSum < nNum)
            cout << "DEFICIENT" << endl;
        else
            cout << "ABUNDANT" << endl;
    }
    cout << "END OF OUTPUT" << endl;

    return 0;
}

解题过程

  • 题意:题目要求找到完美数字,开头啰里啰嗦的讲了一大堆数论里面的知识,然后最后给出了完美数字的解释,总结来说就是一个数字的因子(不包括自身)相加如果等于这个数本身,那么这个数字就被称作完美数字,如果最后的和小于数字本身,那么称为“DEFICIENT”,如果和大于数字本身,称为“ABUNDANT”。

  • 思路:这个问题只需要简单遍历就好,注意范围的选取会让你的速度加快

  • 槽点:今天的问题同样槽点不断,在AC之前我贡献了一次WA,3次PE,我也是醉了:

    • 先说加快计算时间的技巧,还是减少遍历的范围,既然是求因子相加,那么遍历到sqrt(n)就可以,对于因子k的另一个因子就是n/k。
    • 说说我伤心的WA,就是因为没有考虑到n==1时的情况,这个时候属于”DEFICIENT”,还有些文章是误导的,需要注意
    • 再说说那3次PE,这是我第一次提交得到PE的结果,还在这里贡献了3次,其实第一次读这个题的时候我就有心里准备,这个提的输出格式太严了
    • 第一次我认为是最后的”END OF OUTPUT”最后不应该有回车换行,去掉回车后提交,PE。
    • 第二次我认为不应该把”PERFECTION OUTPUT”放在一开始输出,应该放在读取数据之后输出,改后提交,PE。
    • 第三次我也不知道是咋回事,PE,好伤心。
    • 最后问题出在输出的数字后面应该有两个空格,而不是一个,/(ㄒoㄒ)/~~。

提交结果

poj1528

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AlbertS

常来“玩”啊~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值