每日一题 错选择 及 编程题 周总结
Posted 满眼*星辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每日一题 错选择 及 编程题 周总结相关的知识,希望对你有一定的参考价值。
目录
Week4
Day1
错选择
用不带头结点的单链表存储队列,其队头指针指向队头结点,队尾指针指向队尾结点,则在进行出队操作时()
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);
}
}
}
查找两个字符串a,b中的最长公共子串
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);
}
}
}
Day2
错选择
无
字符串反转
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);
}
}
}
Day3
错选择
有权值分别为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;
}
}
MP3光标位置
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);
}
}
}
}
Day5
错选择
在 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 (每日一题 错选择 及 编程题 周总结