国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現求一組數據眾數的方法

2020-01-24 01:32:44
字體:
來源:轉載
供稿:網友

本文實例講述了C#實現求一組數據眾數的方法。分享給大家供大家參考。具體如下:

1.算法描述

1)輸入合法性檢驗(輸入不能為空)
2)制作數組副本,后面的操作將不修改數組本身,只對副本進行操作
3)數組排序(把相等的數都湊到一“堆兒”)
4)統計不同的元素數(統計“堆兒”數,以確定步驟5中要使用的數組大小)
5)統計各個元素數量(統計每“堆兒”的大小,并存入數組)
6)按元素在原數組內數量降序排列,數量相等的元素則按大小升序排列
7)統計眾數數量(確定返回數組的大小),如果眾數數量多余給出閾值的數量,則認為這個數組內沒有眾數
8)生成返回眾數數組

注:本算法只是提供了一種思路,并不代表此類問題的最優解

2.使用到的結構和函數

/// <summary>/// 結構:用于統計每個數出現的次數/// </summary>struct Stats{  //數字,出現的次數  public double Number;  public int Count;  //構造函數  public Stats(double n, int c)   {     Number = n;    Count = c;  }}/// <summary>/// 計算數組的眾數/// </summary>/// <param name="array">數組</param>/// <param name="threshold">數量閾值,眾數數量若多于次數則認為沒有眾數</param>/// <returns></returns>private static double[] ModeOf(double[] array, int threshold = 5){  //數組排序-統計各元素數量-按各元素數量排序-再統計最多的元素  //1.輸入合法性檢驗  if (array == null || array.Length == 0 || threshold < 1)  {    return new double[] { };  }  //2.制作數組副本,后面的操作將不修改數組本身  double[] tempArray = new double[array.Length];  array.CopyTo(tempArray,0);  //3.數組排序  double temp;  for (int i = 0; i < tempArray.Length; i++)  {    for (int j = i; j < tempArray.Length; j++)    {      if (tempArray[i] < tempArray[j])      {        temp = tempArray[i];        tempArray[i] = tempArray[j];        tempArray[j] = temp;      }    }  }  //4.統計不同的元素數  int counter = 1;  for (int i = 1; i < tempArray.Length; i++)  {    if (tempArray[i] != tempArray[i - 1])    {      counter++;    }  }  //5.統計各個元素數量  int flag = 0;  Stats[] statsArray = new Stats[counter];  statsArray[flag].Number = tempArray[0];  statsArray[flag].Count = 1;  for (int i = 1; i < tempArray.Length; i++)  {    if (tempArray[i] == statsArray[flag].Number)    {      statsArray[flag].Count++;    }    else    {      flag++;      statsArray[flag].Number = tempArray[i];      statsArray[flag].Count = 1;    }  }  //6.按元素在原數組內數量(Count屬性)降序排列  // 數量相等的元素則按大小升序排列  for (int i = 0; i < statsArray.Length; i++)  {    for (int j = i; j < statsArray.Length; j++)    {      if (statsArray[i].Count < statsArray[j].Count ||        (statsArray[i].Count == statsArray[j].Count &&          statsArray[i].Number > statsArray[j].Number))      {        temp = statsArray[i].Number;        statsArray[i].Number = statsArray[j].Number;        statsArray[j].Number = temp;        temp = statsArray[i].Count;        statsArray[i].Count = statsArray[j].Count;        statsArray[j].Count = (int)temp;      }    }  }  //7.統計眾數數量  int count = 1;  if (statsArray.Length > threshold &&    statsArray[threshold].Count == statsArray[0].Count)  {    //眾數多余閾值數量,則認為沒有眾數    return new double[] { };  }  else  {    for (int i = 1; i < statsArray.Length && i < threshold; i++)    {      if (statsArray[i].Count == statsArray[i - 1].Count)      {        count++;      }      else break;    }  }  //8.生成返回眾數數組  double[] result = new double[count];  for (int i = 0; i < count; i++)  {    result[i] = statsArray[i].Number;  }  return result;}

3.Main函數調用

static void Main(string[] args){  //示例數組1  double[] arr1 = new double[]   {    3, 2, 7, 4, 8, 8, 5,    5, 6, 5, 4, 3, 4, 9,    1, 1, 1, 2, 2, 0, 6  };  double[] d1 = ModeOf(arr1);  if (d1.Length != 0)  {    Console.Write("數組 1 有 " + d1.Length + " 個眾數:");    for (int i = 0; i < d1.Length; i++)    {      Console.Write(d1[i] + " ");    }    Console.WriteLine();  }  else  {    Console.WriteLine("數組 1 沒有眾數");  }  //示例數組2  double[] arr2 = new double[]   {    1, 2, 3, 4, 5, 6  };  double[] d2 = ModeOf(arr2);  if (d2.Length != 0)  {    Console.Write("數組 2 有 " + d2.Length + " 個眾數:");    for (int i = 0; i < d2.Length; i++)    {      Console.Write(d2[i] + " ");    }    Console.WriteLine();  }  else  {    Console.WriteLine("數組 2 沒有眾數");  }  Console.ReadLine();}

4.運行示例

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 密云县| 文登市| 林周县| 瓦房店市| 枣阳市| 抚宁县| 永宁县| 兴城市| 仁寿县| 诸城市| 志丹县| 南部县| 金溪县| 洪洞县| 定日县| 卫辉市| 泾阳县| 武陟县| 泰宁县| 怀来县| 明星| 金乡县| 迭部县| 公主岭市| 崇仁县| 大荔县| 古浪县| 大渡口区| 库尔勒市| 桓仁| 垦利县| 逊克县| 凉城县| 修水县| 南汇区| 汽车| 综艺| 南城县| 阿克陶县| 四川省| 崇州市|