zoukankan      html  css  js  c++  java
  • 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]);
            }
        }
    }
    



  • 相关阅读:
    条款41:了解隐式接口和编译期多态
    条款41:了解隐式接口和编译期多态
    虚机制
    条款31:将文件间的编译依存关系降至最低
    条款30:透彻了解inlining的里里外外
    条款28:避免返回handles 指向对象内部成分
    条款27:尽量少做转型动作
    条款26:尽可能延后变量定义式的出现时间
    条款25: 考虑写出一个不抛异常的swap函数
    APP测试工程师面试题:之一
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348128.html
Copyright © 2011-2022 走看看