标签:qsort 算法 include 排序 代码 记录 16px 贪心算法 compare
1 00:12-12:12 2 00:12-12:12 14:00-12:00
1 1
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define max(x,y) (x>y?x:y) 4 #define min(x,y) (x<y?x:y) 5 typedef struct time { 6 int start; 7 int end; 8 }Time; 9 int compare(const void * p1, const void * p2) 10 { 11 return ((Time *)p1)->end > ((Time *)p2)->end; 12 } 13 14 int main(void) 15 { 16 int n; 17 while (~scanf("%d", &n)) 18 { 19 int i, hour_start, min_start, hour_end, min_end; 20 int pre_time, latter_time, count; 21 Time table[105]; 22 for (i = 0; i < n; i++) 23 { 24 scanf("%d:%d-%d:%d", &hour_start, &min_start, 25 &hour_end, &min_end); 26 pre_time = hour_start * 60 + min_start; 27 latter_time = hour_end * 60 + min_end; 28 table[i].start = min(pre_time, latter_time); 29 table[i].end = max(pre_time, latter_time); 30 } 31 qsort(table, n, sizeof(table[0]), compare); 32 int last = table[0].end; 33 count = 1; 34 for (i = 1; i < n; i++) 35 { 36 if (table[i].start > last) 37 { 38 count++; 39 last = table[i].end; 40 } 41 } 42 printf("%d\n", count); 43 } 44 return 0; 45 }
标签:qsort 算法 include 排序 代码 记录 16px 贪心算法 compare
原文地址:http://www.cnblogs.com/ray-coding-in-rays/p/6216474.html