BUPT-homework/semester1/pset7/7-perfect.c

46 lines
988 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// #include <stdio.h>
//
// //判断一个数是否为完全数的函数
// int isPerfect(int);
//
// //打印完全数的函数
// void printPerfact(int);
//
// int main() {
// int i, a, b, count;
//
// scanf("%d%d", &a, &b);
// count = 0; // a,b两数间完全数的数量初始化为0
// for (i = a; i <= b; i++) {
// if (isPerfect(i)) //如果是完全数
// {
// printPerfact(i); //打印该完全数
// count++; //计数器加1
// }
// }
// printf("The total number is %d.\n", count); //输出a,b两数间完全数的数量
// return 0;
// }
//判断一个数是否为完全数的函数
int isPerfect(int n) {
int sum = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
sum += i;
}
}
return (sum == n) ? 1 : 0;
}
//打印完全数的函数
void printPerfact(int n) {
printf("%i=1", n);
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
printf("+%i", i);
}
}
printf("\n");
}