• map容器结构体离散化


    小数坐标离散化:
    #include"string.h"
    #include"stdio.h"
    #include"iostream"
    #include"algorithm"
    #include"queue"
    #include"stack"
    #include"stdlib.h"
    #include"map"
    #include"string"
    #include"math.h"
    #define inf 10000000
    #define INF 0x3f3f3f3f
    const double PI=acos(-1.0);
    const double r2=sqrt(2.0);
    const int M=100;
    const int N=1010*502*2;
    const double g=9.8;
    #define eps 1e-10
    using namespace std;
    struct node
    {
        double x,y;
        node(){}
        node(double xx,double yy)
        {
            x=xx;
            y=yy;
        }
        bool operator<(const node &b)const
        {
            if(fabs(x-b.x)<eps)
                return y<b.y;
            else
                return x<b.x;
        }
    };
    int main()
    {
        int n,i;
        while(scanf("%d",&n)!=-1)
        {
            map<node,int>mp;
            int cnt=0;
            for(i=1;i<=n;i++)
            {
                node e;
                scanf("%lf%lf",&e.x,&e.y);
                if(!mp[e])
                mp[e]=++cnt;
            }
            double a,b;
            while(scanf("%lf%lf",&a,&b)!=-1)
            {
                node t(a,b);
                printf("%d
    ",mp[t]);
            }
        }
    }
    

    整数二维坐标离散化:

    #include"string.h"
    #include"stdio.h"
    #include"iostream"
    #include"algorithm"
    #include"queue"
    #include"stack"
    #include"stdlib.h"
    #include"map"
    #include"string"
    #include"math.h"
    #define inf 10000000
    #define INF 0x3f3f3f3f
    const double PI=acos(-1.0);
    const double r2=sqrt(2.0);
    const int M=100;
    const int N=1010*502*2;
    const double g=9.8;
    #define eps 1e-10
    using namespace std;
    struct node
    {
        int x,y;
        node(){}
        node(int xx,int yy)
        {
            x=xx;
            y=yy;
        }
        bool operator<(const node &b)const
        {
            if(x==b.x)
                return y<b.y;
            else
                return x<b.x;
        }
    };
    int main()
    {
        int n,i;
        while(scanf("%d",&n)!=-1)
        {
            map<node,int>mp;
            int cnt=0;
            for(i=1;i<=n;i++)
            {
                node e;
                scanf("%d%d",&e.x,&e.y);
                if(!mp[e])
                mp[e]=++cnt;
            }
            int a,b;
            while(scanf("%d%d",&a,&b)!=-1)
            {
                node t(a,b);
                printf("%d
    ",mp[t]);
            }
        }
    }
    

    同理三维多维也可以:

    #include"string.h"
    #include"stdio.h"
    #include"iostream"
    #include"algorithm"
    #include"queue"
    #include"stack"
    #include"stdlib.h"
    #include"map"
    #include"string"
    #include"math.h"
    #define inf 10000000
    #define INF 0x3f3f3f3f
    const double PI=acos(-1.0);
    const double r2=sqrt(2.0);
    const int M=100;
    const int N=1010*502*2;
    const double g=9.8;
    #define eps 1e-10
    using namespace std;
    struct node
    {
        int x,y,z;
        node(){}
        node(int xx,int yy,int zz)
        {
            x=xx;
            y=yy;
            z=zz;
        }
        bool operator<(const node &b)const
        {
            if(z==b.z)
            {
                if(y==b.y)
                    return x<b.x;
                else
                    return y<b.y;
            }
            else
                return z<b.z;
        }
    };
    int main()
    {
        int n,i;
        while(scanf("%d",&n)!=-1)
        {
            map<node,int>mp;
            int cnt=0;
            for(i=1;i<=n;i++)
            {
                node e;
                scanf("%d%d%d",&e.x,&e.y,&e.z);
                if(!mp[e])
                mp[e]=++cnt;
            }
            int a,b,c;
            while(scanf("%d%d%d",&a,&b,&c)!=-1)
            {
                node t(a,b,c);
                printf("%d
    ",mp[t]);
            }
        }
    }
    



  • 相关阅读:
    JZ067剪绳子
    JZ064滑动窗口的最大值
    JZ061序列化二叉树
    JZ063数据流中的中位数
    JZ066机器人的运动范围
    LeetCode151翻转字符串里的单词
    LeetCode153寻找旋转排序数组中的最小值
    LeetCode162寻找峰值
    JZ062二叉查找树的第 K 个结点
    centos7下rsync+inotify脚本实现文件同步,.NET CORE客户端文件更新后自动重启服务
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348128.html
Copyright © 2020-2023  润新知