首页 > 试题广场 >

点的距离

[编程题]点的距离
  • 热度指数:3634 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

创建一个CPoint 类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。

要求:

1。输入两个点的坐标,输出两个点之间的距离

2。重载运算符为“-


输入描述:
输入第一行为样例数m,接下来m行每行4个整数分别表示两个点的横纵坐标。


输出描述:
输出m行,通过重载“-”运算输出两点的距离,保留小数点后两位。
示例1

输入

1
0 0 2 0

输出

2.00
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
  
using namespace std;
class CPoint{
    
    public :
        int x,y;
        
        CPoint()
        {
            
        }
        CPoint(int x,int y)
        {
            this->x = x; this->y = y;
        }
        void operator - (const CPoint A)
        {
            double ans = 0.0;
            ans = sqrt(1.0*((A.x - x) * (A.x - x)) + 1.0 * ((A.y - y) *(A.y - y)));
            printf("%.2lf\n",ans);
        }
};
int main()
{
    int n;
    cin >> n;
    for(int i = 0 ;i<n;i++)
    {
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        CPoint p(a,b);
        CPoint q(c,d);
        p - q;
    }
}

发表于 2019-03-04 20:57:30 回复(0)
Python实现 重载 ‘-’
class CPoint(object):
    def __init__(self, coordinateX, coordinateY):
        self.x = coordinateX
        self.y = coordinateY

    def __sub__(self, other):
        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5


for _ in range(int(input())):
    firstX, firstY, secondX, secondY = map(int, input().split())
    first = CPoint(firstX, firstY)
    second = CPoint(secondX, secondY)
    print('%.2f' % (first - second))

编辑于 2019-02-26 19:45:43 回复(1)
这里要求用CPoint类,嫌麻烦就没有用QAQ,但是能通过检查点
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){
    int m;
    cin>>m;
    while(m>0){
        int x0,y0,x1,y1;
        cin>>x0>>y0>>x1>>y1;
        cout<<fixed<<setprecision(2)<<sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1))<<endl;
        m--;
    }
}

发表于 2019-02-24 21:18:41 回复(1)
#include<stdio.h>
(737)#include<math.h>
int main()
{
    int m,x1,x2,y1,y2;float d;
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        printf("%.2f\n",d);
    }
}

发表于 2020-05-10 13:17:37 回复(0)
#include <iostream>
#include <cmath>
using namespace std;
class CPoint {
  public:
    int x, y;
    CPoint(int _x, int _y): x(_x), y(_y) {}
    double operator-(CPoint& p) {
        return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
    }
};
int main() {
    int m;
    cin >> m;
    while (m--) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        CPoint p1(x1, y1), p2(x2, y2);
        printf("%.2lf\n", p1 - p2);
    }
}

发表于 2023-01-17 13:19:58 回复(0)
说好的重载呢?咋都直接写了!
发表于 2021-03-08 19:58:21 回复(0)
哎,纯c又没按要求写,一个字:懒!!
#include<bits/stdc++.h>
using namespace std;
int main(){
    int m;
    scanf("%d",&m);
    while(m--)
    {
        int x0,y0,x1,y1;
        scanf("%d %d %d %d",&x0,&y0,&x1,&y1);
        printf("%.2f\n",sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)));
    }
    return 0;
}


发表于 2020-04-11 17:27:01 回复(0)
#include<iostream>
(720)#include<cmath>
using namespace std;
class CPoint{
    private:
    int x;
    int y;
    public:
    CPoint(int x,int y){
        this->x=x;
        this->y=y;
    }
    void operator-(CPoint c){
        x=x-c.get_x();
        y=y-c.get_y();
    }
    int get_x(){
        return x;
    }
    int get_y(){
        return y;
    }
};
int main(){
    int k,a,b,c,d;
    cin>>k;
    while(k-- >0){
        cin>>a>>b>>c>>d;
        CPoint c1(a,b);
        CPoint c2(c,d);
        c1-c2;
        double res=sqrt(pow(c1.get_x(),2)+pow(c1.get_y(),2));
        printf("%.2f\n",res);
    }
    return 0;
}

发表于 2020-04-05 09:52:18 回复(0)
用hypot可以省点敲式子的时间
#include <cstdio>
#include <cmath>

class CPoint {
    public:
    int x, y;
    CPoint(int xx, int yy): x(xx), y(yy) {}
    float operator-(const CPoint &r) {
        return hypot(x - r.x, y - r.y);
    }
};

int main () {
    int N, x1, y1, x2, y2;
    scanf("%d", &N);
    while (N--) {
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        printf("%.2f\n", CPoint(x1, y1) - CPoint(x2, y2));
    }
}


发表于 2020-02-12 22:56:43 回复(0)
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class CPoint{
    public:
    CPoint(int x=0,int y=0):m_x(x),m_y(y){};
    public:
    double operator-(const CPoint &cp);
    private:
    int m_x;
    int m_y;
};
double CPoint::operator-(const CPoint &cp)
{
    return sqrt(pow(this->m_x-cp.m_x,2)+pow(this->m_y-cp.m_y,2));
}
int main()
{
    int m;
    cin>>m;
    while(m--)
    {
        int a1,a2,b1,b2;
        cin>>a1>>a2>>b1>>b2;
        CPoint cp1(a1,a2),cp2(b1,b2);
        double res=cp1-cp2;
        cout<<fixed<<setprecision(2)<<res<<endl;
    }
    return 0;
}



发表于 2019-06-16 13:15:10 回复(0)
#include <iostream>
#include <cmath>

class CPoint
{
private:
    int x;
    int y;
    double ans;

public:
    CPoint(int a, int b):x(a),y(b){}
    CPoint & operator-(const CPoint c)
    {
        this->ans = sqrt(1.0*(x - c.x)*(x - c.x) + 1.0*(y - c.y)*(y - c.y));
        return *this;
    }

    double getAns()
    {
        return ans;
    }
};

int main()
{
    int m;
    std::cin>>m;
    while(m--)
    {
        int x1,y1,x2,y2;
        std::cin>>x1>>y1>>x2>>y2;
        CPoint t(x1,y1),c(x2,y2);
        CPoint a = t-c;
        printf("%.2f\n",a.getAns());
    }

    return 0;
}

发表于 2019-04-21 16:22:16 回复(0)
可以AC
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();//样例数
        while (n>0) {
            int x1 = in.nextInt();
            int y1 = in.nextInt();
            int x2 = in.nextInt();
            int y2 = in.nextInt();
            double a = Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
            buffer.append(String.format("%.2f ", a));        
            n--;
        }
        String[] split = buffer.toString().split(" ");
        for (String string : split) {
            System.out.println(string);
        }
    }
}


发表于 2019-04-12 10:35:43 回复(0)
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main
{
    public static void main(String [] args)
    {
        Scanner reader;
        reader = new Scanner(System.in);
        int num;
        NUM = reader.nextInt();
        double [] x1;
        x1 = new double [num];
        double [] y1;
        y1 = new double [num];
        double [] x2;
        x2 = new double [num];
        double [] y2;
        y2 = new double [num];
        for(int i = 0; i <num; i ++)
        {
            x1 [i] = reader.nextInt();
            Y1 [I] = reader.nextInt();
            X2 [I] = reader.nextInt();
            Y2 [I] = reader.nextInt();
        }
        CPoint cp;
        for(int i = 0; i <num; i ++)
        {
            cp = new CPoint(x1 [i],y1 [i],x2 [i],y2 [i]);
            cp.requiresize();
        }
    }
}
类连接点
{
    双长度;
    CPoint(double x1,double y1,double x2,double y2)
    {
        length = Math.sqrt(Math.abs(x1-x2)* Math.abs(x1-x2)+ Math.abs(y1-y2)* Math。 ABS(Y1-Y2));
    }
    公共无效requiresize()
    {
        DecimalFormat的DF;
        df = new DecimalFormat(“0.00”);
        的System.out.println(df.format(长度));
    }
}

发表于 2019-04-09 16:10:49 回复(0)
# ** 函数的创建&运算符重载 ** # class CPoint(object): def __init__(self, x, y): self.x = x self.y = y def __sub__(self, dot): return ((self.x - dot.x)**2 + (self.y - dot.y)**2)**0.5  # ** 数据输入 ** # num = int(input()) for i in range(num):
    d0 = [int(x) for x in input().split()]
    dot0 = CPoint(d0[0],d0[1])
    dot1 = CPoint(d0[2],d0[3]) print('%0.2f'%(dot0-dot1))
发表于 2019-04-01 16:24:58 回复(0)
//运行时间为4ms,占用内存460k
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
class CPoint{
    private:
    int x,y;
    public :
    CPoint(){}
    CPoint(int a,int b){
        x=a;
        y=b;
    }
    double operator -(CPoint point){//重载运算符-
        return pow((pow(point.x-x,2)+pow(point.y-y,2)),0.5);
    }
};
int main(){
    int m;
    int a,b,c,d;
    cin>>m;
    while(m--){
        cin>>a>>b;
        cin>>c>>d;
        CPoint p1(a,b);//创建对象
        CPoint p2(c,d);//创建对象
        cout<<fixed<<setprecision(2)<<p1-p2<<endl;
    }
}

发表于 2019-03-30 15:41:03 回复(0)
m = int(input())
for i in range(m):
    x1,y1,x2,y2 = map(int,input().split())
    print('%.2f'%((abs(x1-x2)**2 + abs(y1-y2)**2)**0.5))

发表于 2019-03-29 10:52:54 回复(0)
import java.util.Scanner;
import java.lang.Math;
public class Main{
    static class Cpoint{
        int x;
        int y;
        char ch;
        public Cpoint(int x, int y){
            super();
            this.x=x;
            this.y=y;
        }
        public Cpoint() {
   // TODO Auto-generated constructor stub
  }
  public double Distance(int x,int y,char ch){
            if(ch=='-'){
                double d=(this.x-x)*(this.x-x)+(this.y-y)*(this.y-y);
                return Math.sqrt(d);
            }else{
                return 0;
            }
        }
        public void setX(int x){
            this.x=x;
        }
        public void setY(int y){
            this.y=y;
        }
        public int getX(){
            return this.x;
        }
        public int getY(){
            return this.y;
        }
    }
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        for(int i=0;i<m;i++){
            Cpoint p1=new Cpoint();
            Cpoint p2=new Cpoint();
            p1.setX(sc.nextInt());
            p1.setY(sc.nextInt());
            p2.setX(sc.nextInt());
            p2.setY(sc.nextInt());
            double b=p1.Distance(p2.getX(),p2.getY(),'-');
            System.out.println(String.format("%.2f", b));
        }
    }
    
}
我都不想在这发解题思路了实在是没啥意思,哎,,,,,,
发表于 2019-03-25 14:08:10 回复(0)
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
class CPoint{
    public:
    int x,y;
    CPoint(int x,int y) {
        this->x = x;this->y = y;
    }
    double operator-(const CPoint &p) {
        int temp = pow(this->x-p.x,2)+pow(this->y-p.y,2);
        return pow(temp,0.5);
    }
};
int main() {
    int m;
    cin >> m;
    while(m--) {
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        CPoint p1(a,b),p2(c,d);
        cout << fixed << setprecision(2) << p1-p2 << endl;
    }
}

发表于 2019-03-12 20:06:27 回复(0)
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<iomanip>

using namespace std;

class CPoint{
private:
    double x,y;
public:
    CPoint(double a,double b):x(a),y(b){}
    ~CPoint(){}
    double getX(){return x;}
    double getY(){return y;}
    double operator-(CPoint ob);
};
double CPoint::operator-(CPoint ob){
    return sqrt(pow((x-ob.getX()),2)+pow((y-ob.getY()),2));
}

int main(){
    int c;
    cin>>c;
    cout<<fixed<<setprecision(2);
    for(int i=0;i<c;i++){
        double x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        CPoint o1(x1,y1),o2(x2,y2);
        cout<<o1-o2<<endl;
    }
}

发表于 2019-03-10 17:15:02 回复(0)
struct CPoint{
    int x,y;
    double operator - (const CPoint &A)const{
       // printf("%d, %d, %d, %d",x,y,A.x,A.y);
        float m=sqrt((x-A.x)*(x-A.x)+(y-A.y)*(y-A.y));
       return m;
    }
};
int main(){
    int m,i;
    CPoint a,b;
    while(scanf("%d",&m)!=EOF){
       scanf("%d %d %d %d",&a.x,&a.y,&b.x,&b.y);
       //  printf("%lf, %lf, %lf, %lf\n",a.x,a.y,b.x,b.y);
       double m=a-b;
        printf("%.2lf\n",m);
    }
}
发表于 2019-02-26 21:46:51 回复(0)

问题信息

上传者:小小
难度:
20条回答 3434浏览

热门推荐

通过挑战的用户

查看代码