11 Ocak 2013 Cuma

完美數


Problem Description
An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Write a function perfect that determines if parameter number is a perfect number.
請勿用暴力破解
ex:
if(input<100)
printf("6 28 is perfect number\n");
Input Format
大於10的整數
Output Format
Print all the perfect numbers with the format “X is perfect number”.
Sample Input
100
Sample Output
6 28 is perfect number


  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. int isperfect(int val);  
  4. int main(int argc, char *argv[]) {  
  5.     int number,i;  
  6.     scanf("%d",&number);  
  7.     for (i=1;i<number;i++){  
  8.         if(isperfect(i))  
  9.         printf("%d ",i);  
  10.           
  11.     }  
  12.     printf("is perfect number\n");  
  13.     system("PAUSE");  
  14.     return 0;  
  15. }  
  16. int isperfect(int val)  
  17. {  
  18.  int i;  
  19.  int sum = 0;  
  20.  for (i = 1; i <= val/2; i++)  
  21.   if(val % i == 0)  
  22.    sum += i;  
  23.   
  24.   
  25.  if(sum == val)  
  26.   return 1;  
  27.  else  
  28.  return 0;  
  29.     
  30. }  


Hiç yorum yok:

Yorum Gönder

Binary Conversion

Problem Description Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary number...