【发布时间】:2020-05-22 04:09:07
【问题描述】:
问题:MAIT的院长要去参观MAIT的宿舍。如您所知,他是一个非常忙碌的人,因此他决定只访问第一个“K”最近的旅馆。旅馆位于二维平面上。你得到了宿舍的坐标,你必须回答距离原点(Dean's place)最近的第 K 个宿舍的火箭距离
输入格式 输入的第一行包含 Q Total no。的查询和 K 有两种查询:
第一种类型:1 x y 对于第一种类型的查询,您了解了新建宿舍的坐标(x,y)。 second type: 2 查询2nd type,需要输出Kth最近旅馆的Rocket距离到现在。
//院长将永远留在他的地方(原点)。保证在第一个类型 2 查询之前至少有 k 个类型 1 查询。
两点(x2,y2)和(x1,y1)之间的火箭距离定义为(x2 - x1)2 + (y2 - y1)2
约束 1
输出格式 对于类型 2 的每个查询,输出距离 Origin 的第 K 个最近旅馆的 Rocket 距离。//
这是我的代码:
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
class roomno
{
public:
int x;
int y;
roomno(int x,int y)
{
this->x=x;
this->y=y;
}
void print()
{
cout<<"location"<<"("<<x<<","<<y<<")"<<endl;
}
int distance ()
{
return (x*x+y*y);
}
};
class roomcompare
{
public:
bool operator() (roomno r1,roomno r2)
{
return r1.distance()>r2.distance();
}
};
int main()
{
int x[1000]},y[1000];
int l,k=0;
priority_queue<roomno,vector<roomno>,roomcompare> pq;
int n,i,j;
cin>>n>>l;
//cin>>n;
cin.ignore();
for( i=0;i<n;i++)
{
cin>>x[i];
}
cin.ignore();
for( j=0;j<n;j++)
{
cin>>y[j];
}
cin.ignore();
for(i=0;i<n;i++ )
{
roomno r1(x[i],y[i]);
pq.push(r1);
}
while(!pq.empty()&&k!=l)
{ k++;
roomno r2=pq.top();
r2.print();
pq.pop();
}
return 0;
}
原始代码链接:https://codeshare.io/2j1bkA
我的代码有什么问题?
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读the help pages,获取the SO tour,了解how to ask good questions,以及this question checklist。最后,请学习如何创建minimal reproducible example 以向我们展示问题本身。
-
为什么你认为你的代码有问题
-
我从链接中添加了代码。以后请不要链接到您的代码,只需将其复制粘贴到问题中即可。或者,如果您认为自己的代码过多,请创建一个 MRE 代替。并且请告诉您的代码有什么问题。它编译吗?它会在运行时崩溃吗?它会产生错误的结果吗?