一种排序 set<stl> 无重复 + < 符号重载 ==

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

一种排序

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 3
 
描述
现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);

1.按照编号从小到大排序

2.对于编号相等的长方形,按照长方形的长排序;

3.如果编号和长都相同,按照长方形的宽排序;

4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;
 
输入
第一行有一个整数 0<n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0<m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,

第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出
顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
样例输入
1
8
1 1 1
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
样例输出
1 1 1
1 2 1
1 2 2
2 1 1
2 2 1

分析:
只需重载< ,

代码如下:
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include<string>
#include<cstring>
#include<string.h>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
class Node{
    public:
    int id;
    int length;
    int weight;
    public:
    Node(){}
    Node(int a, int b, int c):id(a),length(b),weight(c){}
    int operator<(const Node &s)const
    {
        if(id==s.id)
        {
            if(length==s.length)
                return weight<s.weight;
            return length<s.length;
        }
        else return id<s.id;
    }
    bool operator ==(const Node &s)
    {
        return id==s.id&& length==s.length &&weight==s.weight;
    }
    friend ostream& operator<<(ostream &os, const Node &s){
        os<<s.id<<" "<<s.length<<" "<<s.weight<<endl;
        return os;
    }
};


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        set<Node>myset;
        set<Node>::iterator it;
        int n;
        int id,a,b,Max,Min;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>id>>a>>b;
            Max=a>=b?a:b;
            Min=a<b?a:b;
            myset.insert(Node(id,Max,Min));
        }
        for(it=myset.begin(); it != myset.end(); it++)
        {
            cout<<*it;
        }
    }
    return 0;
}            

  

Problem Description

当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个网络地址。网络地址等于子网掩码与 IP 地址按位进行与运算后的结果,例如:

子网掩码:A.B.C.D

IP 地址:a.b.c.d

网络地址:(A&a).(B&b).(C&c).(D&d)

Input

第一行包含一个整数T1T50代表测试数据的组数,

接下来T组测试数据。每组测试数据包含若干行,

第一行两个正整数N1N1000,1M50,M。接下来N行,每行一个字符串,代表一个 IP 地址,

再接下来M行,每行一个字符串代表子网掩码。IP 地址和子网掩码均采用 A.B.C.D的形式,其中ABCD均为非负整数,且小于等于255。

Output

对于每组测试数据,输出两行:

第一行输出: "Case #i:" 。i代表第i组测试数据。

第二行输出测试数据的结果,对于每组数据中的每一个子网掩码,输出在此子网掩码下的网络地址的数量。

Sample Input
2
5 2
192.168.1.0
192.168.1.101
192.168.2.5
192.168.2.7
202.14.27.235
255.255.255.0
255.255.0.0
4 2
127.127.0.1
10.134.52.0
127.0.10.1
10.134.0.2
235.235.0.0
1.57.16.0
Sample Output
Case #1:
3
2
Case #2:
3
4




构建结构体,并对结构体内进行 < 重载 , 完成排序,并用set 容器进行无重复操作

代码如下:
#include <iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#define maxn 1010
#include<map>
#include<set>
using namespace std;

int nn[maxn][5] ;
int mm[55][5] ;

struct Node{
    int b[4] ;
     bool operator < (const Node & s) const{
        if(b[0] == s.b[0]){
            if(b[1] == s.b[1]){
                if(b[2] == s.b[2]){
                    return b[3] < s.b[3] ;
                }
                else return b[2] < s.b[2] ;
            }
            else return b[1] < s.b[1] ;
        }
        else return b[0] < s.b[0] ;
    }
};

set<Node>se ;
set<Node>::iterator ite ;
int main()
{
    int t , kk = 1;
    Node node[maxn] ;
    cin>> t ;
    while(t--){
        printf("Case #%d:\n" , kk++) ;
        int n , m ;
        cin >> n >> m ;
        for(int i =1 ; i<= n ; i++)
            scanf("%d.%d.%d.%d" , &nn[i][0] , &nn[i][1] , &nn[i][2] , &nn[i][3]) ;
        for(int i = 1 ; i<= m  ;i++){
            scanf("%d.%d.%d.%d" , &mm[i][0]  ,&mm[i][1] , &mm[i][2] , &mm[i][3]) ;
        }
        for(int i =1 ; i<= m  ; i++){
            se.clear() ;
            for(int j = 1 ; j<=n  ; j++){
                    for(int k = 0 ; k < 4;  k++){
                        node[j].b[k]= mm[i][k] & nn[j][k] ;
                    }
                   se.insert(node[j]) ;
            }
            cout << se.size() << endl;
//            for(ite = se.begin() ; ite != se.end() ; ite ++ )
//                cout << *ite<< endl;
        }

    }
    return 0;
}

 







转载于:https://www.cnblogs.com/zn505119020/p/3620486.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值