Giải thuật:
Nhập liệu:
1.Nhập tọa độ (x1, y1) của điểm A và tọa độ (x2, y2) của điểm B từ người dùng.
Tính khoảng cách:
Sử dụng công thức tính khoảng cách giữa hai điểm trong mặt phẳng Descartes:
Khoảng cách = √((x2 - x1)^2 + (y2 - y1)^2)
Công thức này dựa trên định lý Pytago: trong tam giác vuông, bình phương cạnh huyền bằng tổng bình phương hai cạnh góc vuông.
Làm tròn kết quả:
Sử dụng hàm setprecision trong C++ để làm tròn kết quả đến 2 chữ số thập phân.
Xuất kết quả:
In ra màn hình khoảng cách đã tính được.
Độ phức tạp:
Độ phức tạp của thuật toán này là O(1)
Code c++ như sau:
```
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
const int N=1e6+3;
using namespace std;
int x1,y1,x2,y2;
int main(){
cin>>x1>>y1>>x2>>y2;
cout<<fixed<<setprecision(2)<<sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
```
# $\textbf{DISTANCE}$
---
## $\textbf{How to solve this problem?}$
$\text{To calculate the distance between two points in a coordinate plane, use the $\textbf{distance formula}$:}$
$$
d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}
$$
$\text{Where:}$
- $\text{$(x_1, y_1)$ is the coordinates of the first point.}$
- $\text{$(x_2, y_2)$ is the coordinates of the second point.}$
- $\text{$d$ is the Euclidean distance between two points.}$
---
## $\textbf{Full Code}$
### $\textbf{C++}$
```cpp
#include <iostream>
#include <cmath> // For sqrt() and pow()
#include <iomanip> // For setprecision()
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
double d = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
cout << fixed << setprecision(2) << d;
return 0;
}
```
### $\textbf{Python}$
```python
import math # For sqrt()
x1, y1, x2, y2 = map(int, input().split())
d = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
print("{:.2f}".format(d))
```