攜程筆試 攜程筆試題 0327
筆試時間:2025年03月27日
歷史筆試傳送門:
第一題
題目:回文時間
給你一個包含小時數(shù)和分鐘數(shù)的時間,讓你求出從當前開始到達最早的回文時間需要經過多少分鐘 我們將分鐘數(shù)直接拼接到小時數(shù)后面,如果這個數(shù)字正反都是一樣的,那么稱這個時間為回文時間,例如13:31就是一個回文時間,因為拼接得到的數(shù)字1331正反都是一樣的。
輸入描述
在一行上輸入五個字符代表一個時間,除了中間字符為冒號外,其余字符都是數(shù)字,格式形如hh:mm,其中hh表示時,mm表示分。
如13:15,表示13點15分。
時間采取24小時制,保證時間均為合法時間。
輸出描述
每組輸出占一行,包含一個整數(shù),表示答案
樣例輸入
13:29
樣例輸出
2
參考題解
模擬
C++:[此代碼未進行大量數(shù)據(jù)的測試,僅供參考]
#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:[此代碼未進行大量數(shù)據(jù)的測試,僅供參考]
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:[此代碼未進行大量數(shù)據(jù)的測試,僅供參考]
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}!它不想被游游拋棄掉,需要你來幫它恢復一下出入棧序列。即請你給出一對相鄰的元素的位置,保證根據(jù)你的指示交換后,出入棧序列合法即可。
輸入描述
第一行,一個正偶數(shù)n(2<=n<=10^5),表示送餐對于出入棧序列的長度。
第二行,共n個非零的、互不相同的整數(shù){bi}(0<|bi|<=2^20),表示機械人偶所記錄的送餐情況。
保證給定的序列存在一對相鄰的元素,滿足交換兩個元素后,序列為合法的出入棧序列。
輸出描述
一行,兩個空格分隔的正整數(shù)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
說明:
會發(fā)現(xiàn)6在14之后入棧,但是6卻沒有在14之前出棧。按照樣例輸出,交換給定序列的第18和第個19元素后,整體就是一個合法的出入棧序列了。
參考題解
模擬棧操作:遍歷給定的序列,模擬棧的入棧和出棧操作,找到第一個出棧錯誤的位置。嘗試交換相鄰元素:在找到的錯誤位置附近,嘗試交換相鄰的元素,驗證交換后的序列是否合法。具體來說,我們會嘗試交換錯誤位置的前一個元素和當前元素的位置,或者錯誤位置的前兩個元素的位置。驗證合法性:每次交換后,再次模擬棧操作,驗證整個序列的合法性,直到找到正確的交換位置。
C++:[此代碼未進行大量數(shù)據(jù)的測試,僅供參考]
#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%內容,訂閱專欄后可繼續(xù)查看/也可單篇購買
2025打怪升級記錄,大廠筆試合集 C++, Java, Python等多種語言做法集合指南