题解 | #统计成绩#
统计成绩
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 end=length-1; end>0;end--) { for(int i=0;i<end;i++) { if(arr[i]>arr[i+1]) { swap(arr,i,i+1); } } }*/ /*选择排序 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); } *///插入排序 for(int i=0;i<length;i++) { for(int j=i-1;j>=0 && arr[j]> arr[j+1] ;j--) { swap(arr,j,j+1); } } 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; scanf("%d", &n); arr = (float*)realloc(NULL, n * sizeof(float)); for (int i = 0; i < n; i++) { scanf("%f", &arr[i]); } selectionSort(arr, n); arr = NULL; return 0; }