1。从n个整数中查找出现频率最高的所有整数(用java实现)
示例:5,5,8,5,3,5,3,3,3,1
中出现频率最高的整数是3和5
第一道题目我做出来了,问题是算法写的太垃圾了,
请你们把你们的算法写出来参考一下
还有一题数据库
2。 公司员工管理数据库中有俩个基本表
员工Employee(员工编号ID,姓名Name,所在部门DeptID,年龄Age);
部门Department(部门编号ID,部门名称Name,职能Function,地址Addr);
Employee 表中的DeptID和Department表中的ID关联;
请检索年龄低于员工平均年龄的所有"软件开发部"员工(用sql语句实现,计算平均值的sql函数为AVG)
select * from
employee e inner join department d
on e.id=d.id
and 部门='软件开发部'
and where年龄< (select avg(年龄) as '平均年龄' from employee )2道题目是否正确呢,帮我看一下,
请你们把你们的算法写出来参考一下
示例:5,5,8,5,3,5,3,3,3,1
中出现频率最高的整数是3和5
第一道题目我做出来了,问题是算法写的太垃圾了,
请你们把你们的算法写出来参考一下
还有一题数据库
2。 公司员工管理数据库中有俩个基本表
员工Employee(员工编号ID,姓名Name,所在部门DeptID,年龄Age);
部门Department(部门编号ID,部门名称Name,职能Function,地址Addr);
Employee 表中的DeptID和Department表中的ID关联;
请检索年龄低于员工平均年龄的所有"软件开发部"员工(用sql语句实现,计算平均值的sql函数为AVG)
select * from
employee e inner join department d
on e.id=d.id
and 部门='软件开发部'
and where年龄< (select avg(年龄) as '平均年龄' from employee )2道题目是否正确呢,帮我看一下,
请你们把你们的算法写出来参考一下
from Employee a,Department b where a.DeptID=b.ID and b.Name='软件开发部'
and a.Age<avg(a.Age)
select a.员工编号ID,a.姓名Name,a.年龄Age,b.部门名称Name
from Employee a,Department b where a.DeptID=b.ID and b.Name='软件开发部'
and a.Age<avg(a.Age)
and a.Age<avg(a.Age)
这一句我怎么觉得不行啊 个人感觉 我新手.~
我的算法
int[] ar = {5,5,8,5,3,5,3,3,3,1};
int[] fr = new int[ar.length];
int top = 0;
int i, j;
for (i = 0; i < fr.length; i++) {
j = i - 1;
while (j >= 0) {
if (ar[j] == ar[i])
break;
j--;
}
if (j < 0)
fr[i] = 1;
else
fr[i] = fr[j] + 1;
if (fr[i] > top)
top = fr[i];
}
for (i = 0; i < fr.length; i++) {
if (fr[i] == top)
System.out.println("" + ar[i] + " " + top);
}
}
private static void test5() {
String[] array = {"5","5","8","5","3","5","3","3","3","1"};
List list= Arrays.asList(array);
list = new ArrayList(list);
List outValue = new ArrayList();
int count = 0;
while (list.size()-1 > 0) {
String element = (String) list.get(list.size()-1);
int tempCount = 0;
for (int j = list.size()-1; j >= 0; j--) {
if (element.equals(list.get(j))) {
tempCount++;
list.remove(j);
}
}
if (tempCount > count) {
count = tempCount;
outValue.clear();
outValue.add(element);
} else if (tempCount == count) {
outValue.add(element);
}
}
System.out.println("出现频率最高的是:"+outValue+"\n出现了:"+count+"次");
}
private static void test6() {
String[] array = {"1","1","1","1","1","1","1","1","1","1"};
List outValue = new ArrayList();
int length = array.length - 1;
int count = 0;
String element = array[length];
while (true) {
int tempCount = 0;
if (element != null) {
for (int i = length; i >= 0; i--) {
if (element == array[i]) {
tempCount++;
array[i]=null;
}
}
if (tempCount > count) {
count = tempCount;
outValue.clear();
outValue.add(element);
} else if (tempCount == count) {
outValue.add(element);
}
if (length >= 0) {
element = array[length];
}
} else {
--length;
if (length < 0) {
break;
}
element = array[length];
continue;
}
}
System.out.println("出现频率最高的是:"+outValue+"\n出现了:"+count+"次");
}
新建一个map,键值放数组的值,对应的值放出现的次数。
遍历一遍数组能把map填满,再遍历一遍map可以找出出现次数最多的值
http://community.csdn.net/Expert/topic/5678/5678428.xml?temp=.3623163
String[] array = {"5","5","5","8","3","5","3","3","3","1"};
String result = "";
int length = array.length - 1;
int count = 0;
String element = array[length];
while (true) {
int tempCount = 0;
if (element != null) {
for (int i = length; i >= 0; i--) {
if (element.equals(array[i])) {
tempCount++;
array[i]=null;
}
}
if (tempCount > count) {
count = tempCount;
result = element;
} else if (tempCount == count) {
result = element + "," + result;
}
element = array[length];
} else {
--length;
if (length < 0) {
break;
}
element = array[length];
continue;
}
}
System.out.println("The\tmax\tnumber\tis\t"+result+"\nThe\tcount\tis\t"+count);
}
还有什么不足的地方其高手指教。
from Employee E
where E.age<
(select avg(age)
from Employee E,Department D
where E.deptid=D.id and D.name='软件开发部'
)
int[] count=new count[array.length];
int maxCount=0;
for(int i=0;i<array.length;i++){
count[i]=0;
for(int j : array){
j == count[i];
count[i]++;
}
if(count[i]>maxCount){
maxCount = i;
}
}
System.out.println("频率最高的数:"+array[maxCount]+"出现次数:"+count[maxCount]);没编译,不知道有没有问题
int a[]=new int[]{5,5,8,5,3,5,3,3,3,1,1,1,1,1,1,1};
int where=0;//确定出现的是哪一位数组元素
int end=0;//出现的次数
int count;//每一个元素出现的次数
for(int i=0;i<a.length;i++){
count=0;
for(int x=0;x<a.length;x++){
if(a[i]==a[x]){
count++;
}
}
if(count>end){
end=count;
where=i;
}
}
System.out.println("最多的数是:"+a[where]+" 出现次数是:"+end);
import java.util.*;public class RepeatNum {//设计思想:先排序,再两两比较,记录相同数字的个数
public void count(int[] a) {
Arrays.sort(a);//对a排序
int[][] b=new int[2][a.length];//存放每个数字及其个数
int j=0;//中间变量
int max=0; //计数器,记录最大次数 //对a中数字两两比较,将每个数字及其个数存放于b
//同时求出最大次大,放入max中
b[1][0]=1;//初始化:将第一个数的个数设为1
for(int i=0; i<a.length-1; i++) {
if(a[i]==a[i+1]){
b[0][j]=a[i];
b[1][j]++;
if(max<b[1][j]) max=b[1][j];
}
else {
j++;
b[0][j]=a[i+1];
b[1][j]++;
if(max<b[1][j]) max=b[1][j];
}
} //打印所有个数等于max的数字-次数对
for(int i=0; i<a.length; i++) {
if(b[1][i]==max) {
System.out.println (b[0][i]+"出现"+b[1][i]+"次");
}
}
} public static void main (String[] args){
RepeatNum r=new RepeatNum();
int[] a={1,3,3,5,7,9,6,8,10,9,5,3,4,8,5,9,1,1,7,6,9,1,0,0};
r.count(a);
}
}
from employee e,department d
where e.age < (select avg(e1.age) from employee e1)
and e.deptid = d.id
and d.name='软件开发部'楼主的问题错在不能执行,这个已经有人回答了select *
from Employee E
where E.age<
(select avg(age)
from Employee E,Department D
where E.deptid=D.id and D.name='软件开发部'
)
按照这位仁兄写的意思应该是在雇员中选出所有的平均年龄小于软件开发部员工平均年龄的员工
Dim rtnArr As New ArrayList
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer 'aArr中的元素
Dim aCount As Integer = 1 '出现最多的次数
Dim bCount As Integer = 1 '当前元素出现的次数
For i = 0 To aArr.Count - 1
m = aArr(i)
For j = 0 To aArr.Count - 1
If aArr(j) = m Then
bCount = bCount + 1
End If
Next
If bCount > aCount Then
aCount = bCount
k = i
End If
bCount = 1
Next
Return rtnArr
End Functiontoranshaoweng() 像你那样写的话,写的对的话,也只能找到最多的,但如果与两个最多就不行了.
Dim rtnArr As New ArrayList
Dim i As Integer
Dim j As Integer
Dim temp As Integer 'aArr中的元素
Dim aCount As Integer = 1 '出现最多的次数
Dim bCount As Integer = 0 '当前元素出现的次数
Dim coutArr As New ArrayList '出现最多次数的index的arraylist
For i = 0 To aArr.Count - 1
temp = aArr(i)
For j = 0 To aArr.Count - 1
If aArr(j) = temp Then
bCount = bCount + 1
End If
Next
If bCount >= aCount Then
aCount = bCount
If rtnArr.Count <> 0 Then
If rtnArr.IndexOf(temp) < 0 Then
rtnArr.Add(temp)
End If
Else
rtnArr.Add(temp)
End If End If
bCount = 0
Next
Return rtnArr
End Function
.net 的实现,应该没问题,调试过了
from Employee E
where E.age <
(select avg(age)
from Employee E,Department D
where E.deptid=D.id and D.name= '软件开发部 '
)