用不带头结点的单链表存储队列,其队头指针指向队头结点,队尾指针指向队尾结点,则在进行出队操作时()
A 仅修改队头指针
B 仅修改队尾指针
C 队头、队尾指针都可能要修改
D 队头、队尾指针都要修改
正确答案:C
平常如果队列有很多元素的时候,只需要改变队头的指针即可,但是如果是出的倒数第二个元素,则需要改变头指针和尾指针的指向关系,所以队头、队尾指针都可能要修改。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
if(n == 0) return;
int count = 0;
while (n > 2) {
int kong = n%3;
int drink = n/3;
count += drink;
n = kong + drink;
}
if(n == 2) count++;
System.out.println(count);
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str1 = scanner.nextLine();
String str2 = scanner.nextLine();
if(str1.length() > str2.length()) {
String tmp = str1;
str1 = str2;
str2 = tmp;
}
int[] res = new int[2];
int max = 0;
for (int i = 0; i < str1.length(); i++) {
int j = i+1;
String tmp = "";
while (true) {
if(j <= str1.length()) {
tmp = str1.substring(i,j);
}
if(j <= str1.length() && str2.contains(tmp)) {
j++;
}else {
if(j-1-i > max) {
res[0] = i;
res[1] = j-1;
max = j-1-i;
}
break;
}
}
}
String result = str1.substring(res[0],res[1]);
System.out.println(result);
}
}
}
无
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String n = scanner.nextLine();
char[] res = n.toCharArray();
int i = 0;
int j = res.length-1;
while(i < j) {
char tmp = res[i];
res[i] = res[j];
res[j] = tmp;
i++;
j--;
}
String result = String.valueOf(res);
System.out.println(result);
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str1 = scanner.nextLine();
String str2 = scanner.nextLine();
if(str1.length() > str2.length()) {
String tmp = str1;
str1 = str2;
str2 = tmp;
}
int max = 0;
for (int i = 0; i < str1.length(); i++) {
int j = i+1;
String tmp = "";
while (true) {
if(j <= str1.length()) {
tmp = str1.substring(i,j);
}
if(j <= str1.length() && str2.contains(tmp)) {
j++;
}else {
if(j-1-i > max) {
max = j-1-i;
}
break;
}
}
}
System.out.println(max);
}
}
}
有权值分别为11,8,6,2,5的叶子结点生成一棵哈夫曼树,它的带权路径长度为_______。
A 24
B 71
C 48
D 53
正确答案:B
哈夫曼树定义:当用 n 个结点(都做叶子结点且都有各自的权值)试图构建一棵树时,如果构建的这棵树的带权路径长度最小,称这棵树为“最优二叉树”,有时也叫“赫夫曼树”或者“哈夫曼树”
结点的权:给每一个结点赋予一个新的数值,被称为这个结点的权。例如,图 1 中结点 a 的权为 7,结点 b 的权为 5。
结点的带权路径长度:指的是从根结点到该结点之间的路径长度(深度)与该结点的权的乘积。例如,图 1 中结点 b 的带权路径长度为 2 * 5 = 10 。
树的带权路径长度为树中所有叶子结点的带权路径长度之和。通常记作 “WPL” 。例如图 1 中所示的这颗树的带权路径长度为:
WPL = 7 * 1 + 5 * 2 + 2 * 3 + 4 * 3
思路 : 要让自己构造最小的带权路径,这里有5个叶子节点,就画一个5个叶子节点的二叉树,要保证带权路径最小,那就把权值大的节点放在深度低的层,权值小的节点放深度高的层,保证所有带权路径最小。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int k = scanner.nextInt();
int[] arr = new int[2*n];for (int j = 0; j < arr.length; j++) {
arr[j] = scanner.nextInt();
}
for (int j = 0; j < k; j++) {
arr = wash(arr);
}
for (int j = 0; j < arr.length; j++) {
if(j == arr.length-1) {
System.out.println(arr[j]);
continue;
}
System.out.print(arr[j] + " ");
}
}
}
public static int[] wash(int[] arr) {
int i = 0;
int j = arr.length/2;
int[] res = new int[arr.length];
int index = 0;
while (j < arr.length) {
res[index] = arr[i];
index++;
res[index] = arr[j];
index++;
i++;
j++;
}
arr = res;
return arr;
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int count = scanner.nextInt();
scanner.nextLine();
String use = scanner.nextLine();
int choice = 1;
int pageS = 1;
int pageE = 4;
if (count <= 4) {
char[] useing = use.toCharArray();
for (int i = 0; i < useing.length; i++) {
if(useing[i] == 'U') {
if (choice == 1) {
choice = count;
}else {
choice--;
}
}else {
if(choice == count) {
choice = 1;
}else {
choice++;
}
}
}
for (int i = 0; i < count; i++) {
if(i == count-1) {
System.out.println(i+1);
continue;
}
System.out.print(i+1 + " ");
}
System.out.println(choice);
}else {
char[] useing = use.toCharArray();
for (int i = 0; i < useing.length; i++) {
if(useing[i] == 'U') {
if (choice == 1) {
choice = count;
pageE = count;
pageS = pageE - 3;
}else if (choice > pageS) {
choice--;
}else {
choice--;
pageE--;
pageS--;
}
}else {
if(choice == count) {
choice = 1;
pageS = 1;
pageE = pageS+3;
}else if (choice < pageE){
choice++;
}else {
choice++;
pageE++;
pageS++;
}
}
}
for (int i = pageS-1; i <= pageE-1; i++) {
if(i == pageE-1) {
System.out.println(i+1);
continue;
}
System.out.print(i+1 + " ");
}
System.out.println(choice);
}
}
}
}
在 Internet 中实现信息浏览查询服务的是( )
A DNS
B FTP
C WWW
D ADSL
正确答案:C
DNS:域名解析
FTP:文件传输
www:信息查询
ADSL:非对称数字用户线路,数据传输
import java.util.*;
public class Gift {
public int getValue(int[] gifts, int n) {
Arrays.sort(gifts);
int i = 0;
int j = n/2+1;
while(j < n) {
if(gifts[i] == gifts[j]) {
return gifts[i];
}
i++;
j++;
}
return 0;
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str1 = scanner.nextLine();
String str2 = scanner.nextLine();
int res = calStringDistance(str1,str2); //调用求最小步数函数
System.out.println(res);
}
}
//计算最小步数函数
public static int calStringDistance(String str1, String str2) {
//定义dp数组,行多加一行,列多加一列,因为存放初始状态,用来辅助比较
int[][] step = new int[str1.length()+1][str2.length()+1];
//初始列:F(i,0) = i(删除操作)
for (int i = 0; i < step.length; i++) {
step[i][0] = i;
}
//初始行:F(0,j) = j (插入操作)
for (int i = 0; i < step[0].length; i++) {
step[0][i] = i;
}
//开始转换DP动态规划,遍历二维数组
for (int i = 1; i < step.length; i++) {
for (int j = 1; j < step[0].length; j++) {
//如果A[i] == B[j],则不用增加步数,等于上一步 F(i-1,j-1)即可
//如果A[i] != B[j],则需要比较三种操作中最小的步数,F(i,j) = min (F(i,j-1) , F(i-1,j) , F(i-1,j-1)) + 1
if (str1.charAt(i-1) == str2.charAt(j-1)) {
step[i][j] = step[i-1][j-1];
}else {
step[i][j] = Math.min(Math.min(step[i - 1][j], step[i - 1][j - 1]), step[i][j - 1]) + 1;
}
}
}
//返回二维数组DP的右下角,即遍历完两个字符串的最小步数
return step[step.length - 1][step[0].length - 1];
}
}
若一棵二叉树具有12个度为2的结点,6个度为1的结点,则度为0的结点个数是()。
A 10
B 11
C 13
D 不确定
正确答案:C
按节点个数角度考虑:总结点N=N0+N1+N2;
按度数考虑,总度数为节点数-1(除了根节点外,每个节点头上吊着一个度数):N-1=0N0+1N1+2*N2;
两式相减:得出N0=N2+1;
已知小根堆为8,15,10,21,34,16,12,删除关键字 8 之后需重建堆,在此过程中,关键字之间的比较次数是() 。
A 1
B 2
C 3
D 4
正确答案:C
设有向图G=(V,E),顶点集 V={V0,V1,V2,V3},边集 E={
, , , }。若从顶点 V0 开始对图进行深度优先遍历,则可能得到的不同
遍历序列个数是 () 。
A 2
B 3
C 4
D 5
正确答案: D
import java.util.*;
public class Bonus {
public int getMost(int[][] board) {
int[][] dp = new int[board.length][board[0].length];
for(int i = 0; i < dp.length; i++) {
for(int j = 0; j < dp[0].length; j++) {
if(i == 0 && j == 0) {
dp[i][j] = board[i][j];
}else if(i == 0) {
dp[i][j] = dp[i][j-1] + board[i][j];
}else if(j == 0) {
dp[i][j] = dp[i-1][j] + board[i][j];
}else {
dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]) + board[i][j];
}
}
}
return dp[board.length-1][board[0].length-1];
}
}
import java.util.*;
public class Main {
public static int min = Integer.MAX_VALUE; //到达长点最小步骤
public static List<int[]> stack = null; //用链表代替栈的功能
public static List<List<int[]>> list = null; //链表存放能到达终点的所有路径
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] map = new int[n][m];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
map[i][j] = scanner.nextInt();
}
}
boolean[][] isUsed = new boolean[n][m]; //判断该节点是否走过
int count = 1; //计走的步数
stack = new ArrayList<>(); //实例化
list = new ArrayList<>(); //实例化
dfs(0,0,map,isUsed,count); //调用函数,深度优先遍历
int minStep = Integer.MAX_VALUE; //最小步数
List<int[]> res = null; //最小步数的所有节点
for (int i = 0; i < list.size(); i++) {
if (list.get(i).size() < minStep) {
minStep = list.get(i).size();
res = list.get(i);
}
}
//打印所有步数
for (int i = 0; i < res.size(); i++) {
System.out.println("(" + res.get(i)[0] + "," + res.get(i)[1] + ")");
}
}
}
//回溯法
public static void dfs(int i, int j, int[][] map, boolean[][] isUsed,int count) {
stack.add(new int[]{
i,j}); //每次递归进来都给栈加i,j位置
//如果到达了终点,则把之前的所有步位置放到list保存
if (i == map.length-1 && j == map[0].length-1) {
List<int[]> res = new ArrayList<>(stack);
list.add(res);
if (count < min) {
min = count;
}
return; //这里回溯,因为有可能不是最小步数
}
isUsed[i][j] = true; //该位置变为走过
//分别在上下左右进行递归,下一步的位置不能越界,不能走过,不能是墙
//上
if (i-1 < map.length && i-1 >= 0 && !isUsed[i-1][j] && map[i-1][j] == 0) {
dfs(i-1,j,map,isUsed,count+1);
}
//下
if (i + 1 < map.length && !isUsed[i + 1][j] && map[i+1][j] == 0) {
dfs(i+1,j,map,isUsed,count+1);
}
//左
if (j-1 < map[0].length && j-1 >= 0 && !isUsed[i][j-1] && map[i][j-1] == 0) {
dfs(i,j-1,map,isUsed,count+1);
}
//右
if (j + 1 < map[0].length && !isUsed[i][j + 1] && map[i][j+1] == 0) {
dfs(i,j+1,map,isUsed,count+1);
}
isUsed[i][j] = false; //回溯后要给走过位置置为为走过
stack.remove(stack.size()-1); //回溯后要给出栈
}
}