rwkj 1359 友元:兩點距離

來源:互聯網
上載者:User

標籤: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;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.