题解 | #统计成绩#

统计成绩

https://www.nowcoder.com/practice/cad8d946adf64ab3b17a555d68dc0bba

#include <stdio.h>
void bubble_sort(double *arr, int len);
int main(void)
{
    int subject = 0;
    double sum = 0;
    scanf("%d", &subject);         // The length of array
    double subject_score[subject]; // Initialize the array

    for (int i = 0; i < subject; i++)
    {
        scanf("%lf", &subject_score[i]);
        sum += subject_score[i];
    }

    bubble_sort(subject_score, subject);
    printf("%.2lf %.2lf %.2lf\n", subject_score[0], subject_score[subject - 1], sum / subject);
    return 0;
}

// @brief bubble sort(from min to max)
void bubble_sort(double *arr, int len)
{
    double temp_val = 0;
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = 0; j < len - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                temp_val = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp_val;
            }
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务