Java程序以Java计算模式
在统计数学中,模式是出现最多时间的值。例如,假设一组值3、5、2、7、3。此值集的模式为3,因为它比其他任何数字都多。
算法
1.Take an integer set A of n values. 2.Count the occurrence of each integer value in A. 3.Display the value with the highest occurrence.
示例
public class Mode {
static int mode(int a[],int n) {
int maxValue = 0, maxCount = 0, i, j;
for (i = 0; i < n; ++i) {
int count = 0;
for (j = 0; j < n; ++j) {
if (a[j] == a[i])
++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}
return maxValue;
}
public static void main(String args[]){
int n = 5;
int a[] = {0,6,7,2,7};
System.out.println("Mode ::"+mode(a,n));
}
}输出结果
Mode ::7
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿