笔试 笔试 2025腾讯云智春季笔试真题 玦尘 2025-04-05 2025-04-06 本文为原创内容,转载请注明出处并附带原文链接。感谢您的尊重与支持!
1.牛牛水果店 题目描述
牛牛开了一家水果店,已知,一个水果恰好可切成 n 块(不论大小),也只能切成 n 块。一个顾客,他 / 她买一盒水果,要求是:这盒水果中的水果块数必须在闭区间 [l, r] 中。牛牛只按 “个” 卖水果,而不是按 “块” 卖水果,所以,如果整数个水果并不满足顾客要求,牛牛就不会卖给这位顾客;而如果存在整数个水果,使得这些水果切成的块数满足顾客要求,那么,牛牛希望你告诉他,牛牛最少需要切多少个水果,以及最多需要切多少个水果。
输入说明
本题为多组测试数据,第一行输入一个正整数 T (1 ≤ T ≤ 1000),代表测试数据的组数。 对于每组测试数据,一行输入三个正整数 n, l, r (1 ≤ n ≤ 100; 1 ≤ l ≤ r ≤ 1000) ,含义如题所述。
输出说明
对于每组测试数据,如果牛牛的卖水果规则不能满足顾客要求,则输出 -1,否则输出两个正整数,依次代表牛牛需要为这位顾客最少切多少个水果,最多切多少个水果。
输入:
5 2 6 9 3 7 8 1 6 6 9 233 965 10 996 996
输出:
3 4 -1 6 6 26 107 -1
参考答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.Scanner;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); while (n-- > 0 ) { int n = sc.nextInt(); int l = sc.nextInt(); int r = sc.nextInt(); int kMin = (l + n - 1 ) / n; int kMax = r / n; if (kMax < 1 || kMin > kMax) { System.out.println(-1 ); } else { System.out.println(kMin + " " + kMax); } } } }
2.含有子串 AcMer 的最小代价 题目描述
对于一个只包含英文字母的字符串,你可以更改其中的字符,不同的更改类型,代价如下:将一个字母改为另一个大小写相同的字母,花费为 5。将一个字母由大写改为小写或者由小写改为大写花费为 5。现在请你求出对于任给的一个只包含英文字母的字符串,使其中包含有子串 AcMer 的最小代价是多少
输入说明
在一行中给出一个只包含大小写英文字母的字符串 str ,5 ≤ |str| ≤ 200000 。
输出说明
在一行中输出最小的代价。
输入:
AcAer
输出:
5
说明:
AcAer -> AcMer
参考答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import java.util.Scanner;public class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); String s = sc.nextLine().trim(); char [] target = {'A' , 'c' , 'M' , 'e' , 'r' }; int minCost = Integer.MAX_VALUE; for (int i = 0 ; i <= s.length() - 5 ; i++) { int cost = 0 ; for (int j = 0 ; j < 5 ; j++) { cost += calculateCost(s.charAt(i + j), target[j]); } minCost = Math.min(minCost, cost); } System.out.println(minCost); } private static int calculateCost (char current, char target) { if (current == target) return 0 ; boolean currentUpper = Character.isUpperCase(current); boolean targetUpper = Character.isUpperCase(target); if (currentUpper == targetUpper) { return 5 ; } else if (Character.toLowerCase(current) == Character.toLowerCase(target)) { return 5 ; } else { return 10 ; } } }
3.牛牛与牛妹的生死反击战 题目描述
2200 年,牛牛与牛妹是唯二幸存的两位,他们现在正处于科技中心,身旁远程轰炸系统,屏幕上显示的,是他们的城市,这个城市可以粗略的看成是一个 (n x m) 的矩形,僵尸随机分布在城市中,僵尸与僵尸之间无视碰撞体积,即:同一个位置有可能存在多只僵尸。现在,牛牛与牛妹通过卫星系统,得到了僵尸分布图,以及每个位置上的僵尸数量。当僵尸的四周(上下左右)能产生连接的僵尸片时,它们就可以看成是一股僵尸浪潮,为了节省导弹数量,同一股僵尸浪潮只发射一枚导弹进行轰炸。牛牛准备操纵系统,牛妹在一旁进行规划指挥,他们想知道,一共需要发射多少枚导弹,以及最大的一股僵尸浪潮中一共有多少只僵尸。
输入说明
本题为多组测试数据,第一行输入一个正整数 T(1 ≤ T ≤ 1000),代表测试数据的组数。 对于每组测试数据,第一行输入两个正整数 n, m(1 ≤ n, m ≤ 100),代表城市矩形的行、列范围。第 2 到第 n+1行,每行输入 m 个整数,整数 0 代表该位置上没有僵尸,其余的正整数代表该位置上有相应数量的僵尸,每个位置上的僵尸数量不会超过 1000。
输出说明
对于每组测试数据,一行输出两个整数,依次代表所需要的导弹数量,以及最大的一股僵尸浪潮中的僵尸数量。
输入:
1 3 3 1 0 1 0 1 0 9 0 6
输出:
5 9
参考答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 import java.util.Scanner;public class Main { private static final int [][] dirs = {{-1 ,0 }, {1 ,0 }, {0 ,-1 }, {0 ,1 }}; public static void main (String[] args) { Scanner sc = new Scanner (System.in); int T = sc.nextInt(); while (T-- > 0 ) { int n = sc.nextInt(); int m = sc.nextInt(); int [][] grid = newint[n][m]; boolean [][] visited = new boolean [n][m]; for (int i = 0 ; i < n; i++) for (int j = 0 ; j < m; j++) grid[i][j] = sc.nextInt(); int count = 0 ; int maxSum = 0 ; for (int i = 0 ; i < n; i++) { for (int j = 0 ; j < m; j++) { if (grid[i][j] > 0 && !visited[i][j]) { int currentSum = dfs(grid, visited, i, j); maxSum = Math.max(maxSum, currentSum); count++; } } } System.out.println(count + " " + maxSum); } } private static int dfs (int [][] grid, boolean [][] visited, int x, int y) { visited[x][y] = true ; int sum = grid[x][y]; for (int [] dir : dirs) { int nx = x + dir[0 ]; int ny = y + dir[1 ]; if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0 ].length && !visited[nx][ny] && grid[nx][ny] > 0 ) { sum += dfs(grid, visited, nx, ny); } } return sum; } }