簡単 0件解決済み 正答率0%

Largest of Three

Pick the largest of three numbers without a built-in max.

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

  • 組み込みの max を使わずに値を比べる
  • and で条件を組み合わせる
  • 等しい値を扱う

問題

Return the largest of three integers. Practice comparisons with if statements; don't use a built-in max function.

Implement largest_of_three(a, b, c).

Input

a: The first number. b: The second number. c: The third number.

Output

Return the largest value.

制約

-1,000,000,000 ≤ a, b, c ≤ 1,000,000,000

例

例 1
入力:
a = 3
b = 9
c = 4

期待される出力:
expected = 9
入力:
long long a = 3;
long long b = 9;
long long c = 4;

期待される出力:
long long expected = 9;
入力:
long a = 3;
long b = 9;
long c = 4;

期待される出力:
long expected = 9;

解説

9 is greater than both 3 and 4.

例 2
入力:
a = -5
b = -2
c = -2

期待される出力:
expected = -2
入力:
long long a = -5;
long long b = -2;
long long c = -2;

期待される出力:
long long expected = -2;
入力:
long a = -5;
long b = -2;
long c = -2;

期待される出力:
long expected = -2;

解説

-2 appears twice and is the largest value.

解き方のヒント

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

ヒント 1

2つずつ比べて、いちばん大きい数を見つける。

ヒント 2

少しの条件分岐で十分。探索用の構造は不要。

ヒント 3

同じ値がある場合も、正しく最大値を返すよう注意する。

目安の計算量

時間 O(1)、追加メモリ O(1)

参考資料

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

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。