热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java手写算法_java笔试手写算法面试题大全含答案

1.统计一篇英文文章单词个数。publicclassWordCounting{publicstaticvoidmain(String[]args){try(FileReaderfr

e6a690e1742068f04e105f57c0396001.png

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 % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0];

int sum = 0;

for(int i = 0; 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 &#61; 11; i <&#61; 9999; i&#43;&#43;) {

if(isPrime(i) && isPalindromic(i)) {

System.out.println(i);

}

}

}

public static boolean isPrime(int n) {

for(int i &#61; 2; i <&#61; Math.sqrt(n); i&#43;&#43;) {

if(n % i &#61;&#61; 0) {

return false;

}

}

return true;

}

public static boolean isPalindromic(int n) {

int temp &#61; n;

int sum &#61; 0;

while(temp > 0) {

sum&#61; sum * 10 &#43; temp % 10;

temp/&#61; 10;

}

return sum &#61;&#61; n;

}

}

4.全排列&#xff1a;给出五个数字12345的所有排列。public class FullPermutation {

public static void perm(int[] list) {

perm(list,0);

}

private static void perm(int[] list, int k) {

if (k &#61;&#61; list.length) {

for (int i &#61; 0; i

System.out.print(list[i]);

}

System.out.println();

}else{

for (int i &#61; k; i

swap(list, k, i);

perm(list, k &#43; 1);

swap(list, k, i);

}

}

}

private static void swap(int[] list, int pos1, int pos2) {

int temp &#61; list[pos1];

list[pos1] &#61; list[pos2];

list[pos2] &#61; temp;

}

public static void main(String[] args) {

int[] x &#61; {1, 2, 3, 4, 5};

perm(x);

}

}

5.对于一个有N个整数元素的一维数组&#xff0c;找出它的子数组(数组中下标连续的元素组成的数组)之和的最大值。

下面给出几个例子(最大子数组用粗体表示)&#xff1a;

数组&#xff1a;{ 1, -2, 3,5, -3, 2 }&#xff0c;结果是&#xff1a;8

2) 数组&#xff1a;{ 0, -2, 3, 5, -1, 2 }&#xff0c;结果是&#xff1a;9

3) 数组&#xff1a;{ -9, -2,-3, -5, -3 }&#xff0c;结果是&#xff1a;-2

可以使用动态规划的思想求解&#xff1a;public class MaxSum {

private static int max(int x, int y) {

return x > y? x: y;

}

public static int maxSum(int[] array) {

int n &#61; array.length;

int[] start &#61; new int[n];

int[] all &#61; new int[n];

all[n - 1] &#61; start[n - 1] &#61; array[n - 1];

for(int i &#61; n - 2; i >&#61; 0;i--) {

start[i] &#61; max(array[i], array[i] &#43; start[i &#43; 1]);

all[i] &#61; max(start[i], all[i &#43; 1]);

}

return all[0];

}

public static void main(String[] args) {

int[] x1 &#61; { 1, -2, 3, 5,-3, 2 };

int[] x2 &#61; { 0, -2, 3, 5,-1, 2 };

int[] x3 &#61; { -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 &#61;&#61; null || originStr.length()&#61;&#61; 1) {

return originStr;

}

return reverse(originStr.substring(1))&#43; originStr.charAt(0);

}

public static void main(String[] args) {

System.out.println(reverse("hello"));

}

}

7.输入一个正整数&#xff0c;将其分解为素数的乘积。public class DecomposeInteger {

private static List list &#61; new ArrayList();

public static void main(String[] args) {

System.out.print("请输入一个数: ");

Scanner sc &#61; new Scanner(System.in);

int n &#61; sc.nextInt();

decomposeNumber(n);

System.out.print(n &#43; " &#61; ");

for(int i &#61; 0; i

System.out.print(list.get(i) &#43; " * ");

}

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 &#61;&#61; 0) {

list.add(div);

decomposeNumber(n / div);

}

else {

doIt(n, div - 1);

}

}

public static boolean isPrime(int n) {

for(int i &#61; 2; i <&#61; Math.sqrt(n);i&#43;&#43;) {

if(n % i &#61;&#61; 0) {

return false;

}

}

return true;

}

}

8、一个有n级的台阶&#xff0c;一次可以走1级、2级或3级&#xff0c;问走完n级台阶有多少种走法。public class GoSteps {

public static int countWays(int n) {

if(n <0) {

return 0;

}

else if(n &#61;&#61; 0) {

return 1;

}

else {

return countWays(n - 1) &#43; countWays(n - 2) &#43; 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 &#61; str.toLowerCase();

int[] letterCounter &#61; new int[26];

for(int i &#61; 0; i

int index &#61; temp.charAt(i)- &#39;a&#39;;

letterCounter[index]&#43;&#43;;

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.有一个已经排好序的整数数组&#xff0c;其中存在重复元素&#xff0c;请将重复元素删除掉&#xff0c;例如&#xff0c;A&#61; [1, 1, 2, 2, 3]&#xff0c;处理之后的数组应当为A&#61; [1, 2, 3]。public class RemoveDuplication {

public static int[] removeDuplicates(int a[]) {

if(a.length <&#61; 1) {

return a;

}

int index &#61; 0;

for(int i &#61; 1; i

if(a[index] !&#61; a[i]) {

a[&#43;&#43;index] &#61; a[i];

}

}

int[] b &#61; new int[index &#43; 1];

System.arraycopy(a, 0, b, 0, b.length);

return b;

}

public static void main(String[] args) {

int[] a &#61; {1, 1, 2, 2, 3};

a &#61; removeDuplicates(a);

System.out.println(Arrays.toString(a));

}

}

11.给一个数组&#xff0c;其中有一个重复元素占半数以上&#xff0c;找出这个元素。public class FindMost {

public static T find(T[] x){

T temp &#61; null;

for(int i &#61; 0, nTimes &#61; 0; i

if(nTimes &#61;&#61; 0) {

temp&#61; x[i];

nTimes&#61; 1;

}

else {

if(x[i].equals(temp)) {

nTimes&#43;&#43;;

}

else {

nTimes--;

}

}

}

return temp;

}

public static void main(String[] args) {

String[]strs &#61; {"hello","kiss","hello","hello","maybe"};

System.out.println(find(strs));

}

}

12.编写一个方法求一个字符串的字节长度?public int getWordCount(String s){

int length &#61; 0;

for(int i &#61; 0; i

{

int ascii &#61; Character.codePointAt(s, i);

if(ascii >&#61; 0 && ascii <&#61;255)

length&#43;&#43;;

else

length &#43;&#61; 2;

}

return length;

}



推荐阅读
  • C语言常量与变量的深入理解及其影响
    本文深入讲解了C语言中常量与变量的概念及其深入实质,强调了对常量和变量的理解对于学习指针等后续内容的重要性。详细介绍了常量的分类和特点,以及变量的定义和分类。同时指出了常量和变量在程序中的作用及其对内存空间的影响,类似于const关键字的只读属性。此外,还提及了常量和变量在实际应用中可能出现的问题,如段错误和野指针。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • 《2017年3月全国计算机等级考试二级C语言上机题库完全版》由会员分享,可在线阅读,更多相关《2017年3月全国计算机等级考试二级C语言上机题库完全版( ... [详细]
  • 查找给定字符串的所有不同回文子字符串原文:https://www ... [详细]
author-avatar
手机用户2502937541
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有