【面试】5道JAVA笔试算法题

5道JAVA笔试算法题

开始准备面试刷题咯~

1.数据里有{1,2,3,4,5,6,7,8,9},请随机打乱顺序,生成一个新的数组(请以代码实现)

import java.util.Arrays;

//打乱数组
public class Demo1 {
	
	//随机打乱
	public static int[] srand(int[] a) {
		int[] b = new int[a.length];
		
		for(int i = 0; i < a.length;i++) {
			//随机获取下标
			int tmp = (int)(Math.random()*(a.length - i)); //随机数:[ 0 ,a.length - i )  
			b[i] = a[tmp];
			
			//将此时a[tmp]的下标移动到靠后的位置
			int change = a[a.length - i - 1];
			a[a.length - i - 1] = a[tmp];
			a[tmp] = change;
		}
		
		return b;
	}
	
	public static void main(String[] args) {
		int[] a = {1,2,3,4,5,6,7,8,9};
		System.out.println(Arrays.toString(srand(a)));
	}
}

2.写出代码判断一个整数是不是2的阶次方(请代码实现,谢绝调用API方法)

import java.util.Scanner;

//判断整数是不是2的阶次方
public class Demo2 {
	
	public static boolean check(int sum) {
		boolean flag = true; //判断标志
		while(sum > 1) {
			if (sum % 2 == 0) {
				sum = sum/2;
			} else {
				flag = false;
				break;
			}
		}
		return flag;
	}
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		int sum = scanner.nextInt();
		System.out.println(sum + " 是不是2的阶次方:" + check(sum));
	}
}

3.假设今日是2015年3月1日,星期日,请算出13个月零6天后是星期几,距离现在多少天(请用代码实现,谢绝调用API方法)

import java.util.Scanner;

//算出星期几
public class Demo4 {
	public static String[] week = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
	public static int i = 0;
	public static int[] monthday1 = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	public static int[] monthday2 = {0,31,29,31,30,31,30,31,31,30,31,30,31};
	
	//查看距离当前天数的差值
	public static String distance(int year,int month,int day,int newMonth,int newDay) {
		int sum = 0; //设定初始距离天数
		if (month + newMonth >= 12) {
			if (((year + 1) % 4 == 0 && (year + 1) % 100 != 0)||(year + 1) % 400 == 0) {
				sum += 366 + newDay;
				for(int i = 0;i < newMonth - 12;i++) {
					sum += monthday1[month + i];
				}
			} else {
				sum += 365 + newDay;
				for(int i = 0;i < newMonth - 12;i++) {
					sum += monthday1[month + i];
				}
			}
		} else {
			for(int i = 0;i < newMonth;i++) {
				sum += monthday1[month + i];
			}
			sum += newDay;
		}
		return week[sum%7];
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入当前年份");
		int year = scanner.nextInt();
		System.out.println("请输入当前月份");
		int month = scanner.nextInt();
		System.out.println("请输入当前天数");
		int day = scanner.nextInt();
		System.out.println("请输入当前是星期几:以数字表示,如:星期天 为 0");
		int index = scanner.nextInt();
		System.out.println("今天是:" + year + "-" + month + "-" + day + "  " + week[index]);
		
		System.err.println("请输入相隔月份");
		int newMonth = scanner.nextInt();
		System.out.println("请输入剩余天数");
		int newDay = scanner.nextInt();
		
		System.out.println("经过" + newMonth + "月" + newDay + "天后,是" + distance(year,month,day,newMonth,newDay));
	}
}

4.有两个篮子,分别为A 和 B,篮子A里装有鸡蛋,篮子B里装有苹果,请用面向对象的思想实现两个篮子里的物品交换(请用代码实现)

//面向对象思想实现篮子物品交换
public class Demo5 {
	public static void main(String[] args) {
		//创建篮子
		Basket A = new Basket("A");
		Basket B = new Basket("B");
		
		//装载物品
		A.load("鸡蛋");
		B.load("苹果");
		
		//交换物品
		A.change(B);
		
		A.show();
		B.show();
	}
}

class Basket{
	public String name; //篮子名称
	private Goods goods; //篮子中所装物品
	
	public Basket(String name) {
		// TODO Auto-generated constructor stub
		this.name = name;
		System.out.println(name + "篮子被创建");
	}
	
	//装物品函数
	public void load(String name) {
		goods = new Goods(name);
		System.out.println(this.name + "装载了" + name + "物品");
	}
	
	public void change(Basket B) {
		System.out.println(this.name + " 和 " + B.name + "中的物品发生了交换");
		String tmp = this.goods.getName();
		this.goods.setName(B.goods.getName());
		B.goods.setName(tmp);
	}
	
	public void show() {
		System.out.println(this.name + "中有" + goods.getName() + "物品");
	}
}

class Goods{
 	private String name; //物品名称
 	
 	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Goods(String name) {
		// TODO Auto-generated constructor stub
 		this.name = name;
	}
}

5.请写出一个 J.U.C — ForkJoin 测试的 Example 继承 RecursiveTask

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;

public class ForkJoinTaskExample extends RecursiveTask<Integer>{
	
	public static final  int threshold = 2;
	private int start;
	private int end;
	
	public ForkJoinTaskExample(int start,int end) {
		// TODO Auto-generated constructor stub
		this.start = start;
		this.end = end;
	}
	
	@Override
	protected Integer compute() {
		// TODO Auto-generated method stub
		int sum = 0;
		boolean canCompute = (end - start) <= threshold;
		if (canCompute) {
			for(int i = start;i <= end;i++) {
				sum += i;
			}
		} else {
			int middle = (start + end)/2;
			ForkJoinTaskExample leftTask = new ForkJoinTaskExample(start, middle);
			ForkJoinTaskExample rightTask = new ForkJoinTaskExample(middle + 1, end);
			
			leftTask.fork();
			
			leftTask.fork();
			rightTask.fork();
			
			int leftResult = leftTask.join();
			int rightResult = rightTask.join();
			
			sum = leftResult + rightResult;
		}
		return sum;
	}
	
	public static void main(String[] args) {
		ForkJoinPool forkJoinPool = new ForkJoinPool();
		
		ForkJoinTaskExample task = new ForkJoinTaskExample(1, 100);
		
		Future<Integer> result = forkJoinPool.submit(task);
		
		try {
			System.out.println("result:" + result.get());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
发布了36 篇原创文章 · 获赞 25 · 访问量 5817
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览