1.汽水瓶(编程题)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
if(n==0){
break;
}
System.out.println(n/2);
}
}
}
2.查找两个字符串a,b中的最长公共子串(编程题)
import java.util.*;
import java.io.*;
public class Main {
public static String getMaxSubstr(String str1, String str2) {
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
int len1 = arr1.length;
int len2 = arr2.length;
//最长子串的起始位置
int start = 0;
//最长子串的长度
int maxLen = 0;
//多增加一行一列为辅助状态
int[][] maxSubLen = new int[len1 + 1][len2 + 1];
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
//如果第i个和第j个相等,则进行累加
if (arr1[i - 1] == arr2[j - 1]) {
maxSubLen[i][j] = maxSubLen[i - 1][j - 1] + 1;
//更新
if (maxLen < maxSubLen[i][j]) {
maxLen = maxSubLen[i][j];
start = i - maxLen;
}
}
}
}
return str1.substring(start, start + maxLen);
}
public static void main(String args[]) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str1;
String str2;
while ((str1 = reader.readLine()) != null) {
str2 = reader.readLine();
if (str1.length() < str2.length()) {
System.out.println(getMaxSubstr(str1, str2));
} else {
System.out.println(getMaxSubstr(str2, str1));
}
}
}
}
方法二
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str1 = sc.nextLine();
String str2 = sc.nextLine();
if (str1.length() > str2.length()) {
repeat(str2, str1);
} else {
repeat(str1, str2);
}
}
}
public static void repeat(String s1, String s2) {
int right = 1;
int len = Integer.MIN_VALUE;
int start = 0;
int end = 0;
for (int i = 0; i < s1.length(); i++) {
right = i + 1;
while (right < s1.length() + 1) {
String sub = s1.substring(i, right);
if (s2.contains(sub)) {
if (sub.length() > len) {
len = sub.length();
start = i;
end = right;
}
}
right++;
}
}
System.out.print(s1.substring(start, end));
}
}
3.字符串反转(编程题)
import java.util.*;
public class Solution {
/**
* 反转字符串
* @param str string字符串
* @return string字符串
*/
public String solve (String str) {
// write code here
String a = "";
char[] ch = str.toCharArray();
for (int i = ch.length-1; i >= 0; i--) {
a+=ch[i];
}
return a;
}
}
4.公共子串计算(编程题)
这道题就是上面第2题的拓展:
第二题求最长公共子串内容
这一题求最长公共子串长度
import java.util.*;
import java.io.*;
public class Main {
public static String getMaxSubstr(String str1, String str2) {
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
int len1 = arr1.length;
int len2 = arr2.length;
//最长子串的起始位置
int start = 0;
//最长子串的长度
int maxLen = 0;
//多增加一行一列为辅助状态
int[][] maxSubLen = new int[len1 + 1][len2 + 1];
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
//如果第i个和第j个相等,则进行累加
if (arr1[i - 1] == arr2[j - 1]) {
maxSubLen[i][j] = maxSubLen[i - 1][j - 1] + 1;
//更新
if (maxLen < maxSubLen[i][j]) {
maxLen = maxSubLen[i][j];
start = i - maxLen;
}
}
}
}
return str1.substring(start, start + maxLen);
}
public static void main(String args[]) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str1;
String str2;
while ((str1 = reader.readLine()) != null) {
str2 = reader.readLine();
if (str1.length() < str2.length()) {
System.out.println(getMaxSubstr(str1, str2).length());
} else {
System.out.println(getMaxSubstr(str2, str1).length());
}
}
}
}
5.共享栈原理
若栈采用顺序存储方式存储,现两栈共享空间V[1..m],top[i]代表第i个栈( i=1,2)栈顶,
栈1的底在v[1],栈2的底在V[m],则栈满的条件是( B)。
```java
A top[1]+top[2]=m
B top[1]+1=top[2]
C top[2]-top[1]|=0
D top[1]=top[2]
6.前序遍历、中序遍历和层序遍历
某二叉树的前序遍历序列与中序遍历序列相同,均为 ABCDEF ,
则按层次输出(同一层从左到右)的序列为( A )
A ABCDEF
B BCDEFA
C FEDCBA
D DEFABC
已知二叉树后序遍历序列是bfegcda,中序遍历序列是badefcg,它的前序遍历序列是(B)
A abcdefg
B abdcefg
C adbcfeg
D abecdfg
某完全二叉树按层次输出(同一层从左到右)的序列为 ABCDEFGH 。
该完全二叉树的前序序列为(A)
A ABDHECFG
B ABCDEFGH
C HDBEAFCG
D HDEBFGCA
7.解决散列法中出现冲突问题
解决散列法中出现冲突问题常采用的方法是()
A 数字分析法、除余法、平方取中法
B 数字分析法、除余法、线性探测法
C 数字分析法、线性探测法、多重散列法
D 线性探测法、多重散列法、链地址法
8.非线性数据结构、线性数据结构
以下数据结构中,(A)是非线性数据结构
A 树
B 字符串
C 队
D 栈
线性数据结构 | 非线性数据结构 |
---|---|
一维数组、队列、栈 | 树、图、多维数组 |
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119529.html