1.用户从屏幕输入每个学生的成绩,格式如下:学号 成绩 换行 。学号为整数。范围为1-50,成绩为浮点数。
2.如果用户的学号输入有重复,表明前面输入有误,用新的成绩覆盖之。
3.用户最多只能输入50个学生的成绩,如果已经输入了50个,则表明成绩输入完毕。
4.如果用户的学号输入-1,则表明成绩输入完毕。
5.程序将所以学生的成绩进行排序后,按照成绩的高低进行打印,打印格式如下:学号 成绩 换行
举例
1 50
2 60
3 55
2 80
-1
Result:
2 80
3 55
1 50
最后是chatgpt写的,满足要求的,附上源码
#include <stdio.h>
#define MAX_STUDENTS 50
struct Student
{
int id;
float score;
};
void swap(struct Student *a, struct Student *b)
{
struct Student temp = *a;
*a = *b;
*b = temp;
}
void sortStudents(struct Student students[], int count)
{
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
swap(&students[j], &students[j + 1]);
}
}
}
}
int main()
{
struct Student students[MAX_STUDENTS];
int count = 0;
printf("Enter student grades (id score) or -1 to finish:\n");
while (1) {
int id;
float score;
scanf("%d", &id);
if (id == -1) {
break;
}
scanf("%f", &score);
// Check for duplicate id
int duplicateIndex = -1;
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
duplicateIndex = i;
break;
}
}
if (duplicateIndex != -1) {
students[duplicateIndex].score = score;
} else {
students[count].id = id;
students[count].score = score;
count++;
}
if (count >= MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
break;
}
}
sortStudents(students, count);
printf("Result:\n");
for (int i = 0; i < count; i++) {
printf("%d\t%.2f\n", students[i].id, students[i].score);
}
return 0;
}
运行结果如下:
叫chatgpt用链表实现,也满足了要求
#include <stdio.h>
#include <stdlib.h>
struct Node {
int id;
float score;
struct Node* next;
};
struct Node* createNode(int id, float score) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->id = id;
newNode->score = score;
newNode->next = NULL;
return newNode;
}
struct Node* insertOrUpdate(struct Node* head, int id, float score) {
struct Node* newNode = createNode(id, score);
if (head == NULL) {
return newNode;
}
if (head->id == id) {
head->score = score;
free(newNode);
return head;
}
struct Node* curr = head;
struct Node* prev = NULL;
while (curr != NULL) {
if (curr->id == id) {
prev->next = newNode;
newNode->next = curr->next;
free(curr);
return head;
}
prev = curr;
curr = curr->next;
}
prev->next = newNode;
return head;
}
void swap(struct Node* a, struct Node* b) {
int tempId = a->id;
float tempScore = a->score;
a->id = b->id;
a->score = b->score;
b->id = tempId;
b->score = tempScore;
}
void sortStudents(struct Node* head) {
int swapped;
struct Node* ptr1;
struct Node* lptr = NULL;
if (head == NULL) {
return;
}
do {
swapped = 0;
ptr1 = head;
while (ptr1->next != lptr) {
if (ptr1->score < ptr1->next->score) {
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
void printStudents(struct Node* head) {
printf("Result:\n");
while (head != NULL) {
printf("%d\t%.2f\n", head->id, head->score);
head = head->next;
}
}
void freeList(struct Node* head) {
struct Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
int main() {
struct Node* head = NULL;
int count = 0;
printf("Enter student grades (id score) or -1 to finish:\n");
while (1) {
int id;
float score;
scanf("%d", &id);
if (id == -1) {
break;
}
scanf("%f", &score);
if (id < 1 || id > 50) {
printf("Invalid student ID. Please enter a valid ID (1-50).\n");
continue;
}
head = insertOrUpdate(head, id, score);
count++;
if (count >= 50) {
printf("Maximum number of students reached.\n");
break;
}
}
sortStudents(head);
printStudents(head);
freeList(head);
return 0;
}
运行结果如下: