-
题目1196:成绩排序
-
-
代码:#include "stdio.h"
struct student
{
int id;
int score;
};
void sort(struct student stu[],int n)//对学生进行排序
{
struct student temp; //临时存放id和score
int i,j;
for(i=n-1; i>0; i--)
{
for(j=0; j小于i;i++)
{
if(stu[j].score>stu[j+1].score)
{
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
else if(stu[j].score==stu[j+1].score)
{
if(stu[j].id> stu[j + 1].id)
{
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
for(i=0; i<=n-1; i++)
{
printf("%d %d\n",stu[i].id,stu[i].score);
}
}
int main()
{
int n,i=0;
struct student stu[101];
while(scanf("%d",&n)!=EOF) //n代表学生数量
{
for(i=0; i小于n;i++)
{
scanf("%d%d",&stu[i].id,&stu[i].score);
}
sort(stu,n);
}
return 0;
}