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
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
- #include <stdio.h>
- #include <stdlib.h>
- int isperfect(int val);
- int main(int argc, char *argv[]) {
- int number,i;
- scanf("%d",&number);
- for (i=1;i<number;i++){
- if(isperfect(i))
- printf("%d ",i);
- }
- printf("is perfect number\n");
- system("PAUSE");
- return 0;
- }
- int isperfect(int val)
- {
- int i;
- int sum = 0;
- for (i = 1; i <= val/2; i++)
- if(val % i == 0)
- sum += i;
- if(sum == val)
- return 1;
- else
- return 0;
- }
Hiç yorum yok:
Yorum Gönder