贪心法,看了 DISCUSS 后写的, 以前听过大牛的讲解,这是第一道题。

# include <stdio.h>
# include <stdlib.h>

# define N 105

int n;
int t[N][2];

int cmp(const void *x, const void *y)
{
    return *(int *)x > *(int *)y ? 1:-1;
}

int main()
{
    int i, c, tmp;
    
    while (1)
    {
        scanf("%d", &n);
        if (n == 0) break;
        for (i = 0; i < n; ++i) scanf("%d%d", &t[i][1], &t[i][0]);
        qsort(t, n, sizeof(t[0]), cmp);
        
        c = 1;
        tmp = t[0][0];
        for (i = 0; i < n; ++i)
            if (t[i][1] >= tmp)
            {
                ++c;
                tmp = t[i][0];
            }
        
        printf("%d\n", c);
    }
    
    return 0;
}

//

相关文章:

  • 2022-12-23
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-17
  • 2021-07-15
  • 2021-12-10
  • 2021-05-16
  • 2022-12-23
  • 2021-10-15
相关资源
相似解决方案