zoukankan      html  css  js  c++  java
  • rwkj 1359 友元:两点距离


    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;
    }

  • 相关阅读:
    Java实现二叉排序树
    servlet/filter/listener/interceptor区别与联系
    Java中创建对象的5种方式
    字符串练习
    成员变量、类变量、局部变量的区别
    强制清除gradle 缓存
    XML
    jQuery
    JavaScript
    CSS
  • 原文地址:https://www.cnblogs.com/2014acm/p/3911225.html
Copyright © 2011-2022 走看看