2021 Spring Recruitment Algorithm Written Questions

The 2021 Spring Recruitment Algorithm Written Test Questions are organized for your reference!
(The picture comes from the Internet, please contact the blogger for reprinting)

top notch

Xiaomei is learning the operating system recently.
Streaming is an important concept in operating systems.
Xiaomei has also implemented a device similar to /dev/random, but it can only provide a pre-set but looping arrangement of lowercase letters.
Insert picture description here

package MT;

import java.util.Scanner;

public class MT1 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        String in1 = sc.next();
        String in2 = sc.next();
        char[] chars1 = in1.toCharArray();
        char[] chars2 = in2.toCharArray();
        int length1 = chars1.length;
        int length2 = chars2.length;
        int count = 0;
        int i = 0;
        while(true) {
    
    
            for (int j = 0; j < length1; j++) {
    
    
                if (chars1[j] != chars2[i]) {
    
    
                    count++;
                } else  {
    
    
                    if (i < length2-1) i++;
                    else break;
                }
            }
            if (i == length2-1) break;
        }
        System.out.println(count);
    }
}

Two, multiple sets

Xiao Tuan now has two equal-sized multiple sets A and B. She now wants to make A set and B set equal.
Insert picture description here

package MT;

import java.util.HashMap;
import java.util.Scanner;

public class MT2 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];

        for (int i = 0; i < n; i++)
            a[i] = sc.nextInt();

        for (int i = 0; i < n; i++)
            b[i] = sc.nextInt();

        HashMap<Integer, Integer> mapA = new HashMap<>();
        HashMap<Integer, Integer> mapB = new HashMap<>();
        for (int i = 0; i < n; i++) {
    
    
            if (mapA.containsKey(a[i])) mapA.put(a[i], mapA.get(a[i]) + 1);
            else mapA.put(a[i], 1);
        }
        //System.out.println(mapA);
        for (int i = 0; i < n; i++) {
    
    
            if (mapB.containsKey(b[i])) mapB.put(b[i], mapB.get(b[i]) + 1);
            else mapB.put(b[i], 1);
        }
        //System.out.println(mapB);

        for (int i = 1; i < m; i++) {
    
    
            boolean match = true;
            for (Integer num : mapA.keySet()) {
    
    
                int numMod = (num + i) % m;
                if (mapB.containsKey(numMod) && mapA.get(num) == mapB.get(numMod))
                    continue;
                else {
    
    
                    match = false;
                    break;
                }
            }
            if (match) System.out.println(i);
        }
    }
}


//6 8
//1 1 4 5 1 4
//3 0 4 0 3 0

Three, milk tea

The school is holding a milk tea competition. For this competition, n cups of milk tea were prepared, with serial numbers from 1 to n.

package MT;

import java.util.Scanner;

public class MT3 {
    
    
    private static int minTime = Integer.MAX_VALUE;

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        double c = sc.nextInt();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
    
    
            arr[i] = sc.nextInt();
        }
        findTime(arr, 0, m, 0, c);
        System.out.println(minTime);
    }

    private static void findTime(int[] arr, int begin, int people, int time, double c) {
    
    
        if (people == 1) {
    
    
            double sum = 0;
            for (int i = begin; i < arr.length; i++) {
    
    
                sum += arr[i];
            }
            time = Math.max(time, (int) Math.ceil(sum / c));
            minTime = Math.min(minTime, time);
        } else {
    
    
            for (int i = begin; i < arr.length; i++) {
    
    
                double sum = 0;
                for (int j = begin; j <= i; j++) {
    
    
                    sum += arr[j];
                }
                findTime(arr, i + 1, people - 1, Math.max(time, (int) Math.ceil(sum / c)), c);
            }
        }
    }
}


Four, on duty

There are n people in Xiaomei's and Xiaotuan's classes, numbered 1 to n respectively. Among them, Xiaomei’s number is 1, and Xiaotuan’s number is 2.
Insert picture description here

package MT;

import java.util.Scanner;

public class MT4 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] duty = new int[n][n];
        for (int i = 0; i < n; i++) {
    
    
            for (int j = 0; j < n; j++) {
    
    
                duty[i][j] = sc.nextInt();
            }
        }
        System.out.println(studentID(m, duty));
    }

    static int studentID(int m, int[][] dutyTable) {
    
    
        int id = 0;// 值日的人
        // 初值
        int a1 = 1, a2 = 2;
        // 第一天小美 “1”,第二天小团 “2”,从第三天开始,第三天dutyTable[a2-1][a1-1]
        int i = 0;
        while ( i < (m - 2)) {
    
    
            id = dutyTable[a2-1][a1-1];
            a1 = a2;
            a2 = id;
            i++;
        }
        return id;
    }
}




// 输入
//3 7
//0 3 2
//3 0 3
//2 1 0

Five, solution

Xiaomei is doing a chemistry experiment. This experiment needs to configure a variety of solution concentrations, so the solution concentration needs to be calculated in detail before the solution is prepared to avoid misoperation.
Insert picture description here

package MT;

import java.util.Scanner;

public class MT5 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        // 初始溶液质量 m0 和 质量分数 w0
        int m0 = sc.nextInt();;
        double w0 = sc.nextInt() / 100.0;
        int n = sc.nextInt();  // 操作数

        // A操作之后的溶液质量和质量分数 mNew、wNew
        int mNew = m0; double wNew = w0;

        // 记录 A 操作加入溶液质量 m,加入溶液质量分数 w 的缓存值,方便B操作回退
        int mTmp = m0; double  wTmp = w0;

        String B = "B", A = "A";
        int i = 0;
        while (i < n) {
    
     // 操作 n 次,
            String input = sc.next();
            if (input == A) {
    
    
                int m = sc.nextInt();  // 加入溶液质量 m
                double w = sc.nextInt() / 100.0;  // 加入溶液质量分数 w
                // A操作之后的溶液质量和质量分数 mNew、wNew
                mNew = m0 + m;
                wNew = (m0 * w0 + m * w) / mNew;

                mTmp = m; wTmp = w;  // 记录 A 操作加入的量
            }

            if (input == B) {
    
    
                if (mNew == m0 && wNew == wTmp) continue;
                // 回退 上一步的 A,就是 根据公式反求一下
                mNew -= mTmp;
                wNew = (mNew * wNew - mTmp * wTmp) / (mNew - mTmp);// 反推公式中的变量

            }
            i++;
            System.out.println(mNew);
            System.out.println(String.format("%.5f",wNew));  // 保留5位小数
        }
    }
}

Guess you like

Origin blog.csdn.net/econe_wei/article/details/115037497