行业知识
【荣耀笔试题汇总】2024-05-09-荣耀春招笔试题-三语言题解(CPP/Python/Java)
大家好这里是清隆学长 ,一枚热爱算法的程序员
✨ 本系列打算持续跟新荣耀近期的春秋招笔试题汇总~
ACM银牌| 多次AK大厂笔试 | 编程一对一辅导
感谢大家的订阅➕ 和 喜欢
清隆这边最近正在收集近一年互联网各厂的笔试题汇总,如果有需要的小伙伴可以关注后私信一下 清隆领取,会在飞书进行同步的跟新。
文章目录
- 01.K小姐的数字游戏
- 题目描述
- 输入格式
- 输出格式
- 样例输入
- 样例输出
- 数据范围
- 题解
- 参考代码
- 02.K小姐的在线学习系统
- 题目描述
- 输入格式
- 输出格式
- 样例输入
- 样例输出
- 数据范围
- 题解
- 参考代码
- 03.元素圈的奇妙组合
- 题目描述
- 输入格式
- 输出格式
- 样例输入
- 样例输出
- 数据范围
- 题解
- 参考代码
- 写在最后
- 清隆这边最近正在收集近一年互联网各厂的笔试题汇总,如果有需要的小伙伴可以关注后私信一下 清隆领取,会在飞书进行同步的跟新。
01.K小姐的数字游戏
题目描述
K小姐非常喜欢玩数字游戏。这一次,她想要判断给定的数字序列是否满足特定的规则。
存在多个数字序列,每个数字序列由 N N N 个正整数组成,数字之间以空格隔开,并且这些数字都大于 0 0 0 小于 100 100 100。
规则如下:
- 如果相邻的数字是两位数和一位数交替出现,则该序列是合法的。
- 如果序列中第一个和最后一个数是两位数,且中间数字都是一位数,则序列是合法的。
- 如果序列中第一个和最后一个是一位数,且中间数字都是两位数,则序列是合法的。
输入格式
输入多个数字序列,每个数字序列独占一行。每个数字序列由 N N N 个正整数组成,所有整数都大于 0 0 0 小于 100 100 100,每个整数之间由空格分隔, 2 ≤ N ≤ 100 2 \leq N \leq 100 2≤N≤100。输入保证合法。
输出格式
校验多个数字序列是否合法,合法时返回 true,不合法时返回 false。校验结果用空格分开,行末无多余空格。
样例输入
1 23 3 42 3 5644 1 5 71 9 35
样例输出
true false
数据范围
2 ≤ N ≤ 100 2 \leq N \leq 100 2≤N≤100,序列中的数字均为大于 0 0 0 小于 100 100 100 的正整数。
题解
本题可以通过遍历序列,判断相邻数字的位数是否满足题目要求来解决。具体思路如下:
- 遍历整个序列,判断相邻数字的位数是否交替出现。如果出现相邻数字位数相同的情况,则将 flg[0] 置为 0 0 0,表示不满足规则 1 1 1。
- 统计序列中每个数字的位数,并使用 Counter 统计不同位数的数量。
- 判断序列的第一个和最后一个数字的位数是否相同,且该位数在序列中只出现了两次。如果满足条件,则将 flg[1] 置为 1 1 1,表示满足规则 2 2 2 或规则 3 3 3。
- 最后,如果 flg 中存在 1 1 1,则说明序列合法,返回 true;否则返回 false。
参考代码
- Python
from collections import Counterwhile True: try: s = list(map(str, input().split())) def check(s): n = len(s) flg = [1, 0] for i in range(n - 1): a, b = s[i], s[i + 1] if len(a) == len(b): flg[0] = 0 break sz = [len(val) for val in s] d = Counter(sz) if sz[0] == sz[-1] and d[sz[0]] == 2: flg[1] = 1 return max(flg) >= 1 print('true' if check(s) else 'false', end=' ') except: break- Java
import java.util.*;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String[] s = sc.nextLine().split(" "); boolean res = check(s); System.out.print(res ? "true " : "false "); } } private static boolean check(String[] s) { int n = s.length; int[] flg = {1, 0}; for (int i = 0; i = 1; }}- Cpp
#include #include #include #include using namespace std;bool check(vector& s) { int n = s.size(); int flg[2] = {1, 0}; for (int i = 0; i = 1;}int main() { string line; while (getline(cin, line)) { vector s; int pos = 0; while ((pos = line.find(" ")) != string::npos) { s.push_back(line.substr(0, pos)); line.erase(0, pos + 1); } s.push_back(line); bool res = check(s); cout } cur_time = 0 for time in times: cur_time += time minute = (cur_time - 1) // 60 page_counts[minute] = page_counts.get(minute, 0) + 1 if page_counts[minute] 4: return 0 return 1t = int(input())for _ in range(t): print(check_learning_time()) public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- 0) { int n = scanner.nextInt(); int[] times = new int[n]; for (int i = 0; i 4) { return 0; } } return 1; }}- Cpp
#include #include using namespace std;int checkTime() { int n; cin >> n; unordered_map pageCount; int curTime = 0; for (int i = 0; i > time; curTime += time; int minute = (curTime - 1) / 60; pageCount[minute]++; if (pageCount[minute] > 4) { return 0; } } return 1;}int main() { int t; cin >> t; while (t--) { cout ios::sync_with_stdio(false); cin.tie(0); int n, m; cin n >> m; vector f(m + 1, 0); f[0] = 1; for (int i = 0; i
- Cpp
- Cpp
- Java