给你个算法的
int[] n = new int[10000];
int min = 99999;
int max = 0;
System.Random rnd = new System.Random();
for (int ctr = 0; ctr < 10000; ctr++)
{
n[ctr] = (int)rnd.Next(10000, 99999);
Console.WriteLine("第{0}个数,值是{1}", ctr + 1, n[ctr]);
}
for (int ctr2 = 0; ctr2 < 10000; ctr2++)
{
if (n[ctr2] < min)
{
min = n[ctr2];
}
if (n[ctr2] > max)
{
max = n[ctr2];
}
}
//------------排序----------------
int i,j,temp;
for (i = 0; i < n.Length - 1; i++)
{
for (j = i + 1; j < n.Length; j++)
{
if (n[i] > n[j])
{
temp = n[i];
n[i] = n[j];
n[j] = temp;
}
}
}
//----------查找出现次数最多的元素------------------
int[] arrMax = new int[n.Length];//用于存放出现次数最多的元素
int count = 1; //记录出现最多的次数,初始为1,因为最少出现了1次
int now = 1; //临时记录每个数出现的次数
int flag = 0; //用于控制max数组的下标
for (i = 0; i < n.Length - 1; i++)
{
if (n[i] == n[i + 1])
{
now++;
}
else
{
if (now == count)
{
arrMax[flag] = n[i];
flag++;
}
else if (now > count)
{
count = now;
arrMax = new int[n.Length - i];
flag = 0;
arrMax[flag] = n[i];
flag++;
}
now = 1;
}
}
if (count == 1)
{
arrMax[flag] = n[n.Length - 1];
}
Console.WriteLine("最小数是{0},最大数是{1}", min, max);
Console.WriteLine("出现次数最多的数为:");
for (i = 0; i < arrMax.Length; i++)
{
if (arrMax[i] == 0)
{
break;
}
else
{
Console.WriteLine(arrMax[i]);
}
}
Console.WriteLine("出现的次数为:" + count);
Console.ReadKey();