题解 | #统计成绩#
统计成绩
https://www.nowcoder.com/practice/cad8d946adf64ab3b17a555d68dc0bba
#include <stdio.h> #include<stdlib.h> void print(float arr[], int org_length, float avg) { printf("%.2f %.2f %.2f", arr[org_length - 1], arr[0], avg); } void swap(float arr[], int i, int j) { float tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } void selectionSort(float arr[], int length) { int orig_length = length; if (NULL == arr || length < 2) { return; } int minIndex = 0; int maxIndex = 0; for (int i = 0; i < length - 1; i++) { minIndex = i; for (int j = i + 1; j < length; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } swap(arr, i, minIndex); } float sum = 0.0; for (int i = 0; i < orig_length; i++) { sum += arr[i]; } float avg = sum / orig_length; print(arr, orig_length, avg); } int main() { float* arr; int n = 0; int i = 0; scanf("%d", &n); arr = (float*)realloc(NULL, n * sizeof(float)); for (i = 0; i < n; i++) { scanf("%f", &arr[i]); } selectionSort(arr, n); arr = NULL; return 0; }