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]);
            }
        }
    }
    



  • 相关阅读:
    7-20 (样卷)统计单词的个数 (40 分)
    7-21 删除字符 (30 分)
    7-19 计算有n个字符串中最长的字符串长度 (40 分)
    7-16 列表数字元素加权和(1) (40 分)
    7-17 列表元素个数的加权和(1) (40 分)
    7-15 求出歌手的得分 (40 分)
    7-10 jmu-python-异常-学生成绩处理基本版 (15 分)
    7-11 jmu-python-分段函数&数学函数 (15 分)
    7-12 产生每位数字相同的n位数 (30 分)
    7-9 jmu-python-异常-学生成绩处理专业版 (25 分)
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348128.html
Copyright © 2011-2022 走看看