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);

    }
}

全部评论

相关推荐

05-29 20:34
门头沟学院 C++
KarlAllen:得做好直接春招的准备。学历差的话,一是面试要求会比学历好的严格不少,二是就算面试通过了也会被排序。总之暑期和秋招对于学历差的就是及其不友好
无实习如何秋招上岸
点赞 评论 收藏
分享
07-15 11:35
门头沟学院 Java
心里踏实多了,可以安心准备论文了
看不见我ffgh:牛哇佬,要不要来试一试pdd,部门氛围很好
京东开奖153人在聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务