Problem Description

對一個正整數 N 而言,將它除了本身以外所有的因數加起來的總和為 S,如果 S>N,則 N 為盈數,如果 S<N,則 N 為虧數,而如果 S=N,則 N 為完全數(Perfect Number)。例如 10 的因數有 1、2、5、10,1+2+5=8<10,因此10 為虧數,而 12 的因數有 1、2、3、4、6、12,1+2+3+4+6=16>12,因此 12 為盈數。至於 6 的因數有 1、2、3、6,1+2+3=6,所以 6 是完全數(它也是第一個完全數)。

現在請你寫一個程式,輸入一個正整數 N,然後印出它是盈數、虧數還是完全數。

Input Format

none

Output Format

none

Sample Input

30
26
28

Sample Output

盈數
虧數
完全數


-----*Problem from【ZeroJudge, An Online Judge System For Beginners

 

My Answer

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
#include<iostream>
using namespace std;
int main(void)
{
   int n;
   while(cin>>n){
      int sum=1;
      for(int i=2; i<n; ++i){
         if(n%i==0) sum+=i;
      }
      if(sum>n) cout<<"盈數"<<endl;
      else if(sum<n) cout<<"虧數"<<endl;
      else cout<<"完全數"<<endl;
   }
   return 0;
}


 


arrow
arrow
    全站熱搜

    兔老大 發表在 痞客邦 留言(0) 人氣()