C语言求助,第三个数据WA
查看原帖
C语言求助,第三个数据WA
1274445
AlanRosston楼主2024/3/7 12:06

大佬们,第三个数据我已经下载了,在本地跑能出一样的结果,但是提交了却WA了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void buble_sort(int array[], int n)
{
    for(int i = 0; i < n - 1; i++) {
        for(int j = n - 1; j > i; j--) {
            if(array[j] < array[j - 1]) {
                int temp = array[j];
                array[j] = array[j - 1];
                array[j - 1] = temp;
            }
        }
    }
}

void double_buble_sort(double array[], int n)
{
    for(int i = 0; i < n - 1; i++) {
        for(int j = n - 1; j > i; j--) {
            if(array[j] < array[j - 1]) {
                double temp = array[j];
                array[j] = array[j - 1];
                array[j - 1] = temp;
            }
        }
    }
}

int main(void)
{
    int n, m; scanf("%d %d", &n, &m); //n为学生,m为老师
    int ch = getchar(); //消去'\n'
    int **points = (int**)malloc(n * sizeof(int*));

    for(int i = 0; i < n; i++) {
        points[i] = (int*)malloc(m * sizeof(int));
        char str[200];
        fgets(str, sizeof(str), stdin);
        str[strlen(str)] = '\0';

        int count = 0;
        char *token = strtok(str, " ");
        while(token != NULL) {
            points[i][count++] = atoi(token);
            token = strtok(NULL, " ");
            }
        }

    for(int i = 0; i < n; i++) {
        buble_sort(points[i], m);
        points[i][0] = 0;
        points[i][m - 1] = 0;
    }   //去掉最值分

    double *num = (double*)malloc(n * sizeof(double));
    for(int i = 0; i < n; i++) {
        double sum = 0;
        for(int j = 0; j < m; j++) {
            sum += (double)points[i][j];
        }
        num[i] = sum / (m - 2);
    }
    
    double_buble_sort(num, n);
    printf("%.2f", num[n - 1]);

    for(int i = 0; i < n; i++) {
        free(points[i]);
    }
    free(points);

    return 0;
}
2024/3/7 12:06
加载中...