格式
输入格式
第一行为n (n大于0不超过20),表示班里的学生数目;
接下来的n行,每行为每个学生的名字和他的成绩, 中间用单个空格隔开。名字只包含字母且长度不超过20,成绩为一个不大于100的非负整数。
输出格式
把成绩单按分数从高到低的顺序进行排序并输出,每行包含名字和分数两项,之间有一个空格。
1.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu{
char name[21];
int g;
};
int main(){
int n;
char name_t[21];
int g_t;
scanf("%d", &n);
struct stu *s;
s = (struct stu*)malloc(sizeof(struct stu)*n);
for (int i = 0; i < n; i++)
scanf("%s%d", &s[i].name, &s[i].g);
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i-1; j++)//对成绩排序
{
if (s[j].g < s[j + 1].g){
strcpy(name_t, s[j].name);
strcpy(s[j].name, s[j + 1].name);
strcpy(s[j + 1].name, name_t);
g_t = s[j].g;
s[j].g = s[j + 1].g;
s[j + 1].g = g_t;
}
}
for (int i = 0; i < n - 1; i++)//对同成绩的按名字排序
for (int j = 0; j < n - i - 1; j++){
if (s[j].g == s[j + 1].g){
if (strcmp(s[j].name, s[j + 1].name)>0){
strcpy(name_t, s[j].name);
strcpy(s[j].name, s[j + 1].name);
strcpy(s[j + 1].name, name_t);
g_t = s[j].g;
s[j].g = s[j + 1].g;
s[j + 1].g = g_t;
}
}
}
for (int i = 0; i < n; i++){
printf("%s %d\n", s[i].name, s[i].g);
}
return 0;
}
strcmp函数:string头文件;
字符串的结束符\0;