PTA上面的一个练习题说我格式错误

wutianlong220
  • 356

题目出处:
https://pintia.cn/problem-set...

我的答案如下:

int factorsum( int number ) {
    int sum = 0;
    for (int i = 1; i < number; i++) {
        if (number%i == 0) {
            sum = sum + i;
        }
    }
    return sum;
}

void PrintPN( int m, int n ) {
    
    int count = 0;
    
    while (m <= n) {
        if (factorsum(m) == m) {
            count++;
            //6 = 1 + 2 + 3
            //28 = 1 + 2 + 4 + 7 + 14
            printf("%d =",m);
            for (int i = 1; i < m; i++) {
                
                if (i == 1) {
                    printf(" %d ",i);
                } else if (m%i == 0) {
                    printf("+ %d ",i);
                }
            }
            printf("\n");
        }
        m++;
    }
    if (count == 0) {
        printf("No perfect number\n");
    }
}

题目检测答案如下:

image.png

我的完整代码如下:


#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);

    return 0;
}

int factorsum( int number ) {
    int sum = 0;
    for (int i = 1; i < number; i++) {
        if (number%i == 0) {
            sum = sum + i;
        }
    }
    return sum;
}

void PrintPN( int m, int n ) {
    
    int count = 0;
    
    while (m <= n) {
        if (factorsum(m) == m) {
            count++;
            //6 = 1 + 2 + 3
            //28 = 1 + 2 + 4 + 7 + 14
            printf("%d =",m);
            for (int i = 1; i < m; i++) {
                
                if (i == 1) {
                    printf(" %d ",i);
                } else if (m%i == 0) {
                    printf("+ %d ",i);
                }
            }
            printf("\n");
        }
        m++;
    }
    if (count == 0) {
        printf("No perfect number\n");
    }
}

不知道为什么,明明打印出来都是对的,但是题目说我是错的,有没有朋友可以指导下的?

评论
阅读 200
1 个回答

检查以下有没有打印多余的空格,回车,特别是行末是否有空格

撰写回答

登录后参与交流、获取后续更新提醒

宣传栏