Solutions of Distance - MarisaOJ: Marisa Online Judge

Solutions of Distance

Select solution language

Write solution here.


User Avatar phphongyd    Created at    30 likes

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: #clude<bitsstdc++.h>#defefifirst#defesesecond#defelllonglongconstN=1e6+3;usingnamespacestd;x1,y1,x2,y2;ma(){cx1y1x2y2;coutfixedsetision(2)(x2-x1)(x2-x1)+(y2-y1)(y2-y1);}

User Avatar HuyNguyenCong    Created at    0 likes

# DISTANCE --- ## How to solve this problem? To calculate the distance between two points in a coordinate plane, use the distance formula: d=(x2x1)2+(y2y1)2 Where: - (x1,y1) is the coordinates of the first point. - (x2,y2) is the coordinates of the second point. - d is the Euclidean distance between two points. --- ## Full Code ### C++ cpp#clude<iostream>#clude<cmath>/Forandpow()#clude<iomanip>/Forsetision()usingnamespacestd;ma(){ios::syncwithstdio(false);c.tie(νllptr);x1,y1,x2,y2;cx1y1x2y2;doubd=pow((x2-x1),2)+pow((y2-y1),2);coutfixedsetision(2)d;return0;} ### Python pythonimportmath#Forx1,y1,x2,y2=map(,put().split())d=math.(x2-x1)2+(y2-y1)2pr({:.2f}.format(d))