通讯录排序

https://pintia.cn/problem-sets/12/problems/347

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct friend
 4 {
 5     char name[11];
 6     int birthday;
 7     char phone_number[18];
 8 };
 9 void friends_sort(struct friend *p, int n);
10 int main(void)
11 {
12     int i, n;
13     struct friend *p;
14 
15     scanf("%d", &n);
16     getchar(); //读回车符
17     p = (struct friend *)calloc(n, sizeof(struct friend));
18     for (i = 0; i < n; i++)
19     {
20         scanf("%s", (p + i)->name);
21         scanf("%d", &((p + i)->birthday));
22         getchar();
23         gets((p + i)->phone_number);
24     }
25 
26     friends_sort(p, n);
27 
28     for (i = 0; i < n; i++)
29     {
30         printf("%s %d %s
", (p + i)->name, (p + i)->birthday, (p + i)->phone_number);
31     }
32 
33     return 0;
34 }
35 void friends_sort(struct friend *p, int n)
36 {
37     struct friend temp;
38     int i, k, index;
39     for (i = 0; i < n - 1; i++)
40     {
41         index = i;
42         for (k = i + 1; k < n; k++)
43         {
44             if ((p + k)->birthday < (p + index)->birthday)
45             {
46                 index = k;
47             }
48         }
49         temp = *(p + i);
50         *(p + i) = *(p + index);
51         *(p + index) = temp;
52     }
53 }

 

 
原文地址:https://www.cnblogs.com/2018jason/p/12205547.html