1.朋友们的喜好
牛牛有一个name = [‘Niumei’, ‘YOLO’,‘Niu Ke Le’,‘Mona’]记录了他最好的朋友们的名字,请创建一个二维列表friends,使用append函数将name添加到friends的第一行。
假如Niumei最喜欢吃pizza,最喜欢数字3,YOLO最喜欢吃fish,最喜欢数字6,Niu Ke Le最喜欢吃potato,最喜欢数字0,Mona最喜欢吃beef,最喜欢数字3。
请再次创建一个列表food依次记录朋友们最喜欢吃的食物,并将创建好的列表使用append函数添加到friends的第二行;
然后再创建一个列表number依次记录朋友们最喜欢的数字,并将创建好的列表使用append函数添加到friends的第三行。
这样friends就是一个二维list,使用print函数直接打印这个二维list.
输入描述
无
输出描述
[[‘Niumei’, ‘YOLO’, ‘Niu Ke Le’, ‘Mona’], [‘pizza’, ‘fish’, ‘potato’,‘beef’], [3, 6, 0, 3]]
题解
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建三个列表,分别存储名字、食物和数字
List<string> name = Arrays.asList("Niumei", "YOLO", "Niu Ke Le", "Mona");
List<string> food = Arrays.asList("pizza", "fish", "potato", "beef");
List<integer> number = Arrays.asList(3, 6, 0, 3);
// 将上述三个列表放入一个列表中
List<list<?>> friends = Arrays.asList(name, food, number);
// 创建一个StringBuilder对象,用于拼接字符串
StringBuilder sb = new StringBuilder();
sb.append("[");
// 遍历friends列表
for (int i = 0; i < friends.size(); i++) {
List<!--?--> row = friends.get(i);
sb.append("[");
// 遍历当前行中的每个元素
for (int j = 0; j < row.size(); j++) {
Object element = row.get(j);
if (element instanceof String) {
// 如果元素是字符串,则在字符串两边添加单引号
sb.append("'").append(element).append("'");
} else {
// 如果元素不是字符串,则直接添加元素
sb.append(element);
}
// 如果不是最后一个元素,则在元素后添加逗号
if (j < row.size() - 1) {
sb.append(", ");
}
}
sb.append("]");
// 如果不是最后一行,则在行后添加逗号
if (i < friends.size() - 1) {
sb.append(", ");
}
}
sb.append("]");
// 输出拼接后的字符串
System.out.println(sb.toString());
}
}
2.小红走矩阵
小红拿到了一个字符矩阵,矩阵仅由’r’、‘e’、‘d’三种字符组成,她初始站在左上角,每次可以走到一个相邻的字符上(每个字符上、下、左、右最多4个相邻)。但有个限制,小红不能从’r’走到’d’,从’e’走到’r’,从’d’走到’e’,其他情况都能走。
小红想知道,从左上角走到右下角至少需要多少步?
输入描述
第一行输入两个正整数n和m,代表矩阵的行数和列数。
接下来的几行,每行输入一个长度为m的字符串,用来表示矩阵。
1≤n,m≤500
输出描述
如果小红无法到达右下角,则输出-1。
否则输出一个整数,代表小红的最少移动步数。
题解
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取矩阵的行数和列数
int n = scanner.nextInt();
int m = scanner.nextInt();
scanner.nextLine(); // 读取换行符
// 创建一个二维字符数组用于存储矩阵
char[][] matrix = new char[n][m];
// 读取矩阵的每一行并存储在二维数组中
for (int i = 0; i < n; i++) {
matrix[i] = scanner.nextLine().toCharArray();
}
// 如果矩阵只有一个元素,则直接输出0并结束程序
if (n == 1 && m == 1) {
System.out.println(0);
return;
}
// 调用bfs函数计算最短路径长度
int result = bfs(matrix);
// 输出结果
System.out.println(result);
}
static int bfs(char[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
// 创建一个与矩阵大小相同的二维布尔数组,用于记录哪些位置已被访问过
boolean[][] visited = new boolean[n][m];
// 创建一个队列用于存储待访问的位置
Queue<int[]> queue = new LinkedList<>();
// 定义四个方向的行列偏移量
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
// 将起点位置(0,0)及其步数0加入队列,并标记为已访问
queue.offer(new int[]{0, 0, 0});
visited[0][0] = true;
// 当队列不为空时,循环处理队列中的位置
while (!queue.isEmpty()) {
// 从队列中取出一个位置
int[] current = queue.poll();
int row = current[0];
int col = current[1];
int steps = current[2];
// 如果当前位置是终点位置,则返回步数
if (row == n - 1 && col == m - 1) {
return steps;
}
// 遍历四个方向
for (int[] dir : directions) {
// 计算新位置的行列坐标
int newRow = row + dir[0];
int newCol = col + dir[1];
// 如果新位置在矩阵范围内,且未被访问过,且从当前位置到新位置是有效的移动
if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < m
&& !visited[newRow][newCol] && isValidMove(matrix[row][col], matrix[newRow][newCol])) {
// 将新位置及其步数加1加入队列,并标记为已访问
queue.offer(new int[]{newRow, newCol, steps + 1});
visited[newRow][newCol] = true;
}
}
}
// 如果无法到达终点,则返回-1
return -1;
}
static boolean isValidMove(char current_char, char new_char) {
// 如果当前字符是'r'且新字符是'd',则返回false
if (current_char == 'r' && new_char == 'd') {
return false;
}
// 如果当前字符是'e'且新字符是'r',则返回false
if (current_char == 'e' && new_char == 'r') {
return false;
}
// 如果当前字符是'd'且新字符是'e',则返回false
if (current_char == 'd' && new_char == 'e') {
return false;
}
// 如果以上条件都不满足,则返回true
return true;
}
}
3.嘤嘤的长城easy
嘤嘤觉得长城很美,特别是它的锯齿,非常的优雅!
现在有一个数组,嘤嘤想把这个数组变成"长城",即对于"长城"中每一个元素左右两边的元素相等,并且与它不相等。例如{2,1,2,1,2},{1,9,1,9}是长城,{2,1,3,2,4},{1,1,4,5,1,4}则不是长城。
你每次可以将一个元素加一,请问最少需要几次操作?
输入描述
第一行输入一个整数n(1≤n≤2×105)代表数组的长度。
第二行输入n 个正整数ai(1≤ai≤109)表示数组的每一个元素。
输出描述
输出一个整数表示最少的操作次数。
题解
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // 创建 Scanner 对象用于读取输入
int n = sc.nextInt(); // 读取数组的长度 n
long[] arr = new long[n]; // 创建一个 long 类型的数组 arr,长度为 n
long evenMax = 0, oddMax = 0; // 初始化偶数下标最大值 evenMax 和奇数下标最大值 oddMax 为 0
// 循环遍历数组 arr
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong(); // 读取数组的第 i 个元素
if (i % 2 == 0) { // 如果 i 是偶数
evenMax = Math.max(evenMax, arr[i]); // 更新 evenMax 为当前偶数下标元素和原 evenMax 中的较大值
} else { // 如果 i 是奇数
oddMax = Math.max(oddMax, arr[i]); // 更新 oddMax 为当前奇数下标元素和原 oddMax 中的较大值
}
}
// 判断 evenMax 是否等于 oddMax
if (evenMax == oddMax) {
// 如果 evenMax 等于 oddMax,计算两种情况下的最小操作数,并输出较小值
System.out.println(Math.min(operation(arr, evenMax + 1, oddMax), operation(arr, evenMax, evenMax + 1)));
} else {
// 如果 evenMax 不等于 oddMax,计算将数组转换到目标形态所需的操作数,并输出
System.out.println(operation(arr, evenMax, oddMax));
}
}
public static long operation(long[] arr, long num1, long num2) {
long ans = 0; // 初始化操作数 ans 为 0
// 循环遍历数组 arr
for (int i = 0; i < arr.length; i++) {
// 根据下标 i 的奇偶性,将当前元素与相应的目标值求差,并加到操作数 ans 中
ans += (i % 2 == 0 ? num1 - arr[i] : num2 - arr[i]);
}
return ans; // 返回计算得到的总操作数
}
}