题目1196:成绩排序

来源:互联网 发布:保密式网络机柜 编辑:程序博客网 时间:2024/05/30 23:53
题目1196:成绩排序
 

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5219

解决:1802

题目描述:

用一维数组存储学号和成绩,然后,按成绩排序输出。

输入:

输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。

输出:

按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。

样例输入
3
1 90
2 87
3 92
样例输出:
2 87
1 90
3 92
来源:
2009年华中科技大学计算机研究生机试真题
代码:#include "stdio.h"
struct student
{
    intid;
    intscore;
};
void sort(struct student stu[],intn)   //对学生进行排序
{
    structstudenttemp;    //临时存放id和score
    inti,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()
{
    intn,i=0;
    structstudent 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);
    }
    return0;
}
0 0
原创粉丝点击