题目描述

(Perfect Numbers) An integer is said to be a perfect number if the sum of its divisors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function isPerfect that determines whether parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the divisors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000.


输入格式

The input consists of n (0<n<100) lines.
Each line consists of the upper limit value(range from 2 to 10000).
The last line of input consists of Int(-1).


输出格式


样例数据

输入

10
1000
-1

输出

6 = 1 + 2 + 3

6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248


备注

The main function is provided, you only need to complete the function isPerfect and printSum :

#include <iostream>
using namespace std;

bool isPerfect( unsigned int ); // function prototype
void printSum( unsigned int ); // function prototype

int main()
{
   // cout << "Perfect integers between 1 and 1000:" << endl;
   int maxnum = 0;
   while(true){
      cin>>maxnum;
      if (maxnum == -1)
         break;
      // loop from 2 to maxnum
      for ( unsigned int j = 2; j <= maxnum; ++j )
      {
         // if current integer is perfect 
         if ( isPerfect( j ) ) 
            printSum( j ); // print it as sum of factors
      } // end for
      cout << endl;
   }

} // end main

操作

评测记录

优秀代码

信息

时间限制: 1s
内存限制: 128MB
评测模式: Normal

题解