在1000个数中找出出现次数最多的数
来源:互联网 发布:有向图的关联矩阵 编辑:程序博客网 时间:2024/06/07 13:34
输出出现最多的数
package com.fonxian.findmax;/*问题: 在一个由自然数1-1000中某些数字所组成的数组中,每个数字可能出现零次或者多次。设计一个算法,找出出现次数最多的数字。*/public class FindMax { static int findMax(int[] a,int n){ int num;//a中的元素,temp的下标 int max = 0; int[] temp = new int[1000]; //将数组中的元素设为0,意为出现0次 for(int i = 0;i<1000;i++) temp[i] = 0; //a数组中的元素转换为temp数组的下标,temp中的元素则为出现的次数 for(int j = 0;j < n;j++){ num = a[j]; temp[num]++; } //找到出现次数最多的数 for(int t = 0;t<1000;t++){ if(temp[t]>max) max = t; } return max; } public static void main(String[] args) { int[] a={1,2,3,4,5,4,5,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8}; System.out.println("出现次数最多的数为:"+findMax(a,a.length)); }}
测试结果
“出现次数最多的数为:8
输出出现次数最多的前三个数
使用选择排序的思想,来找出现次数最多的前三个数
//输出出现次数最多的三个数 int key;//用来保存下标 int j; int flag=0;//标记用来判断是否满足3个数 int[] number = new int[3]; for(int i = 0;i<1000&&flag!=3;i++){ key = i; for(j=i;j<1000;j++){ if(temp[j]>temp[key]) key = j; } exch(temp,i,key); number[flag]=key; flag++; } for(int i = 0;i<3;i++) System.out.println(number[i]);
exch函数
//交换两个数组的值 static void exch(int[] a,int i,int j){ int temp = a[i]; a[i] = a[j]; a[j] = temp; }
测试结果
8
7
5
0 0
- 在1000个数中找出出现次数最多的数
- 找出数组中出现次数最多的数
- 找出数组中出现次数最多的数
- 找出出现次数最多的数
- 如何在数组中找出重复次数最多的数
- 40w随机数据找出其中次数出现最多的那个数(类似于在10亿百度ip中找出访问次数最多的那个ip)
- 从十亿数据中找出出现最多的数以及出现次数
- C# 在一个字符串中,找出出现次数最多的字母,并计算次数?
- 给定n个正整数,找出它们中出现次数最多的数。
- 数组中出现次数最多的数及出现次数
- 在N个数中找出出现奇数次的数
- OC 找出在只有空格字母的字符串中出现次数最多的单词
- java 找出n个元素数组中重复次数最多的数(假设出现次数大于n/2)
- 在100G文件中找出出现次数最多的100个IP
- 怎么在一串长字符串中找出出现最多次数的字符?
- 找出一句话中出现次数最多的单词
- 找出字符串中连续出现次数最多的子串
- 找出数组中出现次数最多的数字
- Windows Mobile Emulator:Failed to open the VPC Network Driver.
- 页面自动执行(加载)js的几种方法
- [小技巧] git 中查看某个文件是什么时候被删除的
- java判断服务器是那种,例如区分tomcat和weblogic
- AVS3.0 程序分析1
- 在1000个数中找出出现次数最多的数
- UVALive 6859 Points (凸包)
- Arrays类与Array类探究
- POJ 计算几何入门题目推荐(转)
- iOS支付宝接口调用总结
- Scrapy框架 概念笔记1
- Eclipse快捷键与使用技巧总结
- 推荐系统-猜你喜欢设计
- spring(1) helloworld