您的位置:首页 > Web前端

poj解题报告——poj 1528 Perfection

2017-05-20 11:30 453 查看

原题入口

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ㄒ)/~~。

提交结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj icpc ACM 解题报告