标题:[求助]如何把"求众数的函数"优化啊??
只看楼主
梦之翼
Rank: 1
等 级:新手上路
帖 子:74
专家分:4
注 册:2006-7-5
结帖率:100%
 问题点数:0 回复次数:2 
[求助]如何把"求众数的函数"优化啊??

下面是代码草稿:

namespace Ch05Ex07p
{
class Program
{
static int MaxValueInt(int[] doubleArray)
{
int maxVal = doubleArray[0];
for (int i = 1; i < doubleArray.Length; i++)
{
if (doubleArray[i] > maxVal)
maxVal = doubleArray[i];
}
return maxVal;
}
static double MaxValue(double [] doubleArray) //这是求最大值的可以不看
{
double maxVal = doubleArray[0];
for (int i=1;i<doubleArray .Length ;i++)
{
if (doubleArray[i] > maxVal)
maxVal = doubleArray[i];
}
return maxVal ;
}

static double PublicVal (double []doubleArray)
{
double publicVal =doubleArray[0];
int timenum=0;
int []times=new int [doubleArray.Length];
for (int i=0;i<doubleArray .Length ;i++)
{
for (int n = 0; n < doubleArray.Length; n++)
{
if (doubleArray[i] == doubleArray[n])
times[i] = ++times[i];
}
}
timenum=MaxValueInt (times);
for (int x=0;x<times .Length ;x++)
{
if (times[x]==timenum)
return doubleArray[x];
}
return 0;
}

static void Main(string[] args)
{
double maxVal;
double publicVal;
double[] numbers ={ 10, 10, 6, 6, 6, 9, 0 };
maxVal = MaxValue(numbers);
Console.WriteLine("最大值为{0}", maxVal);
publicVal = PublicVal(numbers);
Console.WriteLine("众数为{0}", publicVal);
}
}
}

这段代码很冗长,而且如果有两个相同数量的数只能输出一个:
请大家帮我优化一下,谢谢
搜索更多相关主题的帖子: 众数 函数 
2006-07-08 22:19
夜空的星星
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2006-3-17
得分:0 

using System;
using System.Collections;
using System.Collections.Generic;
namespace Ch05Ex07p
{
class Program
{
static int[] MaxValueInt(int[] doubleArray)
{
int[] maxArray = new int[doubleArray.Length];
int maxVal = doubleArray[0];
for (int i = 1; i < doubleArray.Length; i++)
{
if (doubleArray[i] > maxVal)
maxVal = doubleArray[i];
}
int p = 0;
for (int i = 0; i < doubleArray.Length; i++)
{
if (doubleArray[i] == maxVal)
{
maxArray[p++] = doubleArray[i];
}
}
return maxArray;
//return maxVal;
}
static double MaxValue(double[] doubleArray) //这是求最大值的可以不看
{
double maxVal = doubleArray[0];
for (int i = 1; i < doubleArray.Length; i++)
{
if (doubleArray[i] > maxVal)
maxVal = doubleArray[i];
}
return maxVal;
}
static double[] PublicVal(double[] doubleArray)
{
/* double publicVal = doubleArray[0];
// int timenum = 0;
int[] timenum;
int[] times = new int[doubleArray.Length];
for (int i = 0; i < doubleArray.Length; i++)
{
for (int n = 0; n < doubleArray.Length; n++)
{
if (doubleArray[i] == doubleArray[n])
times[i] = ++times[i];
}
}
timenum = MaxValueInt(times);
/* for (int x = 0; x < times.Length; x++)
{
if (times[x] == timenum)
return doubleArray[x];
}//改写挺麻烦的,我自己写个好了
*/
List<double> list = new List<double>();
double[] array = new double[doubleArray.Length];
int[] arraytick = new int[doubleArray.Length];
for (int i = 0; i < arraytick.Length; i++)
arraytick[i] = 0;
int p = 0;
for (int i = 0; i < doubleArray.Length; i++)
{
int index = list.FindIndex(delegate(double pp) {return pp==doubleArray[i]; });

if ( index== -1)
{
array[p] = doubleArray[i];
arraytick[p++]++;
list.Add(doubleArray[i]);
}
else
{
arraytick[index]++;
}
}
ArrayList doublemax = new ArrayList();
int max = arraytick[0];
for (int i = 1; i < arraytick.Length; i++)
{
if (arraytick[i] > max)
max = arraytick[i];
}
for (int i = 0; i < arraytick.Length; i++)
{
if (arraytick[i] == max)
doublemax.Add(array[i]);
}
double[] res = new double[doublemax.Count];
for (int i = 0; i < doublemax.Count; i++)
{
res[i] = (double)doublemax[i];
}
return res;
}

static void Main(string[] args)
{
double maxVal;
double[] publicVal;
double[] numbers ={ 10, 10, 6,6,10, 6, 9, 0 };


maxVal = MaxValue(numbers);


Console.WriteLine("最大值为{0}", maxVal);
publicVal = PublicVal(numbers);
foreach (double p in publicVal)
{
Console.Write(p);
Console.Write(" ");
}
Console.WriteLine();
}
}
}
可以了,即使出现多个众数,都没问题


qq:58926957 技术群:22185706
2006-07-09 20:20
梦之翼
Rank: 1
等 级:新手上路
帖 子:74
专家分:4
注 册:2006-7-5
得分:0 

非常感谢!!!!!发自肺腑~


既不回头,何必不忘。 既然无缘,何必誓言。 今日种种,似水无痕。 明夕何夕,君已陌路。
2006-07-10 09:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-77295-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.222287 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved