java笔试⼿写算法⾯试题⼤全含答案
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}
squidgame游戏下载};
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;
adodc控件如何使用}
}
return true;
excelrandom函数用法}
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 list = new ArrayList(); 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.(i) + " * ");
}
System.out.(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++) {
java常见笔试题
回弹模量计算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 = LowerCase();
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”));
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论