给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。
格式
输入格式
第一行为n (n大于0不超过20),表示班里的学生数目;
接下来的n行,每行为每个学生的名字和他的成绩, 中间用单个空格隔开。名字只包含字母且长度不超过20,成绩为一个不大于100的非负整数。
输出格式
把成绩单按分数从高到低的顺序进行排序并输出,每行包含名字和分数两项,之间有一个空格。
样例
输入样例
4
Kitty 80
Han 90
Joey 92
Tim 28
输出样例
Joey 92
Han 90
Kitty 80
Tim 28
#include <stdio.h>
#include <string.h>
struct studen{
char name[25];
int score;
}stu[25];
int main() {
int n;
scanf("%d", &n);
for(int i=0;i<n;i++){
scanf("%s %d", stu[i].name, &stu[i].score);
}
int tmps;
char tmpn[25];
int max;
for(int i=0;i<n-1;i++){
max = i;
for(int j=i+1;j<n;j++){
if(stu[j].score > stu[max].score || (stu[j].score == stu[max].score && strcmp(stu[j].name, stu[max].name) < 0))max=j;
}
if(max!=i){
tmps = stu[max].score;
stu[max].score=stu[i].score;
stu[i].score=tmps;
strcpy(tmpn, stu[max].name);
strcpy(stu[max].name, stu[i].name);
strcpy(stu[i].name, tmpn);
}
}
for(int i=0;i<n;i++){
printf("%s %d\n", stu[i].name, stu[i].score);
}
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
#define maxSize 21
struct student
{
char name[maxSize];
int score;
};
int main()
{
struct student s[maxSize],temp;
int n;
cin>>n;
for(int i=0; i<n; i++)
cin>>s[i].name>>s[i].score;
//冒泡排序
for(int i=0; i<n-1; i++){
for(int j=0; j<n-i-1; j++){
if(s[j].score<s[j+1].score){
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
if(s[j].score==s[j+1].score && strcmp(s[j].name,s[j+1].name)>0){
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
for(int i=0; i<n; i++)
cout<<s[i].name<<" "<<s[i].score<<endl;
return 0;
}
这个?