《Java语言程序设计与数据结构》编程练习答案(第二十二章)(一)

本文主要是介绍《Java语言程序设计与数据结构》编程练习答案(第二十二章)(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《Java语言程序设计与数据结构》编程练习答案(第二十二章)(一)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

22.1

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter a string:");String xxx = input.nextLine();int start = 0;int end = 1;if (xxx.length() != 1) {int head = 0;int tail = 1;int maxLength = 1;while (tail < xxx.length()) {while(tail < xxx.length() && xxx.charAt(tail-1) < xxx.charAt(tail)){tail++;}if(tail - head > maxLength){maxLength = tail - head;start = head;end = tail;head = tail;tail++;}}for(int i=start;i<end;i++){System.out.print(xxx.charAt(i));}}else{System.out.println(xxx);}}
}

22.2

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter a string:");String xxx = input.nextLine();int[] max = new int[xxx.length()];String[] lcs = new String[xxx.length()];for(int i = 0;i < xxx.length();i++){max[i] = 1;lcs[i] = xxx.charAt(i)+"";}for(int i = 1;i < xxx.length();i++){for(int j=0;j<i;j++){if(xxx.charAt(i) > xxx.charAt(j) && max[i] < max[j]+1){max[i] = max[j] + 1;lcs[i] = lcs[j] + xxx.charAt(i);}}}int maxLen = 0;int maxIndex = 0;for(int i=0;i<xxx.length();i++){if(maxLen < max[i]){maxLen = max[i];maxIndex = i;}}System.out.println(lcs[maxIndex]);}
}

22.3

KMP

22.4

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter a string s1:");String s1 = input.nextLine();System.out.println("Enter a string s2:");String s2 = input.nextLine();int index = -1;for(int i=0;i <= s1.length()-s2.length();i++){boolean success = true;for(int j=0;j<s2.length();j++){if(s1.charAt(i+j)!=s2.charAt(j)){success = false;break;}}if(success){index = i;}}if(index != -1){System.out.println("Matched at index "+index);}else{System.out.println("Not match");}}
}

22.5

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);ArrayList<Integer> numbers = new ArrayList<>();System.out.println("Please enter a sequence ending with 0:");while(true){int tmp = input.nextInt();if(tmp == 0){break;}else{numbers.add(tmp);}}int maxLen = 0;int maxIndex = 0;int head = 0;while (head < numbers.size()-1){int tmpIndex = head;int len = 1;while(head < numbers.size()-1 && numbers.get(head).equals(numbers.get(head + 1))){len++;head++;}if(len > maxLen){maxLen = len;maxIndex = tmpIndex;}head++;}System.out.println("The longest same number sequence starts at index "+maxIndex+" with "+maxLen+" values of "+numbers.get(maxIndex));}
}

22.6

我爬了

这篇关于《Java语言程序设计与数据结构》编程练习答案(第二十二章)(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/766354

相关文章

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring