1. 程式人生 > >C刷題記錄-1017

C刷題記錄-1017

amp math.h data span sample ane == can 刷題記錄

題目描述

一個數如果恰好等於不包含它本身所有因子之和,這個數就稱為"完數"。 例如,6的因子為1、2、3,而6=1+2+3,因此6是"完數"。 編程序找出N之內的所有完數,並按下面格式輸出其因子

輸入

N

輸出

? its factors are ? ? ?

樣例輸入

1000

樣例輸出

6 its factors are 1 2 3 
28 its factors are 1 2 4 7 14 
496 its factors are 1 2 4 8 16 31 62 124 248 

 1 #include <stdio.h>
 2 #include <math.h>
 3
4 int main(){ 5 int i,n,j,k,count,factor_sum=0; 6 scanf("%d",&n); 7 for(i = 2; i < n; i++){ 8 int arr[100]={0}; 9 count = 0;factor_sum = 0; 10 for(j = 1;j < i; j++){ 11 if (i % j == 0) 12 { 13 arr[count] = j; 14 count++; 15 factor_sum += j;
16 } 17 } 18 if (factor_sum == i) 19 { 20 printf("%d its factors are ",i); 21 for(k = 0; k < count; k++) 22 { 23 printf("%d ",arr[k]); 24 } 25 printf("\n"); 26 } 27 } 28 return 0; 29 }

C刷題記錄-1017