標籤:java os io 資料 時間 c++ ios on
C++:友元1(兩點之間的距離)
時間限制(普通/Java):1000MS/3000MS 運行記憶體限制:65536KByte
總提交:674 測試通過:457
描述
定義一個二維平面中的點(point)類,類中的資料成員為點的座標,然後定義友元函數dist()用來計算兩點之間的距離。
將下面的程式1 和程式2填寫完整。
程式1 :
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
……………………………………
……………………………………
……………………………………
int main()
{
int n;
double x1,x2,y1,y2;
cin>>n;
while (n--)
{
cin>>x1>>y1>>x2>>y2;
point p1(x1,y1),p2(x2,y2);
cout<<fixed<<setprecision(3)<<dist(p1,p2)<<endl;
}
return 0;
}
程式2:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
……………………………………
……………………………………
……………………………………
int main()
{ int n; double x1,x2,y1,y2; test t;
cin>>n;
while (n--)
{ cin>>x1>>y1>>x2>>y2;
point p1(x1,y1),p2(x2,y2);
cout<<fixed<<setprecision(3)<<t.dist(p1,p2)<<endl;
}
}
輸入
輸入包含n組測試例, 第1行是測試組數。
第2行--第n+1行為測試資料,每組測資料有4個實數,表示 2個點的座標(x1,y1)和(x2,y2)。
輸出
兩點之間的距離(保留3位小數)。
範例輸入
2
0 0 3 4
1 1 2 2
範例輸出
5.000
1.414
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double distance(int x1,int y1,int x2,int y2)
{
double s;
s=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
return s;
}
int main()
{
int x1,x2,y1,y2,n;
double s;
cin>>n;
while(n--)
{
cin>>x1>>y1>>x2>>y2;
s=distance(x1,y1,x2,y2);
cout<<setiosflags(ios::fixed)<<setprecision(3)<<s<<endl;
}
return 0;
}
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class point
{
double x,y;
friend double dist(point, point);
public:
point(double xx, double yy) {x=xx;y=yy;}
};
double dist(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{
int n;
double x1,x2,y1,y2;
cin>>n;
while (n--)
{
cin>>x1>>y1>>x2>>y2;
point p1(x1,y1),p2(x2,y2);
cout<<fixed<<setprecision(3)<<dist(p1,p2)<<endl;
}
return 0;
}
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
class point
{
double x,y;
friend double distance(point, point);
public:
point(double xx, double yy) {x=xx;y=yy;}
};
double distance(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{
int n;
double x1,x2,y1,y2;
cin>>n;
while (n--)
{
cin>>x1>>y1>>x2>>y2;
point p1(x1,y1),p2(x2,y2);
cout<<fixed<<setprecision(3)<<distance(p1,p2)<<endl;
}
return 0;
}
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
class point
{
double x,y;
public:
double distance(point p)
{ return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); }
point(double xx, double yy) {x=xx;y=yy;}
};
int main()
{
int n;
double x1,x2,y1,y2;
cin>>n;
while (n--)
{
cin>>x1>>y1>>x2>>y2;
point p1(x1,y1),p2(x2,y2);
cout<<fixed<<setprecision(3)<<p1.distance(p2)<<endl;
}
return 0;
}