12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0
5解题思路:典型的贪心算法源代码:#include<stdio.h> #include<stdlib.h> typedef struct { int b; int e; }data; int m; data s[110]; int cmp(void const *a, void const *b) { if( ((data *)a)->e >= ((data *)b)->e ) return 1; else return -1; } int fun() { int i,j=1; int count=1; for(i=2;i<=m;i++) { if(s[i].b>=s[j].e) { j=i; count++; } else continue; } return count; } int main() { int i; while(scanf("%d",&m)!=EOF && m!=0) { for(i=1;i<=m;i++) scanf("%d%d",&s[i].b,&s[i].e); qsort(s+1, m, sizeof(s[0]), cmp); printf("%d\n",fun()); } system("pause"); return 0; }
原文地址:http://blog.csdn.net/zchlww/article/details/41577807