站点图标 陌路寒暄

距离产生美

距离产生美

时间: 1ms        内存:128M

描述:

小明和静静是大学同学,毕业后要去两个不同的城市工作。小明要静静做他的女朋友,静静说,如果他们的工作单位之间的距离在某个范围之内的话,就考虑小明的要求。

C++代码如下,只需提交空缺部分

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
/***********************/

          填空部分

/***********************/

double Distance(Point &p1,Point &p2)
{
    double d;
    d=sqrt((p1.x-p2.x)*1.0*(p1.x-p2.x)+(p1.y-p2.y)*1.0*(p1.y-p2.y));
    return d;
}
int main()
{
    double d;
    Point p1,p2;
    p1.input();
    p2.input();
    d=Distance(p1,p2);
    int r1,r2;
    cin>>r1>>r2;
    if(d>=r1&&d<=r2)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

输入:

输入有三行,所有数据均为正整数
第一行为小明单位的坐标 x1 y1
第二行为静静单位的坐标 x2 y2
第三行为静静要求的距离范围 r1 r2

输出:

如果静静答应做小明的女朋友输出 "Yes",否则输出"No"。

示例输入:

10 10
20 20
12 15

示例输出:

Yes

提示:

参考答案(内存最优[1092]):

#include<stdio.h>
int main()
{
	float a,b,c,d,e,f,g,h;
	scanf("%f %f",&a,&b);
	scanf("%f %f",&c,&d);
	scanf("%f %f",&e,&f);
	g=(a-c)*(a-c)+(b-d)*(b-d);
	e=e*e;
	f=f*f;
	if(g>=e&&g<=f)
	printf("Yes\n");
	else
	printf("No\n");
}

参考答案(时间最优[0]):


#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
class Point
{
public:
    void input();
    friend double Distance(Point &p1,Point &p2);
private:
    int x;
    int y;
};
void Point::input()
{
    cin>>x>>y;
}
double Distance(Point &p1,Point &p2)
{
    double d;
    d=sqrt((p1.x-p2.x)*1.0*(p1.x-p2.x)+(p1.y-p2.y)*1.0*(p1.y-p2.y));
    return d;
}
int main()
{
    double d;
    Point p1,p2;
    p1.input();
    p2.input();
    d=Distance(p1,p2);
    int r1,r2;
    cin>>r1>>r2;
    if(d>=r1&&d<=r2)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版