想学习算法?Java笔试手写算法面试12题『含答案』整的明明白白

本文转载自:想学习算法?Java笔试手写算法面试12题『含答案』整的明明白白


前言:

我坚信,机会永远属于有准备的人,我们与其羡慕他人的成功,不如从此刻起,积累足够多的的知识和面试经验,为将来进入更好的工作做好充分的准备!

算法面试题+答案

1. 统计一篇英文文章单词个数。

public class WordCounting {
	public static void main(String[] args) {
		try(FileReader fr = new FileReader("a.txt")) {
			int counter = 0;
			Boolean state = false;
			int currentchar;
			while((currentchar= fr.read()) != -1) {
				if(currentchar== ' ' || currentchar == 'n'|| currentchar == 't' || currentchar == 'r') {
					state = false;
				} else if(!state) {
					state = true;
					counter++;
				}
			}
			System.out.println(counter);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}

补充:这个程序可能有很多种写法,这里选择的是Dennis M. Ritchie和Brian W. Kernighan老师在他们不朽的著作《The C Programming Language》中给出的代码,向两位老师致敬。下面的代码也是如此。

2. 输入年月日,计算该日期是这一年的第几天。

public class DayCounting {
	public static void main(String[] args) {
		int[][] data = {
		{31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
		{31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
		};
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入年月日(1980 11 28): ");
		int year = sc.nextint();
		int month = sc.nextint();
		int date = sc.nextint();
		int[] daysOfMonth = data[(year %
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值