当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现求一组数据众数的方法

C#实现求一组数据众数的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网