close
Content

對一個正整數 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
 
Output
 
Sample Input #1
30
26
28
Sample Output #1
盈數
虧數
完全數
測資資訊:
記憶體限制: 512 MB
公開 測資點#0 (100%): 1.0s , <1M
#include <iostream>
using namespace std;
 
int main() {
    int m;
    while (cin >> m) {
       int total=0;
       for(int i=1;i<m;i++){
       	if(m%i==0){total+=i; }
	   }
	   if(total>m){cout<<"盈數\n";}
	   else if(total==m){cout<<"完全數\n";}
	   else{cout<<"虧數\n";}
	     
    }
    return 0;
}
arrow
arrow
    文章標籤
    高中生程式解題 C++
    全站熱搜
    創作者介紹
    創作者 趴趴熊日常 的頭像
    趴趴熊日常

    資工趴趴熊的小天地

    趴趴熊日常 發表在 痞客邦 留言(0) 人氣()