首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 下面的程序用来计算并寻找平面坐标系中给定点中最近的点对(若存在多对,则输出其中的一对即可)。程序运行时,先输入点的个数和一组互异的点的坐标,通过计算每对点之
阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 下面的程序用来计算并寻找平面坐标系中给定点中最近的点对(若存在多对,则输出其中的一对即可)。程序运行时,先输入点的个数和一组互异的点的坐标,通过计算每对点之
admin
2013-07-03
18
问题
阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。
【说明】
下面的程序用来计算并寻找平面坐标系中给定点中最近的点对(若存在多对,则输出其中的一对即可)。程序运行时,先输入点的个数和一组互异的点的坐标,通过计算每对点之间的距离,从而确定出距离最近的点对。例如,在下图所示的8个点中,点(1,1)与(2,0.5)是间距最近的点对。
【C++代码】
#include
#include
using namespace std;
class GPoint{
private:
double x,y;
public:
void setX(double x){this->x=x;}
void setY(double y){this->y=y;}
double getX(){return this->x;}
double getY(){return this->y;}
};
class ComputeDistance{
public:
double distance(GPoint a,GPoint b){
return sqrt((a.getX() - b.getX())*(a.getX() - b.getX())
+(a.getY() - b.getY())*(a.getY() - b.getY()));
}
};
int main()
{
int i,j,numberOfPoints=0;
cout<<”输入点的个数:”;
cin>>numbe[OfPoints:
(1)
points=new GPoint[numberOfPoints];//创建保存点坐标的数组
memset(points,0,Sizeof(points));
cout<<”输入”<< numberOfPoints<<”个点的坐标:”;
for(i=0;i
double tmpx,tmpy;
cin>>tmpx>>tmpy;,
points
.setX(tmpx);
points
.setY(tmpy);
}
(2)
computeDistance= new ComputeDistance();
int p1=0,p2=1;//p1和p2用于表示距离最近的点对在数组中的下标double shortestDistance=computeDistance->distance(points[p1],points[p2]);
//计算每一对点之间的距离
for(i=0;i<numberOfPoints;i++){
for(j=i+1;j<
(3)
;j++){
double tmpDistance=computeDistance->
(4)
;
if(
(5)
) {
p1=i;p2=j;
shortestDistance= tmpDistance;
}
}
cout<<”距离最近的点对是:(”;
cout<<points[p1].getX()<<”,”<<points[p1].getY()<<”)和(”;
cout<<points[p2].getX()<<”,”<
delete computeDistance;
return 0:
}
选项
答案
(1)Gpoint* (2)ComputeDistance * (3)number()tPoints (4)distance(points[i],points[j]) (5)shortestDistance2>tmpDistance
解析
本题考查考生使用C++语言进行面向对象程序设计的能力。首先要理解清楚题目中有关最近点对的概念和计算方法,然后阅读程序以实现该功能。
(1)处显示创建保存点坐标的数组。这里的new运算符用于开辟数组空间,其语法规则为:new类型[初值]。(1)处需要填入一类型修饰符,因此应填入GPoint*。类似的思路,(2)处应填入ComputeDistance*。
根据程序段中的注释,(3)和(4)处实现计算每一对点
之间的距离。(3)处为循环控制变量,因为要计算所有对点间的距离,因此应填入numberOtPoints。(4)处应调用computeDistance类的distance函数计算每一对点points
和points[j]之间的距离,因此应填入distance(points
,points[j])。
(5)处应填入一个判断条件,以输出距离最小的点对。这可通过比较shortestDistance和tmpDistance来实现。因此,(5)处应填入shortestDistance>tmpDistance。
转载请注明原文地址:https://kaotiyun.com/show/gnjZ777K
本试题收录于:
程序员下午应用技术考试题库软考初级分类
0
程序员下午应用技术考试
软考初级
相关试题推荐
以下关于计算机网络协议的叙述中,不正确的是(58)________________。
______不属于企业信息系统存在的问题。
在PowerPoint中,下列关于自定义放映的叙述不正确的是(63)。
文件的使用权限包括完全控制、允许写入、允许读取、允许运行、允许删除等,其中完全控制、允许写入、允许读取、允许运行都允许用户进行的操作是(38)。
若在Excel的A1单元格中输入函数“=SUM(1,12,false)”,按回车键后,则A1单元格中显示的值为________。
编译程序的作用是将高级程序语言源程序翻译为(4)。
()是移动互联网的组成部分。
某PPT文件共有8张幻灯片,现选中第6张幻灯片,对其设置新的背景颜色,单击“应用”按钮后,则()。
为什么一般处理“震荡波”病毒时,首先要把被侵入的计算机系统从网络上断开?在计算机系统发现病毒并清除以后,在未接入网络之前,从安全方面考虑,若需重新安装操作系统,通常需要执行以下几项主要工作后,方可接入网络。请给出下列工作的合理顺序。A.安装操作
防火墙包过滤规则的默认策略为拒绝,下表给出防火墙的包过滤规则配置界面。若要求内部所有主机能使用IE浏览器访问外部IP地址为202.117.118.23的Web服务器,为图中(1)~(4)空缺处选择正确答案。(1)A.允许B.拒绝(2)A.192
随机试题
校务委员会是学校的()
常用的局域网操作系统有UNIX和Novell公司的_______、Microsoft公司的_______。
呼吸道病毒中,基因常发生变异的是
女,48岁。反酸、烧心、上腹胀4年余。对明确诊断有帮助的是()
供电人向用电人供电,用电支付电费的合同,是( )合同。
已满14周岁,不满18周岁的人犯罪,可以从轻或者减轻处罚。()
为何第二次世界大战后报业垄断进一步加剧?其影响如何?
Consumersandproducersobviouslymakedecisionsthatmoldtheeconomy,butthereisathirdmajor【C1】______toconsider,therol
Goodmorning,class.Today’stopicis"IdiomsandTheirOrigins".Anidiomisaphraseorexpressionwith【C1】______.Themeaning
TheriseoftheInternethasbeenoneofthemosttransformativedevelopmentsinhumanhistory,comparableinimpacttotheinve
最新回复
(
0
)