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 % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0]; int sum = 0; for(int i = 0; i < month -1; i++) { sum += daysofmonth[i]; } sum += date; system.out.println(sum); sc.close(); }}
3.回文素数:所谓回文数就是顺着读和倒着读一样的数(例如:11,121,1991…),回文素数就是既是回文数又是素数(只能被1和自身整除的数)的数。编程找出11~9999之间的回文素数。
public class palindromicprimenumber { public static void main(string[] args) { for(int i = 11; i <= 9999; i++) { if(isprime(i) && ispalindromic(i)) { system.out.println(i); } } } public static boolean isprime(int n) { for(int i = 2; i <= math.sqrt(n); i++) { if(n % i == 0) { return false; } } return true; } public static boolean ispalindromic(int n) { int temp = n; int sum = 0; while(temp > 0) { sum= sum * 10 + temp % 10; temp/= 10; } return sum == n; }}
4.全排列:给出五个数字12345的所有排列。
public class fullpermutation { public static void perm(int[] list) { perm(list,0); } private static void perm(int[] list, int k) { if (k == list.length) { for (int i = 0; i < list.length; i++) { system.out.print(list[i]); } system.out.println(); }else{ for (int i = k; i < list.length; i++) { swap(list, k, i); perm(list, k + 1); swap(list, k, i); } } } private static void swap(int[] list, int pos1, int pos2) { int temp = list[pos1]; list[pos1] = list[pos2]; list[pos2] = temp; } public static void main(string[] args) { int[] x = {1, 2, 3, 4, 5}; perm(x); }}
5.对于一个有n个整数元素的一维数组,找出它的子数组(数组中下标连续的元素组成的数组)之和的最大值。
下面给出几个例子(最大子数组用粗体表示):
数组:{ 1, -2, 3,5, -3, 2 },结果是:8
2) 数组:{ 0, -2, 3, 5, -1, 2 },结果是:9
3) 数组:{ -9, -2,-3, -5, -3 },结果是:-2
可以使用动态规划的思想求解:
public class maxsum { private static int max(int x, int y) { return x > y? x: y; } public static int maxsum(int[] array) { int n = array.length; int[] start = new int[n]; int[] all = new int[n]; all[n - 1] = start[n - 1] = array[n - 1]; for(int i = n - 2; i >= 0;i--) { start[i] = max(array[i], array[i] + start[i + 1]); all[i] = max(start[i], all[i + 1]); } return all[0]; } public static void main(string[] args) { int[] x1 = { 1, -2, 3, 5,-3, 2 }; int[] x2 = { 0, -2, 3, 5,-1, 2 }; int[] x3 = { -9, -2, -3,-5, -3 }; system.out.println(maxsum(x1)); // 8 system.out.println(maxsum(x2)); // 9 system.out.println(maxsum(x3)); //-2 }}
6.用递归实现字符串倒转
public class stringreverse { public static string reverse(string originstr) { if(originstr == null || originstr.length()== 1) { return originstr; } return reverse(originstr.substring(1))+ originstr.charat(0); } public static void main(string[] args) { system.out.println(reverse("hello")); }}
7.输入一个正整数,将其分解为素数的乘积。
public class decomposeinteger { private static list<integer> list = new arraylist<integer>(); public static void main(string[] args) { system.out.print("请输入一个数: "); scanner sc = new scanner(system.in); int n = sc.nextint(); decomposenumber(n); system.out.print(n + " = "); for(int i = 0; i < list.size() - 1; i++) { system.out.print(list.get(i) + " * "); } system.out.println(list.get(list.size() - 1)); } public static void decomposenumber(int n) { if(isprime(n)) { list.add(n); list.add(1); } else { doit(n, (int)math.sqrt(n)); } } public static void doit(int n, int div) { if(isprime(div) && n % div == 0) { list.add(div); decomposenumber(n / div); } else { doit(n, div - 1); } } public static boolean isprime(int n) { for(int i = 2; i <= math.sqrt(n);i++) { if(n % i == 0) { return false; } } return true; }}
8、一个有n级的台阶,一次可以走1级、2级或3级,问走完n级台阶有多少种走法。
public class gosteps { public static int countways(int n) { if(n < 0) { return 0; } else if(n == 0) { return 1; } else { return countways(n - 1) + countways(n - 2) + countways(n -3); } } public static void main(string[] args) { system.out.println(countways(5)); // 13 }}
9.写一个算法判断一个英文单词的所有字母是否全都不同(不区分大小写)
public class allnotthesame { public static boolean judge(string str) { string temp = str.tolowercase(); int[] lettercounter = new int[26]; for(int i = 0; i <temp.length(); i++) { int index = temp.charat(i)- 'a'; lettercounter[index]++; if(lettercounter[index] > 1) { return false; } } return true; } public static void main(string[] args) { system.out.println(judge("hello")); system.out.print(judge("smile")); }}
10.有一个已经排好序的整数数组,其中存在重复元素,请将重复元素删除掉,例如,a= [1, 1, 2, 2, 3],处理之后的数组应当为a= [1, 2, 3]。
public class removeduplication { public static int[] removeduplicates(int a[]) { if(a.length <= 1) { return a; } int index = 0; for(int i = 1; i < a.length; i++) { if(a[index] != a[i]) { a[++index] = a[i]; } } int[] b = new int[index + 1]; system.arraycopy(a, 0, b, 0, b.length); return b; } public static void main(string[] args) { int[] a = {1, 1, 2, 2, 3}; a = removeduplicates(a); system.out.println(arrays.tostring(a)); }}
11.给一个数组,其中有一个重复元素占半数以上,找出这个元素。
public class findmost { public static <t> t find(t[] x){ t temp = null; for(int i = 0, ntimes = 0; i< x.length;i++) { if(ntimes == 0) { temp= x[i]; ntimes= 1; } else { if(x[i].equals(temp)) { ntimes++; } else { ntimes--; } } } return temp; } public static void main(string[] args) { string[]strs = {"hello","kiss","hello","hello","maybe"}; system.out.println(find(strs)); }}
12.编写一个方法求一个字符串的字节长度?
public int getwordcount(string s){ int length = 0; for(int i = 0; i < s.length(); i++) { int ascii = character.codepointat(s, i); if(ascii >= 0 && ascii <=255) length++; else length += 2; } return length;}
以上就是java笔试手写算法面试题大全含答案的详细内容。
