C语言 · 学生信息(P1102)

算法训练 P1102  
时间限制:1.0s   内存限制:256.0MB
    
  定义一个学生结构体类型student,包括4个字段,姓名、性别、年龄和成绩。然后在主函数中定义一个结构体数组(长度不超过1000),并输入每个元素的值,程序使用冒泡排序法将学生按照成绩从小到大的顺序排序,然后输出排序的结果。
  输入格式:第一行是一个整数N(N<1000),表示元素个数;接下来N行每行描述一个元素,姓名、性别都是长度不超过20的字符串,年龄和成绩都是整型。
  输出格式:按成绩从小到大输出所有元素,若多个学生成绩相同则成绩相同的同学之间保留原来的输入顺序。
输入:
  3
  Alice female 18 98
  Bob male 19 90
  Miller male 17 92

输出:
  Bob male 19 90
  Miller male 17 92
  Alice female 18 98
 
作者注释:自从学会了用结构体排序的方法,这种题目都蛮容易搞定的。
代码一:
 1 #include<stdio.h>
 2 #include<string.h> 
 3 #include<stdlib.h>
 4 #include<math.h>
 5 #include<ctype.h>
 6 /*定义一个结构体*/
 7 typedef struct Stu{
 8     char name[30];
 9     char sex[20];
10     int age;
11     int score;
12 }stu;
13 /*    定义排序(回调)函数cmp: 
14         返回类型必须是int;
15         两个参数的类型必须都是const void *;
16         如果是升序,那么就是如果a比b大返回一个正值,小则负值,相等返回0;
17 */ 
18 int cmp(const void *a,const void *b){
19     /* *(stu*)a是因为:a是个void *类型,要先
20     用(stu*)将它转成stu*类型,然后再用*取值,
21     变成stu类型,才能比较大小。*/
22     stu c=*(stu*)a;
23     stu d=*(stu*)b;
24     //按成绩升序排序 
25     return c.score-d.score;
26 }
27 main(){
28     int n;
29     stu sz[100];
30     scanf("%d",&n);
31     for(int i=0;i<n;i++){
32         scanf("%s %s %d %d",&sz[i].name,&sz[i].sex,&sz[i].age,&sz[i].score);
33     }
34     /*
35     qsort函数参数: 
36         1 待排序数组首地址;
37         2 数组中待排序元素数量;
38         3 各元素的占用空间大小,推荐使用sizeof(s[0])这样,特别是对结构体 ; 
39         4 指向函数的指针,用于确定排序的顺序.
40     注意:如果要对数组进行部分排序,比如对一个s[n]的数组排列其从s[i]开始的m个元素,只需要
41 在第一个和第二个参数上进行一些修改:qsort(&s[i],m,sizeof(s[i]),cmp);
42     */
43     qsort(sz,n,sizeof(sz[0]),cmp);
44     for(int i=0;i<n;i++){
45         printf("%s %s %d %d
",sz[i].name,sz[i].sex,sz[i].age,sz[i].score);
46     }
47 }

代码二:

 1 #include<stdio.h>
 2 #include<string.h> 
 3 //结构体 
 4 struct student
 5 {
 6     char name[20];
 7     char sex[10];
 8     int age;
 9     int score;
10 };
11 int main()
12 {
13     int n;
14     scanf("%d",&n);
15     student stu[1000];
16     for(int i=0;i<n;i++)
17     {
18         scanf("%s %s %d %d",&stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score);
19     }
20     for(int i=0;i<n-1;i++)
21     {
22         int j=i+1;
23         if(stu[i].score>stu[j].score)
24         {
25             char str[20];
26             strcpy(str,stu[i].name);
27             strcpy(stu[i].name,stu[j].name);
28             strcpy(stu[j].name,str);
29             
30             strcpy(str,stu[i].sex);
31             strcpy(stu[i].sex,stu[j].sex);
32             strcpy(stu[j].sex,str);
33             
34             int t;
35             t=stu[i].score;
36             stu[i].score=stu[j].score;
37             stu[j].score=t;
38             
39             t=stu[i].age;
40             stu[i].age=stu[j].age;
41             stu[j].age=t;
42             i=-1;//如果进行了排序那么前面的可能还需排序。 
43         }
44     }
45     for(int i=0;i<n;i++)
46     {
47         printf("%s %s %d %d
",stu[i].name,stu[i].sex,stu[i].age,stu[i].score);
48     }
49     return 0;
50 }
 
 
原文地址:https://www.cnblogs.com/panweiwei/p/6498888.html