Skip to content

11503: 【原1503】[Textbook]Ex2-11 求两点间距离

题目

题目描述

author: 翁惠玉 张億一 万诚 白毅伟 陈乐群 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1503

Description

对于一个二维平面上的两个点(x1, y1)和(x2, y2),编写一个程序计算两点间的距离

Input Format

输入两个点的坐标x1, y1, x2, y2

Output Format

输出两点之间的距离(保留5位小数)

Sample Input

1 2 3 4

Sample Output

2.82843

ligongzzz's solution

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main() {
    double x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    auto c = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    c = int64_t(c * 100000.0 + 0.5) / 100000.0;
    cout << c;

    return 0;
}

yyong119's solution

#include <cmath>
#include <iostream>
using namespace std;
int main() {
    float x1, x2, y1, y2, deltax, deltay;
    cin >> x1 >> y1 >> x2 >> y2;
    deltax = abs(x1 - x2); deltay = abs(y1 - y2);
    float ans = sqrt(deltax * deltax + deltay * deltay);
    cout.precision(6);
    cout << ans << endl;
    return 0;
}