NYOJ8一种排序

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=8


STL sort的模板题,注意一下输入的时候长和宽的问题。。


代码:

#include <cstdio>
#include <algorithm>

using namespace std;

struct Node{
    int number;
    int length;
    int width;
}a[1005];

bool cmp(Node x,Node y)

{
    if(x.number != y.number)
        return x.number < y.number;
    else if(x.length != y.length)
        return x.length < y.length;
    else if(x.width != y.width)
        return x.width < y.width;
}
bool isEquals(Node x,Node y)

{
    if(x.number == y.number && x.length == y.length && x.width == y.width)
        return true;
    return false;
}
int main()

{
    int _case;
    scanf("%d",&_case);
    while(_case--)
    {
        int m;
        scanf("%d",&m);
        for(int i = 0;i < m;++i){
            scanf("%d%d%d",&a[i].number,&a[i].length,&a[i].width);
            if(a[i].length < a[i].width)
            {
                int t = a[i].length;
                a[i].length = a[i].width;
                a[i].width = t;
            }
        }
        sort(a,a + m,cmp);
        printf("%d %d %d\n",a[0].number,a[0].length,a[0].width);
        for(int i = 1;i < m;++i)
            if(!isEquals(a[i],a[i - 1]))
                printf("%d %d %d\n",a[i].number,a[i].length,a[i].width);
    }
    return 0;
}


你可能感兴趣的:(排序,ACM,STL)