結果

問題 No.275 中央値を求めよ
ユーザー Hidetoshi KamataHidetoshi Kamata
提出日時 2022-05-09 16:02:00
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 65 ms / 1,000 ms
コード長 1,025 bytes
コンパイル時間 10,098 ms
コンパイル使用メモリ 144,716 KB
実行使用メモリ 166,148 KB
最終ジャッジ日時 2023-09-23 14:28:50
合計ジャッジ時間 14,531 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
28,724 KB
testcase_01 AC 64 ms
29,040 KB
testcase_02 AC 64 ms
28,904 KB
testcase_03 AC 59 ms
29,136 KB
testcase_04 AC 64 ms
29,048 KB
testcase_05 AC 63 ms
28,984 KB
testcase_06 AC 63 ms
29,004 KB
testcase_07 AC 65 ms
31,104 KB
testcase_08 AC 61 ms
31,096 KB
testcase_09 AC 61 ms
29,236 KB
testcase_10 AC 65 ms
31,232 KB
testcase_11 AC 59 ms
28,996 KB
testcase_12 AC 63 ms
31,096 KB
testcase_13 AC 59 ms
28,892 KB
testcase_14 AC 63 ms
28,796 KB
testcase_15 AC 64 ms
29,112 KB
testcase_16 AC 65 ms
29,264 KB
testcase_17 AC 65 ms
29,100 KB
testcase_18 AC 63 ms
28,844 KB
testcase_19 AC 60 ms
29,084 KB
testcase_20 AC 64 ms
31,212 KB
testcase_21 AC 64 ms
29,168 KB
testcase_22 AC 63 ms
29,196 KB
testcase_23 AC 60 ms
28,820 KB
testcase_24 AC 64 ms
29,208 KB
testcase_25 AC 65 ms
31,260 KB
testcase_26 AC 59 ms
29,184 KB
testcase_27 AC 60 ms
29,016 KB
testcase_28 AC 60 ms
31,016 KB
testcase_29 AC 61 ms
29,132 KB
testcase_30 AC 59 ms
29,052 KB
testcase_31 AC 64 ms
31,068 KB
testcase_32 AC 59 ms
29,144 KB
testcase_33 AC 61 ms
28,984 KB
testcase_34 AC 58 ms
28,772 KB
testcase_35 AC 59 ms
29,048 KB
testcase_36 AC 60 ms
31,232 KB
testcase_37 AC 63 ms
29,352 KB
testcase_38 AC 64 ms
29,208 KB
testcase_39 AC 59 ms
29,112 KB
testcase_40 AC 61 ms
166,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 127 ms).
.NET 向け Microsoft (R) Build Engine バージョン 17.0.0-preview-21470-01+cb055d28f
Copyright (C) Microsoft Corporation.All rights reserved.

  プレビュー版の .NET を使用しています。https://aka.ms/dotnet-core-preview をご覧ください
  main -> /home/judge/data/code/bin/Release/net6.0/main.dll
  main -> /home/judge/data/code/bin/Release/net6.0/publish/

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var count = Convert.ToInt32(Console.ReadLine());
        var values = Console.ReadLine()
                        .Split(" ", count)
                        .Select(m => Convert.ToInt32(m))
                        .ToList();
        Console.WriteLine(GetMedian(values));
    }
    
    static decimal GetMedian(List<int> numbers)
    {
        var sortedNumbers = numbers.OrderBy(m => m).ToList();
        if (numbers.Count % 2 == 0)
        {
            // 偶数の場合は中央順位2個の算術平均
            var medians = numbers.OrderBy(m => m).Skip(numbers.Count / 2 - 1).Take(2).ToList();
            return Math.Round(((medians[0] + medians[1])) / 2m, 1, MidpointRounding.AwayFromZero); 
        }
        else
        {
            // 奇数個の場合は中央順位のデータ
            return numbers.OrderBy(m => m).ToList()[(numbers.Count / 2)];
        }
    }
}
0