2024美团秋招第一场笔试
解题思路:
首先,要知道我们第一行输入n表示,要输入的字符串个数,然后下一行表示正确的密码字符串。最简单是就是首先我们将所有的字符串(待测密码)都存储到字符串数组中去,然后将小于和等于正确密码的所有字符串个数计算出来记为a和b,那么最少测试数即是a+1,最多测试个数即为a+b,因为测试字符串是从最短的字符串开始的,并且不重复测,因此还需要对字符串数组去重,这里用到了HashSet集合进行字符串数组去重。
下面情况java代码:
package jxkjsfdx.lgq;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Demo03 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String correct_Pwd = in.next();
String[] strings = new String[n];
// 录入待测密码字符串
for (int i = 0; i < strings.length; i++) {
strings[i] = in.next();
}
// 对字符串数组进行去重--使用HashSet集合
Set<String> s = new HashSet<>();
for (int i = 0; i < strings.length; i++) {
s.add(strings[i]);
}
// 去重之后,定义两个变量count1和count2,分别记录小于正确密码字符串长度的字符串有多少个以及等于正确密码长度的字符串有多少个
int count1 = 0;
int count2 = 0;
int correct_pwd_length = correct_Pwd.length();
// 使用增强for循环来遍历集合
for (String s1 : s) {
if (s1.length() < correct_pwd_length){
count1++;
}else if (s1.length() == correct_pwd_length){
count2++;
}
}
int min_count = count1 + 1;
int max_count = count1 + count2;
System.out.println(min_count + " " + max_count);
}
}


