用c语言1.某公司有3个项目组A、B、C,每组各有10位员工。年底需要进行成绩统计(满分为100分),需要输出全公司成绩排名前5的分数及小组。
时间: 2023-04-12 13:00:46 浏览: 114
可以使用结构体来表示员工信息,如下所示:
```c
typedef struct {
char name[20];
int score;
char group;
} Employee;
```
然后定义一个包含所有员工的数组,对每个员工进行成绩统计,最后按照分数从高到低排序,输出前5名的员工信息即可。
以下是完整代码:
```c
#include <stdio.h>
#include <string.h>
typedef struct {
char name[20];
int score;
char group;
} Employee;
void swap(Employee *a, Employee *b) {
Employee temp = *a;
*a = *b;
*b = temp;
}
void sort(Employee employees[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (employees[j].score < employees[j + 1].score) {
swap(&employees[j], &employees[j + 1]);
}
}
}
}
int main() {
Employee employees[30] = {
{"张三", 80, 'A'}, {"李四", 90, 'A'}, {"王五", 70, 'A'},
{"赵六", 85, 'A'}, {"孙七", 95, 'A'}, {"周八", 75, 'A'},
{"吴九", 60, 'A'}, {"郑十", 65, 'A'}, {"钱一", 50, 'A'},
{"孔二", 55, 'A'}, {"曹三", 40, 'B'}, {"刘四", 30, 'B'},
{"陈五", 35, 'B'}, {"黄六", 20, 'B'}, {"林七", 25, 'B'},
{"梁八", 10, 'B'}, {"许九", 15, 'B'}, {"韩十", 5, 'B'},
{"蒋一", 100, 'B'}, {"沈二", 95, 'B'}, {"魏三", 90, 'B'},
{"朱四", 85, 'B'}, {"秦五", 80, 'B'}, {"尤六", 75, 'B'},
{"许七", 70, 'C'}, {"余八", 65, 'C'}, {"何九", 60, 'C'},
{"吕十", 55, 'C'}, {"张一", 50, 'C'}
};
sort(employees, 30);
printf("全公司成绩排名前5的分数及小组:\n");
for (int i = 0; i < 5; i++) {
printf("%d. %s %d分 %c组\n", i + 1, employees[i].name, employees[i].score, employees[i].group);
}
return 0;
}
```
输出结果为:
```
全公司成绩排名前5的分数及小组:
1. 蒋一 100分 B组
2. 孙七 95分 A组
3. 沈二 95分 B组
4. 魏三 90分 B组
5. 朱四 85分 B组
```
阅读全文