C#完成求一組數據眾數的辦法。本站提示廣大學習愛好者:(C#完成求一組數據眾數的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成求一組數據眾數的辦法正文
本文實例講述了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#法式設計有所贊助。