习题6-3 使用函数输出指定范围内的完数 (20 分)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int factorsum(int number);
void PrintPN(int m, int n);

int main()
{
    int m, n;

    scanf("%d %d", &m, &n);
    if (factorsum(m) == m) printf("%d is a perfect number\n", m);
    if (factorsum(n) == n) printf("%d is a perfect number\n", n);
    PrintPN(m, n);

    system("pause");
    return 0;
}

/* 你的代码将被嵌在这里 */
int factorsum(int number) {
    int sum = 0;
    if (number == 1)
        return 1;
    for (int i = 1; i <= number/2; i++)
        if (number % i == 0)
            sum = sum + i;
    if (sum == number)
        return sum;
    else
        return 0;
}

void PrintPN(int m, int n) {
    int flag = 0;
    for (int i = m; i <= n; i++) {
        if (i == factorsum(i)) {
            flag = 1;
            printf("%d = 1", i);
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    printf(" + %d", j);
                }
            }
            printf("\n");
        }
    }
    if (flag == 0) {
        printf("No perfect number");
    }
}

 

上一篇:Android 耳机插拔流程源码跟踪浅析


下一篇:Vue 路由