携程笔试 携程秋招 0906
笔试时间:2025年9月6日
往年笔试合集:
第一题
题目描述
给定一个长度为n的字符串(下标范围0~n-1),以及m个大小写变换操作。每次操作有两种类型:
- toLowerCase(x):将下标x及之后的子串全部转换为小写
- toUpperCase(x):将下标x及之后的子串全部转换为大写
输入描述
输出描述
输出执行所有操作后的最终字符串
样例输入
5 4
abCde
toLowerCase(1)
toUpperCase(4)
toUpperCase(0)
toLowerCase(2)
样例输出
AbcDE
参考题解
解题思路:
直接模拟即可,提取当前操作类型和操作的位置x,执行操作。
C++:
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
string s;
cin >> s;
string op;
for (int i = 0; i < m; ++i) {
cin >> op;
op.pop_back();
int idx = 0, p = 1;
while (isdigit(op.back())) {
idx = (op.back() - '0') * p + idx;
p *= 10;
op.pop_back();
}
op.pop_back();
if (op == "toLowerCase") {
for (int j = idx; j < n; j++) {
s[j] = tolower(s[j]);
}
} else if (op == "toUpperCase") {
for (int j = idx; j < n; j++) {
s[j] = toupper(s[j]);
}
}
}
cout << s << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
Java:
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
char[] chars = s.toCharArray();
for (int i = 0; i < m; i++) {
String op = sc.nextLine();
int idx = Integer.parseInt(op.substring(op.indexOf('(') + 1, op.indexOf(')')));
if (op.startsWith("toLowerCase")) {
for (int j = idx; j < n; j++) {
chars[j] = Character.toLowerCase(chars[j]);
}
} else if (op.startsWith("toUpperCase")) {
for (int j = idx; j < n; j++) {
chars[j] = Character.toUpperCase(chars[j]);
}
}
}
System.out.println(new String(chars));
}
}
Python:
def solve():
n, m = map(int, input().split())
s = list(input())
for _ in range(m):
op = input().strip()
idx = int(op[op.index('(') + 1: op.index(')')])
if op.startswith("toLowerCase"):
for i in range(idx, n):
s[i] = s[i].lower()
elif op.startswith("toUpperCase"):
for i in range(idx, n):
s[i] = s[i].upper()
print(''.join(s))
solve()
第二题
题目描述
对于给定的四个整数a, b, c, d,判断(a/b + c/d)是否为十进制下的有限小数或整数。
输入描述
输出描述
对于每组数据,若为有限小数或整数输出YES,否则输出NO
样例输入
3
2 3 1 3
2 7 1 13
19 79 3 316
样例输出
YES
NO
YES
参考题解
解题思路:
合并分数:(ac)/(b*d),化简为最简分数,检查分母。若分母此时只含质因子2和5,则答案为YES,
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2025 春招笔试合集 文章被收录于专栏
2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南
美的集团公司福利 798人发布
