手机找回
时间: 1ms 内存:128M
描述:
小慧把手机掉了,着急的她想让你帮她找到,她的手机是很高级的,离开主人一定时间后就会发射固定频率的电磁波。
现在你有一个仪器可以接受电磁波并能确定手机坐标,请你编程来确定小慧和手机的距离。
输入:
4个double型的数,前两个是手机的坐标,后两个是小慧的坐标。
输出:
小慧与手机的距离。(保留两位小数)
示例输入:
5 6 2 3
示例输出:
There are 4.24 meters between xiaohui and her phone.
提示:
参考答案(内存最优[1108]):
#include <stdio.h>
#include <math.h>
int main()
{
double a,b,c,d,e;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
e=sqrt(pow(a-c,2)+pow(b-d,2));
printf("There are %.2lf meters between xiaohui and her phone.",e);
return 0;
}
参考答案(时间最优[0]):
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
class Point
{
public:
Point(double a=0,double b=0,double c=0,double d=0):x1(a),y1(b),x2(c),y2(d) {}
void input();
friend void output(Point &);
private:
double x1;
double y1;
double x2;
double y2;
};
void Point::input()
{
cin>>x1>>y1>>x2>>y2;
}
void output(Point &p)
{
double s;
s=sqrt((p.x1-p.x2)*(p.x1-p.x2)+(p.y1-p.y2)*(p.y1-p.y2));
cout<<setiosflags(ios::fixed)<<setprecision(2);
cout<<"There are "<<s<<" meters between xiaohui and her phone."<<endl;
}
int main()
{
Point p1;
p1.input();
output(p1);
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。