HDU 2037 今年暑假不AC (结构体排序示例题)

来源:互联网 发布:淘宝店铺贷款入口 编辑:程序博客网 时间:2024/05/20 06:05

本题用到的一个很重要的知识就是结构体排序,对结构体进行排序在竞赛中的使用范围非常的广泛,希望大家可以熟练掌握这项技巧

结构体排序有两种写法,详情请看代码

#include<stdio.h>#include<algorithm>using namespace std;struct Ti {    int s;    int e;    friend bool operator < (Ti &a, Ti &b)    {        return a.e < b.e;    }};/*bool cmp(Ti a, Ti b){    return a.e < b.e;}**/                                //如果要使用这个bool规则,需要在sort函数括号里面加入“,cmp”struct Ti a[110];int main(){    int n,i;    while (scanf("%d", &n) != EOF)    {        if (n == 0)            return 0;        int count = 1;        for (i = 0;i < n;i++)        {            scanf("%d %d", &a[i].s, &a[i].e);        }        sort(a, a + n);        int temp = a[0].e;        for (i = 1;i < n;i++)        {            if (a[i].s >= temp)            {                count++;                temp = a[i].e;            }        }        printf("%d\n", count);    }    return 0;}
0 0
原创粉丝点击