-
题目描述:
-
有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出N个学生排序后的信息。
-
输入:
-
测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。
每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。
-
输出:
-
将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。
然后输出学生信息,按照如下格式:
姓名 年龄 成绩
-
样例输入:
-
3 abc 20 99 bcd 19 97 bed 20 97
-
样例输出:
-
bcd 19 97 bed 20 97 abc 20 99
#include<stdio.h> #include<string.h> struct stu{ char name[10]; int age; int score; }stu; void Bubblesort(struct stu st[],int n) { int swap=1; struct stu temp; int i=n-1,j; while(i>=0&&swap!=0) { swap=0; for(j=0;j<i;j++) { if(st[j].age>st[j+1].age) { temp=st[j+1]; st[j+1]=st[j]; st[j]=temp; swap=1; } } i--; }//按年纪排序一次; swap=1; i=n-1; while(i>=0&&swap!=0) { swap=0; for(j=0;j<i;j++) { if(strcmp(st[j].name,st[j+1].name)>0) { temp=st[j+1]; st[j+1]=st[j]; st[j]=temp; swap=1; } } i--; }//按名字排序一次; swap=1; i=n-1; while(i>0&&swap!=0) { swap=0; for(j=0;j<i;j++) { if(st[j].score>st[j+1].score) { temp=st[j+1]; st[j+1]=st[j]; st[j]=temp; swap=1; } } i--; }//按分数排序一次; } int main() { struct stu st[1000]; int n,i; while(~scanf("%d",&n)) { if(n<1||n>1000) return 0; for(i=0;i<n;i++) scanf("%s%d%d",&st[i].name,&st[i].age,&st[i].score); Bubblesort(st,n); for(i=0;i<n;i++){ printf("%s %d %d",st[i].name,st[i].age,st[i].score); printf("\n"); } } return 0; }
-