P1004 standings

  This question is relatively simple, probably investigate the use of the structure, nor the other, not even the sort of necessary. Let's look at the topic

  

  Similarly, demand is very clear, to output the highest and lowest scores of people to learn the name of number. Such sub-processing program that is quite clear, then input into the first data row qsort a sequence, the output of the first and last array, bin. But we have to know how to sort of use in stdlib to either write your own sorting algorithm. Qsort sorting algorithms and other blog I have described in the article, not repeat them here.

code show as below:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define MAX_SIZE 12
 4 #define MAX_STD_SEIZE 150
 5 
 6 typedef struct data
 7 {
 8     char name[MAX_SIZE];
 9     int ID[MAX_SIZE];
10     int Mark;
11 } STD;
12 
13 int cmp(const void *a, const void *b);
14 
15 int main()
16 {
17     int n, i = 0;
18     int Min_Mark = 100, Max_Mark = 0;
19     int Min_Pos, Max_Pos;
20     scanf("%d", &n);
21     STD Student[MAX_STD_SEIZE];
22 
23     for (int i = 0; i < n; i++)
24     {
25         scanf("%s %s %d", Student[i].name, Student[i].ID, &Student[i].Mark);
26         getchar();
27     }
28     qsort(Student, n, sizeof(Student[0]), cmp);
29     printf("%s %s\n", Student[0].name, Student[0].ID);
30     printf("%s %s\n", Student[n-1].name, Student[n-1].ID);
31 
32     return 0;
33 }
34 
35 int cmp(const void *a, const void *b)
36 {
37     return (* (STD *) a).Mark > (* (STD *) b).Mark ? -1 : 1;
38 }
Built-in fast row

 

  In fact, this question does not have to sort, we can define a maximum of four variables are stored at the time of input, minimum, maximum index, the minimum value of the index. Enter finished on the election of the highest score lowest score, time complexity is O (n), is more easy than O (nlogn) fast row.

  code show as below

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define MAX_SIZE 12
 4 #define MAX_STD_SEIZE 150
 5 
 6 typedef struct
 7 {
 8     char name[MAX_SIZE];
 9     int ID[MAX_SIZE];
10     int Mark;
11 } STD;
12 
13 int main()
14 {
15     int n, i = 0;
16     int Min_Mark = 100, Max_Mark = 0;
17     int Min_Pos, Max_Pos;
18     scanf("%d", &n);
19     STD Student[MAX_STD_SEIZE];
20 
21     while (n--)
22     {
23 
24         scanf("%s %s %d", Student[i].name, Student[i].ID, &Student[i].Mark);
25         if (Min_Mark > Student[i].Mark)
26         {
27             Min_Pos = i;
28             Min_Mark = Student[i].Mark;
29         }
30         if (Max_Mark < Student[i].Mark)
31         {
32             Max_Pos = i;
33             Max_Mark = Student[i].Mark;
34         }
35         i++;
36         getchar();
37     }
38 
39     printf("%s %s\n", Student[Max_Pos].name, Student[Max_Pos].ID);
40     printf("%s %s\n", Student[Min_Pos].name, Student[Min_Pos].ID);
41 
42     return 0;
43 }
View Code

Well, then again 

     - Anterior and more difficult, gentlemen encourage each other!

Guess you like

Origin www.cnblogs.com/daker-code/p/11618622.html