C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法
本文實(shí)例講述了C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法。分享給大家供大家參考。具體如下:
1.算法描述
1)輸入合法性檢驗(yàn)(輸入不能為空)
2)制作數(shù)組副本,后面的操作將不修改數(shù)組本身,只對(duì)副本進(jìn)行操作
3)數(shù)組排序(把相等的數(shù)都湊到一“堆兒”)
4)統(tǒng)計(jì)不同的元素?cái)?shù)(統(tǒng)計(jì)“堆兒”數(shù),以確定步驟5中要使用的數(shù)組大?。?br />
5)統(tǒng)計(jì)各個(gè)元素?cái)?shù)量(統(tǒng)計(jì)每“堆兒”的大小,并存入數(shù)組)
6)按元素在原數(shù)組內(nèi)數(shù)量降序排列,數(shù)量相等的元素則按大小升序排列
7)統(tǒng)計(jì)眾數(shù)數(shù)量(確定返回?cái)?shù)組的大小),如果眾數(shù)數(shù)量多余給出閾值的數(shù)量,則認(rèn)為這個(gè)數(shù)組內(nèi)沒有眾數(shù)
8)生成返回眾數(shù)數(shù)組
注:本算法只是提供了一種思路,并不代表此類問題的最優(yōu)解
2.使用到的結(jié)構(gòu)和函數(shù)
/// <summary>
/// 結(jié)構(gòu):用于統(tǒng)計(jì)每個(gè)數(shù)出現(xiàn)的次數(shù)
/// </summary>
struct Stats
{
//數(shù)字,出現(xiàn)的次數(shù)
public double Number;
public int Count;
//構(gòu)造函數(shù)
public Stats(double n, int c)
{
Number = n;
Count = c;
}
}
/// <summary>
/// 計(jì)算數(shù)組的眾數(shù)
/// </summary>
/// <param name="array">數(shù)組</param>
/// <param name="threshold">數(shù)量閾值,眾數(shù)數(shù)量若多于次數(shù)則認(rèn)為沒有眾數(shù)</param>
/// <returns></returns>
private static double[] ModeOf(double[] array, int threshold = 5)
{
//數(shù)組排序-統(tǒng)計(jì)各元素?cái)?shù)量-按各元素?cái)?shù)量排序-再統(tǒng)計(jì)最多的元素
//1.輸入合法性檢驗(yàn)
if (array == null || array.Length == 0 || threshold < 1)
{
return new double[] { };
}
//2.制作數(shù)組副本,后面的操作將不修改數(shù)組本身
double[] tempArray = new double[array.Length];
array.CopyTo(tempArray,0);
//3.數(shù)組排序
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.統(tǒng)計(jì)不同的元素?cái)?shù)
int counter = 1;
for (int i = 1; i < tempArray.Length; i++)
{
if (tempArray[i] != tempArray[i - 1])
{
counter++;
}
}
//5.統(tǒng)計(jì)各個(gè)元素?cái)?shù)量
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.按元素在原數(shù)組內(nèi)數(shù)量(Count屬性)降序排列
// 數(shù)量相等的元素則按大小升序排列
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.統(tǒng)計(jì)眾數(shù)數(shù)量
int count = 1;
if (statsArray.Length > threshold &&
statsArray[threshold].Count == statsArray[0].Count)
{
//眾數(shù)多余閾值數(shù)量,則認(rèn)為沒有眾數(shù)
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.生成返回眾數(shù)數(shù)組
double[] result = new double[count];
for (int i = 0; i < count; i++)
{
result[i] = statsArray[i].Number;
}
return result;
}
3.Main函數(shù)調(diào)用
static void Main(string[] args)
{
//示例數(shù)組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("數(shù)組 1 有 " + d1.Length + " 個(gè)眾數(shù):");
for (int i = 0; i < d1.Length; i++)
{
Console.Write(d1[i] + " ");
}
Console.WriteLine();
}
else
{
Console.WriteLine("數(shù)組 1 沒有眾數(shù)");
}
//示例數(shù)組2
double[] arr2 = new double[]
{
1, 2, 3, 4, 5, 6
};
double[] d2 = ModeOf(arr2);
if (d2.Length != 0)
{
Console.Write("數(shù)組 2 有 " + d2.Length + " 個(gè)眾數(shù):");
for (int i = 0; i < d2.Length; i++)
{
Console.Write(d2[i] + " ");
}
Console.WriteLine();
}
else
{
Console.WriteLine("數(shù)組 2 沒有眾數(shù)");
}
Console.ReadLine();
}
4.運(yùn)行示例

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)將CSV轉(zhuǎn)為XLSX文件
Microsoft?Excel的XLSX格式以及基于文本的CSV(逗號(hào)分隔值)格式,是數(shù)據(jù)交換中常見的文件格式,本文主要介紹了如何在C#中以編程的方式將CSV文件轉(zhuǎn)化為XLSX?文件,需要的可以參考下2024-03-03
C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法
這篇文章主要介紹了C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法,涉及C#針對(duì)數(shù)字的簡單正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
C#基于WebSocket實(shí)現(xiàn)聊天室功能
這篇文章主要為大家詳細(xì)介紹了C#基于WebSocket實(shí)現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
c# 預(yù)處理識(shí)別硬幣的數(shù)據(jù)集
這篇文章主要介紹了c# 預(yù)處理識(shí)別硬幣的數(shù)據(jù)集的方法,幫助大家更好的利用c#進(jìn)行深度學(xué)習(xí),感興趣的朋友可以了解下2020-12-12
C#中免費(fèi)密碼庫BouncyCastle的使用詳解
這篇文章主要來和大家分享一個(gè)C#版開源、免費(fèi)的Bouncy?Castle密碼庫:BouncyCastle,文中介紹了BouncyCastle的具體使用,需要的可以參考下2024-03-03

