# B2 友元光顾

B2 友元光顾

//************* begin *****************
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
class CPoint
{
private:
double x;  // 横坐标
double y;  // 纵坐标
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy) {}
double distance1(CPoint &);
____(1)_____ double distance2(CPoint &, CPoint &);
};
double CPoint::distance1(CPoint &p)
{
double dx,dy;
dx=_____(2)_____;
dy=_____(3)_____;
return sqrt(dx*dx+dy*dy);
}
double distance2(CPoint &p1,CPoint &p2)
{
double dx,dy;
dx=_____(4)_____;
dy=_____(5)_____;
return sqrt(dx*dx+dy*dy);
}
//************* begin *****************
int main()
{
double px1, py1, px2, py2;
cin>>px1>>py1>>px2>>py2;
CPoint p1(px1, py1), p2(px2, py2);
cout <<setiosflags(ios::fixed)<<setprecision(2);
cout<<"1. "<<p1.distance1(p2)<<endl;
cout<<"2. "<<distance2(p1,p2)<<endl;
return 0;
}

``2.5 4.3 7.8 6.7``

``````1. 5.82
2. 5.82``````

``````#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,d,s;
scanf("%f%f%f%f",&a,&b,&c,&d);
s=sqrt((c-a)*(c-a)+(d-b)*(d-b));
printf("1. %.2f\n2. %.2f\n",s,s);
return 0;
}
``````

``````#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
class CPoint
{
private:
double x;  // 横坐标
double y;  // 纵坐标
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy) {}
double distance1(CPoint &);
friend double distance2(CPoint &, CPoint &);
};

double CPoint::distance1(CPoint &p)
{
double dx,dy;
dx=x-p.x;
dy=y-p.y;
return sqrt(dx*dx+dy*dy);
}

double distance2(CPoint &p1,CPoint &p2)
{
double dx,dy;
dx=p1.x-p2.x;
dy=p1.y-p2.y;
return sqrt(dx*dx+dy*dy);
}

int main()
{
double px1, py1, px2, py2;
cin>>px1>>py1>>px2>>py2;
CPoint p1(px1, py1), p2(px2, py2);
cout <<setiosflags(ios::fixed)<<setprecision(2);
cout<<"1. "<<p1.distance1(p2)<<endl;
cout<<"2. "<<distance2(p1,p2)<<endl;
return 0;
}``````