• 求中位数


     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace GetMedian
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int[] list = { 1,2,5,3,9,7,15,0,4};
    14             SortList(list);
    15             foreach (int a in list)
    16             {
    17                 Console.Write(a + ",");
    18             }
    19             double mid = GetMedian(list);
    20             Console.WriteLine("The midian is {0}",mid);
    21             Console.ReadKey();
    22         }
    23         public static void SortList(int[] list)
    24         {
    25             for (int i = 0; i < list.Length; i++)
    26             {
    27                 for (int j = 0; j < list.Length - 1 - i; j++)
    28                 {
    29                     if (list[j] > list[j + 1])
    30                     {
    31                         int temp = list[j];
    32                         list[j] = list[j + 1];
    33                         list[j + 1] = temp;
    34                     }
    35                 }
    36             }
    37         }
    38         public static double GetMedian(int[] list)
    39         {
    40             double mid = 0;
    41             if (list.Length % 2 == 1)
    42             {
    43                 mid = list[list.Length / 2 ];
    44             }
    45             else
    46             {
    47                 mid = (double)(list[list.Length / 2] + list[list.Length / 2 - 1]) / 2.0;
    48             }
    49             return mid;
    50         }
    51     }
    52 }
  • 相关阅读:
    所有选择器
    display:block、display:inline与displayinline:block的概念和区别
    jQuery 选择器
    JS日历制作获取时间
    HTML DOM 事件
    访问HTML元素(节点)
    HTML常用标签
    flask+mysql的Docker 部署
    docker(三)
    flask如何部署
  • 原文地址:https://www.cnblogs.com/hehe625/p/7773341.html
Copyright © 2020-2023  润新知