map容器结构体离散化

时间:2024-04-16 11:27:35
小数坐标离散化:
#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\n",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\n",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\n",mp[t]);
}
}
}