笔试笔试2024京东春季笔试真题
玦尘本文为原创内容,转载请注明出处并附带原文链接。感谢您的尊重与支持!
1.藻类的总重量
题目描述
我们用xi表示第i年年初池塘中藻类植物的总重量,那么池塘中藻类植物的发展满足这个规律xi+1 =r * xi - d。现在给你r, d, x2024,请你计算未来十年里每年年初池塘中藻类植物的总重量。
输入描述
在一行中给出三个正整数r, d, x2024,含义如题所示。
2 ≤ r ≤ 5;
1 ≤ d ≤ 100;
d ≤ x2024 ≤ 200。
输出描述
共输出10行,依次为x2025, x2026, … , x2034
输入示例
2 10 20
输出示例
30
50
90
170
330
650
1290
2570
5130
10250
提示信息
时间限制:c/c++/go:1s;其他语言:3s。
参考答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.util.Scanner;
public class Question1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int r = sc.nextInt(); int d = sc.nextInt(); int x1 = sc.nextInt(); long cur = x1; int count = 10; while (count-- > 0) { cur = cur * r - d; System.out.println(cur); } } }
|
2.吃豆人游戏
题目描述
有一款叫做吃豆人(Pacman)的游戏有许多粉丝,这些粉丝只要看到看到任何包含”pacman”作为子串的字符串就会变得非常激动。现在你有一个长度为n的字符出s,你每次可以将其中一个字母换为另外一个字母,请问你最少需要替换多少次才能使其不含有”pacman”作为子串?
输入描述
输入一个仅包含小写字母的字符串。
1 <= s.length <= 100000。
输出描述
输出一个整数,表示最小的操作次数。
输入示例
pacman
输出示例
1
提示信息
通过把第一个p换成a即可。也有其他的换法,但至少需要一次。
时间限制:c/c++/go:1s;其他语言:3s。
参考答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.util.Scanner;
public class Question2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String x = sc.nextLine(); int res = 0; for(int i = 0;i <= x.length() - 6;){ if(x.substring(i,i+6).equals("pacman")){ res++; i += 6; }else { i++; } } System.out.println(res); } }
|
3.平衡子串的长度
题目描述
给出一个仅由大写字母”AB”构成的字符串s,请你求出s中包含’A’和’B’个数相同的连续区间的最长长度。
输入描述
输入的第一行给出字符串s。
1 ≤ s.length ≤ 200000
输出描述
输出s中包含’A’和’B’个数相同的连续区间的最长长度。
输入示例
AAAAAA
输出示例
0
提示信息
时间限制:c/c++/go:1s;其他语言:3s。
参考答案:
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
| import java.util.HashMap; import java.util.Map; import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int maxLen = 0; int currentSum = 0; Map<Integer, Integer> sumMap = new HashMap<>(); sumMap.put(0, -1);
for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'A') { currentSum += 1; } else { currentSum -= 1; } if (sumMap.containsKey(currentSum)) { maxLen = Math.max(maxLen, i - sumMap.get(currentSum)); } else { sumMap.put(currentSum, i); } } System.out.println(maxLen); } }
|