携程笔试 携程笔试题 0327
笔试时间:2025年03月27日
历史笔试传送门:
第一题
题目:回文时间
给你一个包含小时数和分钟数的时间,让你求出从当前开始到达最早的回文时间需要经过多少分钟 我们将分钟数直接拼接到小时数后面,如果这个数字正反都是一样的,那么称这个时间为回文时间,例如13:31就是一个回文时间,因为拼接得到的数字1331正反都是一样的。
输入描述
在一行上输入五个字符代表一个时间,除了中间字符为冒号外,其余字符都是数字,格式形如hh:mm,其中hh表示时,mm表示分。
如13:15,表示13点15分。
时间采取24小时制,保证时间均为合法时间。
输出描述
每组输出占一行,包含一个整数,表示答案
样例输入
13:29
样例输出
2
参考题解
模拟
C++:[此代码未进行大量数据的测试,仅供参考]
#include <iostream> #include <iomanip> #include <string> using namespace std; bool isPalindrome(int h, int m) { string time = (h < 10 ? "0" : "") + to_string(h) + (m < 10 ? "0" : "") + to_string(m); return time == string(time.rbegin(), time.rend()); } int main() { string s; cin >> s; int h = stoi(s.substr(0, 2)); int m = stoi(s.substr(3, 2)); if (isPalindrome(h, m)) { cout << 0 << endl; return 0; } int count = 0; while (true) { m++; count++; if (m == 60) { m = 0; h++; } if (h == 24) { h = 0; } if (isPalindrome(h, m)) { cout << count << endl; break; } } return 0; }
Java:[此代码未进行大量数据的测试,仅供参考]
import java.util.Scanner; public class PalindromeTime { public static boolean isPalindrome(int h, int m) { String time = String.format("%02d%02d", h, m); String reversed = new StringBuilder(time).reverse().toString(); return time.equals(reversed); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); int h = Integer.parseInt(s.substring(0, 2)); int m = Integer.parseInt(s.substring(3, 5)); if (isPalindrome(h, m)) { System.out.println(0); return; } int count = 0; while (true) { m++; count++; if (m == 60) { m = 0; h++; } if (h == 24) { h = 0; } if (isPalindrome(h, m)) { System.out.println(count); break; } } } }
Python:[此代码未进行大量数据的测试,仅供参考]
s = input().strip() h, m = map(int, s.split(':')) current_h, current_m = h, m # 检查初始时间是否是回文 time_str = f"{current_h:02}{current_m:02}" if time_str == time_str[::-1]: print(0) else: count = 0 whileTrue: current_m += 1 count += 1 # 处理进位 if current_m >= 60: current_m -= 60 current_h += 1 if current_h >= 24: current_h -= 24 # 检查回文 time_str = f"{current_h:02}{current_m:02}" if time_str == time_str[::-1]: print(count) break
第二题
题目:披萨餐厅
在游游的披萨餐厅有很多机械人偶,其中有一只负责送餐的机械人偶,会记录自己的送餐情况。送餐情况可以看作是一个合法的出入栈序列{ai}。如果ai>0,则认为元素ai入栈,即机械人偶拿到餐品;如果ai<0,则认为元素-ai出栈,即机械人偶放下餐品。栈初始是空的,按照序列进行出入栈操作后,栈也是空的。注意,合法的出入栈序列中,任意的两个元素先入栈的必然会晚出栈。不过,这名机械人偶最近有了一些"心事”,不小心把序列中某两个相邻的元素交换了一下,变成了子序列{bi}!它不想被游游抛弃掉,需要你来帮它恢复一下出入栈序列。即请你给出一对相邻的元素的位置,保证根据你的指示交换后,出入栈序列合法即可。
输入描述
第一行,一个正偶数n(2<=n<=10^5),表示送餐对于出入栈序列的长度。
第二行,共n个非零的、互不相同的整数{bi}(0<|bi|<=2^20),表示机械人偶所记录的送餐情况。
保证给定的序列存在一对相邻的元素,满足交换两个元素后,序列为合法的出入栈序列。
输出描述
一行,两个空格分隔的正整数u,v,满足1<=u=v-1<=n-1,表示交换{bi} 中第u 和第v个元素后,序列即为一个合法的出入栈序列。
如果有多种解,输出任意一种即可。
样例输入
20
11 14 16 13 -13 -16 1 -1 6 10 7 -7 -10 5 3 -3 -5 -14 -6 -11
样例输出
18 19
说明:
会发现6在14之后入栈,但是6却没有在14之前出栈。按照样例输出,交换给定序列的第18和第个19元素后,整体就是一个合法的出入栈序列了。
参考题解
模拟栈操作:遍历给定的序列,模拟栈的入栈和出栈操作,找到第一个出栈错误的位置。尝试交换相邻元素:在找到的错误位置附近,尝试交换相邻的元素,验证交换后的序列是否合法。具体来说,我们会尝试交换错误位置的前一个元素和当前元素的位置,或者错误位置的前两个元素的位置。验证合法性:每次交换后,再次模拟栈操作,验证整个序列的合法性,直到找到正确的交换位置。
C++:[此代码未进行大量数据的测试,仅供参考]
#include <iostream> #include <vector> #include <stack> #include <utility> using namespace std; int getFirstError(const vector<int>& seq) { stack<int> stk; for (int i = 0; i < seq.size(); ++i) { int num = seq[i]; if (num > 0) { stk.push(num); } else { if (stk.empty() || stk.top() != -num) { return i; } stk.pop(); } } return stk.empty() ? -1 : seq.size(); } int main() { int n; cin >> n; vector<int> b(n); for (int i = 0; i < n; ++i) { cin >> b[i]; } int errorPos = getFirstError(b); if (errorPos == -1) { cout << "1 2" << endl; return 0; } vector<pair<int, int>> candidates; if (errorPos > 0) candidates.emplace_back(errorPos - 1, errorPos); if (errorPos < n - 1) candidates.emplace_back(errorPos, errorPos + 1); if (errorPos > 1) candidates.emplace_back(errorPos - 2, errorPos - 1); for (auto [u, v] : candidates) { vector<int> new_b = b; swap(new_b[u], new_b[v]); if (getFirstError(new_b
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南