簡単 14件解決済み 正答率46.7%

Distance Between Points

Compute the straight-line distance between two points.

このチャレンジで学べること

  • Apply the distance formula
  • Use square roots
  • Return a floating-point result

問題

翻訳を表示

Compute the Euclidean distance between (x1, y1) and (x2, y2): the square root of (x2 − x1)² + (y2 − y1)².

Implement distance(x1, y1, x2, y2).

Input

x1: First point x. y1: First point y. x2: Second point x. y2: Second point y.

Output

Return the distance.

制約

-1,000,000 ≤ each coordinate ≤ 1,000,000

例

例 1
入力:
x1 = 0.0
y1 = 0.0
x2 = 3.0
y2 = 4.0

期待される出力:
expected = 5.0
入力:
double x1 = 0.0;
double y1 = 0.0;
double x2 = 3.0;
double y2 = 4.0;

期待される出力:
double expected = 5.0;
入力:
double x1 = 0.0;
double y1 = 0.0;
double x2 = 3.0;
double y2 = 4.0;

期待される出力:
double expected = 5.0;

解説

√(3² + 4²) = √25 = 5.

例 2
入力:
x1 = 1.0
y1 = 1.0
x2 = 1.0
y2 = 1.0

期待される出力:
expected = 0.0
入力:
double x1 = 1.0;
double y1 = 1.0;
double x2 = 1.0;
double y2 = 1.0;

期待される出力:
double expected = 0.0;
入力:
double x1 = 1.0;
double y1 = 1.0;
double x2 = 1.0;
double y2 = 1.0;

期待される出力:
double expected = 0.0;

解説

Both points are the same, so the distance is 0.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Compute the horizontal and vertical differences between the two points.

ヒント 2

Square both differences, add them, then take the square root of the total.

ヒント 3

Watch for negative coordinates, but the distance formula still works the same.

目安の計算量

O(1) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 LMLaura Mahlow Python 676 ms - 5/5 コードを見る
2 KKKaneki Ken Python 677 ms - 5/5 コードを見る
3 Н"Никита "Programmer" Python 678 ms - 5/5 コードを見る
4 ゼゼロダニ Python 700 ms - 5/5 コードを見る
5 RCRiccardo Corti Python 705 ms - 5/5 コードを見る
6 JVJulien VACHAUDEZ Python 711 ms - 5/5 コードを見る
7 MSMichael Stoddart Python 712 ms - 5/5 コードを見る
8 HJhettory Jurash Python 732 ms - 5/5 コードを見る
9 LMliam morales Python 733 ms - 5/5 コードを見る
10 JPJittipat Phonhan Go 945 ms - 5/5 コードを見る
11 AAnjh3l0 C++ 2,565 ms - 5/5 コードを見る
12 ·𝓕·.★·.·⍣·.·★ 𝓕𝓪𝓫𝓲𝓸𝓵𝓪 𝓔𝓵𝓮𝓷𝓪 ★·.·⍣·.·★.· C++ 2,619 ms - 4/5 コードを見る
13 YSYoo Seungmin Java 2,626 ms - 5/5 コードを見る
14 PTphúc thịnh phạm C++ 2,672 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が14件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。