對一個正整數 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,然後印出它是盈數、虧數還是完全數。
輸入說明 :
none
輸出說明 :
none
範例輸入 :
30
26
28
範例輸出 :
盈數
虧數
完全數
程式碼 :
#include <stdio.h>
#include <math.h>
int main()
{
int i,n,total,temp;
while(scanf("%d",&n)==1)
{
temp = sqrt(n) + 1;
total = 1;
for(i=2; i<temp; i++)
if(n%i==0)
if(i*i == n)
total = total + i;
else
total = total + i + n/i;
if(total > n)
printf("盈數\n");
else if(total == n)
printf("完全數\n");
else
printf("虧數\n");
}
return 0;
}
http://zerojudge.tw/ShowProblem?problemid=d010
沒有留言:
張貼留言