欢迎使用信阳师范学院在线评测(Online Judge)平台! 1798: 一种排序 时间限制: 3 Sec 内存限制: 64 MB 提交: 17 解决: 13 您该题的状态:已完成 [提交

第一种方法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct H {
    int a,b,c;
} A[10050];
int cmp(H x,H y) {
    if(x.a==y.a&&x.b==y.b)
        return x.c<y.c;
    if(x.a==y.a)
        return x.b<y.b;
    return x.a<y.a;
}
int main() {
    int m;
    scanf("%d",&m);
    while(m--) {
        int N;
        scanf("%d",&N);
        for(int i=0; i<N; i++) {
            scanf("%d %d %d",&A[i].a,&A[i].b,&A[i].c);
            if(A[i].b<A[i].c) {
                int t=A[i].b;
                A[i].b=A[i].c;
                A[i].c=t;

            }
        }
        sort(A,A+N,cmp);
        for(int i=0; i<N; i++) {
            {
                if(A[i].a==A[i+1].a&&A[i].b==A[i+1].b&&A[i].c==A[i+1].c)//除重
                    continue;
            }
            printf("%d %d %d\n",A[i].a,A[i].b,A[i].c);
        }
    }
    return 0;
}

第二种方法

/*set容器可以去重,因为这道题我用的是结构体,所以主要是运算符的重
载,默认情况下的比较函数是按键值从小到大的顺序插入元素,然后我们需要重载小于号*/
#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
struct Node {
    int num,l,w;
    bool operator < (const Node& x)const { // 运算符重载 //const是一个C语言的关键字,它限定一个变量不允许被改变
        return num!=x.num?num<x.num:(l!=x.l?l<x.l:w<x.w); 
    }//按照num从小到大排序,如果num相等则按照l从小到大排
};
int main() {
    int n,m;
    scanf("%d",&n);
    set<Node>s;
    Node rec;
    while(n--) {
        s.clear();
        scanf("%d",&m);
        for(int i=0; i<m; i++) {
            scanf("%d %d %d",&rec.num,&rec.l,&rec.w);
            if(rec.l<rec.w)
                swap(rec.l,rec.w);
            s.insert(rec);
        }
        set<Node>::iterator it;
        for(it=s.begin(); it!=s.end(); it++) {
            printf("%d %d %d\n",(*it).num,(*it).l,(*it).w);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40835329/article/details/81507377
今日推荐