5.从键盘输入10名学生成绩(以空格为间隔),并按学生成绩降序排列,输出学生成绩排行榜到磁盘文件d:\score.txt中保存。(期末真题)
【输入样例】
Please input 10 students’scores:81 73 95 62 86 74 83 66 93 52
【文件输出样例】
The sort is:
95.00
93.00
86.00
83.00
81.00
74.00
73.00
66.00
62.00
52.00
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int scores[MAX_SIZE];
printf("请输入10名学生成绩(以空格为间隔): ");
for (int i = 0; i < MAX_SIZE; i++) {
scanf("%d", &scores[i]);
}
bubbleSort(scores, MAX_SIZE);
FILE *file = fopen("d:\\score.txt", "w");
if (file == NULL) {
printf("无法打开文件\n");
return 0;
}
for (int i = 0; i < MAX_SIZE; i++) {
fprintf(file, "%d\n", scores[i]);
}
fclose(file);
printf("学生成绩排行榜已保存到d:\\score.txt\n");
return 0;
}
标签:arr,int,MAX,11.27,file,scores,SIZE From: https://www.cnblogs.com/lml66/p/17909403.html