面试题,高手进

[复制链接]
查看11 | 回复9 | 2010-11-18 15:04:43 | 显示全部楼层 |阅读模式
java统计字符串中每个字符出现的次数,区分大小写。

编写一个模拟时钟,初始其时间;有一个任务列表,根据时钟的时间找出任务列表中与该时间最近的任务,执行该任务并从列表
中删除,当任务列表中没有任务时,结束程序。

代码实现一个队列。

用Java语言实现的各种排序,使用插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序任意一种
对数组或列表中元素 1,0,3,8,5,6,7,9,12,14,56,24,35,79,90,87,56,4进行排序,
排序完成后使用二分查找法找出79的位置
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
第一题:
package edu.cumt.jnotnull;
public class Test {

public static void main(String[] args) {

String str = "abcABCab";

int[] strCounts = new int[255];// 基本 ASCII 是 128 个,扩展 ASCII 是 256 个。

char ch = 0;

for (int i = str.length() - 1; i >= 0; i--) {

ch = str.charAt(i);

++strCounts[ch];

}

for (int i = strCounts.length - 1; i >= 0; i--) {

if (strCounts != 0) {

System.out.print((char) i);

System.out.println(strCounts);

}

}

}
}

复制代码

第二题:不明白它是要求执行一次还是执行多次,要是任务列表中任务按照开始时间排序的话,要执行肯定就执行 当前时间后的所有任务了(总有一个最接近的)。也不知道这个序列可不可以再加入新任务。如果可以加入新任务,弄一个观察者模式观察他就可以了。
[ 本帖最后由 sunnyan 于 2009-9-8 10:18 编辑 ]
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
第三题 来个最简单的 数组实现
package edu.cumt.jnotnull;
public interface QueueInterface {

public abstract void clear();

public abstract T dequeue();

public abstract void enqueue(T newEntry);

public abstract T getFront();

public abstract boolean isEmpty();
}复制代码package edu.cumt.jnotnull;
import java.io.Serializable;
public class ArrayQueue implements QueueInterface, Serializable {
private T[] queue;
private int frontIndex;
private int backIndex;
private static final int DEFAULT_INITIAL_CAPACITY = 50;

public ArrayQueue(){
this(DEFAULT_INITIAL_CAPACITY);
}

public ArrayQueue(int initialCapacity){
queue = (T[])new Object[initialCapacity+1];
frontIndex = 0;
backIndex = initialCapacity;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public T dequeue() {
T front = null;
if(!isEmpty()){

front = queue[frontIndex];

queue[frontIndex] = null;

frontIndex = (frontIndex+1)%queue.length;
}
return front;
}
@Override
public void enqueue(T newEntry) {
if(isArrayFull()){

doubleArray();
}
backIndex = (backIndex+1)%queue.length;
queue[backIndex] = newEntry;
}
@Override
public T getFront() {
T front = null;
if(!isEmpty()){

front = queue[frontIndex];
}
return front;
}
@Override
public boolean isEmpty() {
return frontIndex == (backIndex+1)%queue.length;
}

private boolean isArrayFull(){
return frontIndex == (backIndex+2)%queue.length;
}

private void doubleArray(){
T [] oldQueue = queue;
int oldSize = oldQueue.length;
queue = (T[])new Object[2*oldSize];
for(int index = 0;index复制代码
[ 本帖最后由 sunnyan 于 2009-9-8 10:26 编辑 ]
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
第四题:
插入排序 递归和非递归实现
package edu.cumt.jnotnull;
public class InsertOrder {

public static > void insertionSort(T[] a,

int first, int last) {

for (int unsortedIndex = first + 1; unsortedIndex > void insertInOrder(//将firstUnsorted插入到从a[begin]到a[end]的有序数组之间

T firstUnsorted, T[] a, int begin, int end) {

int index = end;

while ((index >= begin) && (firstUnsorted.compareTo(a[index]) > void insertionSortRec(T[] a,

int first, int last) {

if(first > void insertInOrderRec(T element,T[] a,

int begin, int end){

if(element.compareTo(a[end])>0){

a[end+1] = element;

}

else if(begin 复制代码
[ 本帖最后由 sunnyan 于 2009-9-8 10:14 编辑 ]
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
二分查找
private static > int BinarySearch(T[] a,

int begin, int end, T target) {

int resultIndex = -1;

int middle = (begin+end)/2;

if(begin>end){

return -1;

}

if ((a[middle]).compareTo(target) == 0) {

return resultIndex = middle;

} else if ((a[middle]).compareTo(target) > 0) {

return BinarySearch(a, begin, middle-1, target);

} else if ((a[middle]).compareTo(target) 复制代码
[ 本帖最后由 sunnyan 于 2009-9-8 10:15 编辑 ]
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
呵呵
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
感谢“ sunnyan” 的讲解,下面是我写的代码,估计问题不少,还请大家给指点一下。。
第二题:我理解应该是这个时钟对应一个任务列表,没有新的加入。我的思路是写一个定时器,根据时间查找对应的任务去执行
第三题:
public class T110 {

public static void main(String args[]) {

int[] k = { 1, 2, 3, 4, 5 };

getOutput(k);

}

public static void getOutput(int[] k) {

Queue queue = new LinkedList(); // ListedList implements Queue

for (int i = 0; i0) {

Object o = queue.poll(); // 出队

System.out.print(o);

System.out.print(" ");

}

}
}
第四题:冒泡和选择
import java.sql.Array;
import java.util.Arrays;
/**
* 用Java语言实现的各种排序,使用插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序任意一种 对数组或列表中元素
* 1,0,3,8,5,6,7,9,12,14,56,24,35,79,90,87,56,4进行排序, 排序完成后使用二分查找法找出79的位置
*
* 先排序再进行二分查找
*
* @author Administrator
*
*/
public class SortAndFind {

// sort the array by Bubble arithmetic

public static int[] getSortByBubble(int[] arr) {

int out = 0, in = 0;

for (out = arr.length - 1; out > 1; out--) {

for (in = 0; inarr[in + 1]) {

int temp = arr[in];

arr[in] = arr[in + 1];

arr[in + 1] = temp;

}

}

}

for (int i = 0; iend) {

index = -1;

}

if (target > midVal) {

index = binarySearch(arr, target, middle + 1, end);

} else if (target < midVal) {

index = binarySearch(arr, target, begin, middle - 1);

} else {

index = middle;

}

// System.out.println(index);

return index;

}

public static void main(String args[]) {

int[] tmpArr = { 1, 0, 3, 8, 5, 6, 7, 9, 12, 14, 65, 24, 35, 79, 90,

87, 56, 4 };

int index = 0;

// getSortArray(tmpArr);
//
index = binarySearch(getSortByBubble(tmpArr), 8, 0, tmpArr.length - 1);

Arrays.sort(tmpArr);

index = binarySearch(tmpArr, 8, 0, tmpArr.length - 1);

// binarySearch1(getSortArray(tmpArr), 79);

System.out.println();

System.out.println(index);

}
}
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
nice job
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
呵呵。。。进来学习的
回复

使用道具 举报

千问 | 2010-11-18 15:04:43 | 显示全部楼层
李敖同学比较用功啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行