1.编程实现:二分搜索算法
解答:
public class SearchTest {
/** 被搜索数据的大小 */
private static final int size = 5000000;
public static void main(String[] args) {
long[] data = new long[size];
// 添加测试数据
for (int k = 0; k < data.length; k++) {
data[k] = k;
}
// 要查找的数据
long target = 4970002;
binaryFindTest(data, target);
}
/**
* 二分搜索算法实现
*
* @param data
* 数据集合
* @param target
* 搜索的数据
* @return 返回找到的数据的位置,返回-1表示没有找到。
*/
public static int binaryFind(long[] data, long target) {
int start = 0;
int end = data.length – 1;
while (start <= end) {
int middleIndex = (start + end) / 2;
if (target == data[middleIndex]) {
return middleIndex;
}
if (target >= data[middleIndex]) {
start = middleIndex + 1;
} else {
end = middleIndex – 1;
}
}
return -1;
}
/**
* 二分搜索测试
*
* @param data
* 数据集合
* @param target
* 搜索的数据
*/
public static void binaryFindTest(long[] data, long target) {
long start = System.nanoTime();
int result = binaryFind(data, target);
long end = System.nanoTime();
System.out.println(“binary search position:” + result);
System.out.println(“binary search time:” + (end – start));
}
}
2.编程实现:线程A向队列Q中不停写入数据,线程B从队列Q中不停读取数据(只要Q中有数据)。
解答:
接口中有两个一个是向队列中写push方法 一个是从队列中读。
public interface StackInterface
{
public void push(int n);
public int[] pop();
}
上边接口的实现类。
public class SafeStack implements StackInterface {
private int top = 0;
private int[] values = new int[10];
private boolean dataAvailable = false;
public void push(int n) {
synchronized (this) {
while (dataAvailable) // 1
{
try {
wait();
} catch (InterruptedException e) {
// 忽略 //2
}
}
values[top] = n;
System.out.println(“压入数字” + n + “步骤1完成”);
top++;
dataAvailable = true;
notifyAll();
System.out.println(“压入数字完成”);
}
}
public int[] pop() {
synchronized (this) {
while (!dataAvailable) // 3
{
try {
wait();
} catch (InterruptedException e) {
// 忽略 //4
}
}
System.out.print(“弹出”);
top–;
int[] test = { values[top], top };
dataAvailable = false;
// 唤醒正在等待压入数据的线程
notifyAll();
return test;
}
}
}
读线程
public class PopThread implements Runnable
{
private StackInterface s;
public PopThread(StackInterface s)
{
this.s = s;
}
public void run()
{
while(true)
{
System.out.println(“->”+ s.pop()[0] + “<-”);
try {
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
}
写线程
public class PushThread implements Runnable
{
private StackInterface s;
public PushThread(StackInterface s)
{
this.s = s;
}
public void run()
{
int i = 0;
while(true)
{
java.util.Random r = new java.util.Random();
i = r.nextInt(10);
s.push(i);
try {
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
}
3.编程实现:使用Socket经行网络通信时,客户端和服务器端流程。
解答:
服务器,使用ServerSocket监听指定的端口,端口可以随意指定(由于1024以下的端口通常属于保留端口,在一些操作系统中不可以随意使用,所以建议使用大于1024的端口),等待客户连接请求,客户连接后,会话产生;在完成会话后,关闭连接。
客户端,使用Socket对网络上某一个服务器的某一个端口发出连接请求,一旦连接成功,打开会话;会话完成后,关闭Socket。客户端不需要指定打开的端口,通常临时的、动态的分配一个1024以上的端口。
4.编写代码实现同一平面内两圆是否碰撞,其中:
第一个圆圆心坐标为(x1,y1),半径是r1,第二个圆圆心坐标为(x2,y2),半径是r2。
方法声明如下:
boolean collisWith(int x1,int y1,int r1,int x2,int y2,int r2){}
解答:
boolean collisWith(int x1, int y1, int r1, int x2, int y2, int r2) {
boolean flag=false;
int num1=(x1-x2)*(x1-x2);
int num2=(y1-y2)*(y1-y2);
int num3=num1+num2;
double distance=Math.sqrt(num3);
if(distance<=(r1+r2)){
flag=true;
}
return flag;
}
5.判断一个int数组中的元素是否存在重复,方法声明如下:
boolean isRepeat(int[] m){ }
解答:
public boolean isRepeat2(int[] m){ Set h =new HashSet(m.length); for (int i = 0; i < m.length; i++) { h.add(new Integer(m[i])); } if (h.size()==m.length ){ return false; }else { return true; } }
6.用递归方法实现正序显示数组元素。例如String[] s = {“a”,”b”,”c”,”d”};
方法声明如下:
void print(String[] s,int i){ }
解答:参数 i 是指打印string数组的起始位置,原理是正序打印s从第0个开始的所有字符串,等价于先打印第0个,在打印s中从第一个开始的所有字符串,如此递归
void print(String[] s, int i) {
if ((i >= 0) && (i < s.length)) {
System.out.print(s[i]);
i++;
print(s, i);
}
}
7.请写出求n!的算法。
解答:
public class Factorial {
public static void main(String[] args) {
long n = 6;
System.out.println(doFactorial(n));
}
public static long doFactorial(long n) {
if (n < 1) {
System.out.println(“ERROR”);
return 0;
} else if (n == 1 || n == 2) {
return n;
} else {
return n * doFactorial(n – 1);
}
}
}
8.在当前的JSP网页里,提交用户名和密码,提交给post . jsp, post . jsp打印出用户名和密码并返回给浏览器。请写出post . jsp
解答:
假设页面用户名和密码在login.jsp里,login.jsp页面代码如下:
<form action=”post.jsp” method=”post”>
<input type=”text” name=”userName”>
<input type=”password” name=”pwd”>
<input type=”submit”>
</form>
post.jsp页面代码:
<%
String userName=request.getParameter(“userName”);
String pwd=request.getParameter(“pwd”);
out.println(“用户名:”+userName+”,密码:”+pwd);
%>
9.编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。
解答:采用了冒泡进行排序
import java.util.Scanner;
import java.util.Scanner;
public class MaxAndMin {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
int next = scanner.nextInt();
arr[i] = next;
}
int[] after=Arrays.sort(arr);
System.out.println(“最小值:”+after[0]+”,最大值:”+after[arr.length-1]);
}
}
10.写一个排序算法 1-100随机数字 进行排序 要求效率。
解答:
public class Sort {
// 选择排序方法
public static void selectionSort(int[] number) {
for (int i = 0; i < number.length – 1; i++) {
int m = i;
for (int j = i + 1; j < number.length; j++) {
if (number[j] < number[m])
m = j;
}
if (i != m)
swap(number, i, m);
}
}
// 用于交换数组中的索引为i、j的元素
private static void swap(int[] number, int i, int j) {
int t;
t = number[i];
number[i] = number[j];
number[j] = t;
}
public static void main(String[] args) {
// 定义一个数组
int[] num = new int[100];
for(int i=0;i<num.length;i++){
num[i]=(int)(Math.random()*100)+1;
}
// 排序
selectionSort(num);
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]);
}
}
}
1、转载来源于 https://blog.csdn.net/wickedvalley/article/details/51589792
2、想要转载请注明转载来源