美团3-16笔试题
美团3-16的笔试题,选择题若干+3道编程题,一共花了1个小时20多分钟。
第3道题稍微花了点时间,题目难度算中等吧。
1.小美定义以下三种单词是合法的:
1. 所有字母都是小写。例如:good
2. 所有字母都是大写。例如:APP
3. 第一个字母大写,后面所有字母都是小写。例如:Alice
现在小美拿到了一个单词,她每次操作可以修改任意一个字符的大小写。小美想知道最少操作几次可以使得单词变成合法的?
输入描述
一个仅由大写字母和小写字母组成的字符串,长度不超过10^5。
输出描述
一个整数,代表操作的最小次数。
示例 1
输入
AbC
输出
1
说明
变成 ABC 或者 Abc 均可。只需要一次操作。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine();
int len = str.length();
int low = 0, high = 0;
boolean firstHigh = false;
for(int i = 0; i < len; i++) {
char c = str.charAt(i);
if('a' <= c && c <= 'z') {
low++;
} else if('A' <= c && c <= 'Z') {
high++;
}
}
if('A' <= str.charAt(0) && str.charAt(0) <= 'Z') {
firstHigh = true;
}
if(low == len || high == len || (high == 1 && firstHigh)) {
System.out.println(0);
} else {
// 走1 need high 次
// 走2 need low 次
// 走3 need high - FirstHigh 次
int other = high - (firstHigh == true ? 1 : 0);
int minn = Math.min(Math.min(high, low), other);
System.out.println(minn);
}
}
}
}
2.美团都会为自己的员工生成一个10位的员工号,格式是
YYMMAADDDD
YY:是入职年份的最后2位(入职年份在1970到2023之间)
MM:是入职年份的月份
AA:是是入职月份的日期
DDDD:4位数字,保证唯一,同时满足整个10位的员工号可以被13整除。
现在需要写一份代码,判断输入的工号是否符合对应的格式。
提示:闰年判断的条件是能被 4 整除, 但不能被 100 整除;或能被 400 整除。
输入描述
第一行输入数字n(1<= n <= 10)
接下来n行,每一行输入需要判断的工号。
输出描述
输出n行,如果是合法的,输出 "Yes" ,否则输出 "No"
示例 1
输入
3
9030121235
9012121235
9012121234
输出
No
Yes
No
说明
30不是月份
9012121234不能被13整除
public class Main {
public static void main(String[] args) {
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 31, 30};
Set<String> hashSet = new HashSet<>();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
while(n -- > 0) {
String id = in.nextLine();
if(id == null || id.length() != 10 || hashSet.contains(id)) {
System.out.println("No");
continue;
}
hashSet.add(id);
int yy = Integer.parseInt(id.substring(0, 2));
int mm = Integer.parseInt(id.substring(2, 4));
int aa = Integer.parseInt(id.substring(4, 6));
int dddd = Integer.parseInt(id.substring(6));
if(mm < 0 || mm > 12) {
System.out.println("No");
continue;
}
if(Long.parseLong(id) % 13 != 0) {
System.out.println("No");
continue;
}
int year = 0;
if(70 <= yy) {
year = 1900 + yy;
} else {
year = 2000 + yy;
}
if(mm != 2) {
if(aa >= 1 && aa <= days[mm]) {
System.out.println("Yes");
} else {
System.out.println("No");
}
} else {
boolean is366 = false;
if(year % 4 == 0 && year % 100 != 0) {
is366 = true;
}
if(year % 400 == 0) {
is366 = true;
}
int upper = is366 ? 29 : 28;
if(1 <= aa && aa <= upper) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}
}
3.小美拿到了一个数组,她每次操作会将除了第x个元素的其余元素翻倍,一共操作了q次。请你帮小美计算操作结束后所有元素之和。
由于答案过大,请对10^9+7取模。
输入描述
第一行输入两个正整数n,q,代表数组的大小和操作次数。
第二行输入n个正整数a_i,代表数组的元素。
第三行输入一个正整数q,代表操作的次数。
接下来的q行,每行输入一个正整数x_i,代表第i次操作未被翻倍的元素。
1 <= n,q <= 10^5
1 <= x_i <= n
1 <= a_i <= 10^9
输出描述
一个整数,代表操作结束后所有元素之和模10^9+7的值。
示例 1
输入
4 2
1 2 3 4
1
2
输出
34
说明
第一次操作后,数组变成[1,4,6,8]
第二次操作后,数组变成[2,4,12,16]
所有元素之和为 34。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long mod = 1_000_000_007L;
while (in.hasNextInt()) {
int n = in.nextInt();
int q = in.nextInt();
long a[] = new long[n];
for(int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
long normalTime = 1;
Map<Integer, Integer> hashMap = new HashMap<>();
Map<Integer, Long> mapTimes = new HashMap<>();
for(int i = 0; i < q; i++) {
int op = in.nextInt();
hashMap.put(op - 1, hashMap.getOrDefault(op - 1, 0) + 1);
normalTime *= 2;
normalTime %= mod;
mapTimes.put(i + 1, normalTime);
}
long ans = 0;
for(int i = 0; i < n; i++) {
Integer times = hashMap.get(i);
if(times == null) {
ans += (a[i] * normalTime) % mod;
ans %= mod;
} else {
long unNormalTime = mapTimes.get(q - times);
ans += (a[i] * unNormalTime) % mod;
ans %= mod;
}
}
System.out.println(ans);
}
}
}
#校招##美团##笔试##面经##春招#
查看11道真题和解析