2023 猿辅导笔试 猿辅导笔试题 0917
笔试时间:2023年9月17日 秋招
第一题
题目:书籍分类
有一位特别有责任心的老师,为了给学生带来高质量的课堂,翻阅了非常多的书籍,最后终于挑出了最有价值的几类书,并写了一个类目录,每个类用一个文字母代表,比如m代表math。老师振臂高呼太棒啦,却一不小心把所有书籍散落在了地上,你能帮帮老师统计出每个类下有多少书籍吗?举例:老师统计的目录为[a,b,m]。散落在地上的书籍为a,a,m,m,b,d,d,x。则你应该统计的结果: a 2,b 1,m 2。
输入描述
第一行输入一个字符串,代表类目录,其中类目录的长度catalogueLen(3 < catalogueLen < 53)
第二行输入一个字符串,代表地上散落的书籍,其中书籍的长度bookLen(3 < bookLen < 10^5)
保证不会出现类目录重复的情况,如 [a,a,b]。
输出描述
输出可能有多行,每行两个,第一个代表种类,第二个代表此种类下有多少本书,
最后按种类进行字典序排序
样例输入
示例一:
[a,m,b]
a,a,m,m,b,d,d,x,x
示例二:
[a,z,b]
a,a,m,m,b,d,d,x,x
样例输出
示例一:
a 2
b 1
m 2
示例二:
a 2
b 1
z 0
参考题解
Python做比较简单,可用counter 计算出每类数的数量,然后对类目录进行排序,分别输出每类书的数量即可。
C++:[此代码未进行大量数据的测试,仅供参考]
#include <iostream> #include <string> #include <vector> #include <map> #include <algorithm> int main() { std::string input1, input2; std::getline(std::cin, input1); std::getline(std::cin, input2); input1 = input1.substr(1, input1.length() - 2); // Remove square brackets std::vector<std::string> a; size_t pos = 0; while ((pos = input1.find(',')) != std::string::npos) { a.push_back(input1.substr(0, pos)); input1.erase(0, pos + 1); } a.push_back(input1); std::map<std::string, int> mp; pos = 0; while ((pos = input2.find(',')) != std::string::npos) { std::string s = input2.substr(0, pos); mp[s]++; input2.erase(0, pos + 1); } mp[input2]++; for (const std::string& s : a) { std::cout << s << " " << mp[s] << std::endl; } return 0; }
Java:[此代码未进行大量数据的测试,仅供参考]
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input1 = scanner.nextLine(); String input2 = scanner.nextLine(); input1 = input1.substring(1, input1.length() - 1); // Remove square brackets String[] a = input1.split(","); Map<String, Integer> mp = new HashMap<>(); String[] input2Array = input2.split(","); for (String s : input2Array) { mp.put(s, mp.getOrDefault(s, 0) + 1); } for (String s : a) { System.out.println(s + " " + mp.getOrDefault(s, 0)); } } }
Python:[此代码未进行大量数据的测试,仅供参考]
from collections import Counter a = sorted(input()[1:-1].split(',')) mp = Counter(input() .split(',')) for s in a: print(s, mp.get(s, 0))
第二题
题目:有序下课最短时间
教室中间有一条走廊,出口如图,在走廊两旁有几排座位,同学们随意坐在不同的座位上,假设每个单位时间学生们可以移动距离为1,且同一时间同一个位置只能有一个同学。所有同学的下课出教室路径都是从座位先移动到走廊,然后走到门口即可。假设走廊是y轴,出口是(0,0)。
输入描述
第一行输入n代表学生数。(1 <= n <= 10000)
后面n行输入,每行两个数字,表示学生标(xi,yi),(-10000 <= x <= 10000, x !=0)(0 <= y<= 10000)
输出描述
输出最短时间t。
样例输入
示例一:
1
3 4
示例二:
3
3 4
-3 4
2 3
样例输出
示例一:
7
提示:先从座位走3到走廊,然后走4到出口。
示例二:
8
参考题解
C++:[此代码未进行大量数据的测试,仅供参考]
#include <iostream> #include <vector> #include <algorithm> int main() { int n; std::cin >> n; std::vector<int> a; for (int i = 0; i < n; ++i) { int x, y; std::cin >> x >> y; a.push_back(std::abs(x) + y); } std::sort(a.begin(), a.end()); int pre = -10; for (int i = 0; i < n; ++i) { if (a[i] <= pre) { a[i] = pre + 1; } pre = a[i]; } std::cout << pre << std::endl; return 0; }
Java:[此代码未进行大量数据的测试,仅供参考]
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i <
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2023秋招各大笔试题汇总,c++,java,python多种语言分析,解答。