校招笔试题目复盘:2021-08-01 奇安信笔试

心得

总时间:2小时

选择题半小时做完的,后面算法题背包问题的题意给理解错了,被坑了好长时间,没来得及调整输出就提交了。

所以,一定要先看清题目再开始写!

选择题(20单选、10多选)

涉及的知识点:

  • 各种排序算法的时间复杂度、是否是稳定排序?
  • 二叉排序树的时间复杂度、平均查找长度?中序遍历结果是有序的?
  • IPv6 协议相关知识:IPv6可以使用 DCHP 获取地址,对不对?
  • 数据链路层功能:是否包括拥塞控制、是否包括路由转发、是否包括限流?
  • 数据库删除一个视图的语句怎么写?DROP 视图名
  • 下列哪一个是内核锁?semaphore?cas?message?
  • CPU 调度相关知识
  • 虚拟内存容量受什么限制?
  • 软链接、硬链接,删除源文件之后是否还能访问?是否创建 inode?
  • 给你几段 IP 地址,问那些选项是完整的 CIDR 地址块?
  • 哪个是多连接协议?HTTP、SIP、FTP、H323?
  • linux命令,问哪个可以查看文件? tee?cat?more?less?
  • 守护进程初始化过程?https://blog.csdn.net/qq_35535992/article/details/52938324
  • 僵尸进程产生原因?如何避免僵尸进程,使用父进程2次fork可以吗?
  • java Optional 类包括那些方法?
  • 删除数据库的 sql 怎么写?删除表的 sql 怎么写?
  • 端口 80,1008,8080,443,监听哪些端口需要 root 权限?
  • java Annotation 是接口吗?注解只需要实现这个接口吗?Annotation 可以继承吗?

算法题

1、类似广义背包问题,告诉你总钱数,给你 n 个商品的价格、价值,不限制购买数量(必须是整数个),如何使价值最大化?

这个解法考试结束之后才写完,跑其中一个测试用例跑通了,没来得及提交,所以不知道是不是正解。

import java.util.*;

class Goods {
    int price;
    int value;
    double unitPrice;//单价:1元对应多少价值

    Goods(int price, int value) {
        this.price = price;
        this.value = value;
        this.unitPrice = (double) value / price;
    }

    @Override
    public String toString() {
        return "Thing{" +
                "price=" + price +
                ", value=" + value +
                ", unitPrice=" + unitPrice +
                '}';
    }
}

public class Main {
    public static void main(String[] args) {
        List<Goods> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int totalMoney = sc.nextInt();
        int totalGoods = sc.nextInt();
        for (int i = 0; i < totalGoods; i++) {
            int price = sc.nextInt();
            int value = sc.nextInt();
            list.add(new Goods(price, value));
        }
        Collections.sort(list, new Comparator<Goods>() {
            @Override
            public int compare(Goods o1, Goods o2) {
                if (o1.unitPrice == o2.unitPrice) {
                    return 0;
                } else {
                    return o1.unitPrice > o2.unitPrice ? 1 : -1; // 从小到大
                }
            }
        });

        int dp[][] = new int[21][200001];//题目告诉总钱数不会超过200000,物品不会超过20

        int goodsCount;    //第goodsCount个物品
        int leftMoney;    //剩余钱数

        //填dp表
        for (goodsCount = 1; goodsCount < totalGoods; goodsCount++) {
            for (leftMoney = 1; leftMoney < totalMoney; leftMoney++) {
                if (list.get(goodsCount).price > leftMoney)   //第goodsCount件物品钱数不够 此时物品的价值 = 判断完上一件物品之后物品的价值
                {
                    dp[goodsCount][leftMoney] = dp[goodsCount - 1][leftMoney];
                } else {
                    int value1 = dp[goodsCount - 1][leftMoney - list.get(goodsCount).price] + list.get(goodsCount).value;    //买了第goodsCount件物品后 物品总价值 = 先给这件物品留出钱数,剩余的钱数能买的最大价值 + 这件物品的价值
                    int value2 = dp[goodsCount - 1][leftMoney]; //不放入第goodsCount件物品 物品总价值 = 不用给这件物品留出钱数,当前钱数能买进的最大价值(就是判断完上一件物品之后物品的价值)
                    if (value1 > value2) {
                        dp[goodsCount][leftMoney] = value1;
                    } else {
                        dp[goodsCount][leftMoney] = value2;
                    }
                }
            }
        }
        System.out.println(dp[totalGoods - 1][totalMoney - 1]);
    }
}
/**
 * 测试用例
 * 100
 * 5
 * 77 92
 * 22 22
 * 29 36
 * 50 46
 * 99 90
 * 答案:114
 */

2、给你一个数组,问你,用这个数组的数,组成的全排列,有多少个能被 7 整除?

排列组合,全部AC

public class Solution {
    public int total = 0;

    public static void main(String[] args) {
        int[] digit = {1, 1, 2}; // 测试用例,注意这里有 2 个 1,所以结果集中会出现多个{1,1,2},均保留
        Solution solution = new Solution();
        solution.reletive_7(digit); // 答案:2
    }

    public int reletive_7(int[] digit) {
        dfs(digit, 0);
        return total;
    }

    public void dfs(int[] digit, int k) {
        if (k == digit.length) {
            int exp = 0;
            int sum = 0;
            for (int i = 0; i < digit.length; i++) {
                sum += digit[i] * Math.pow(10, exp);
                exp += 1;
            }
            if (sum % 7 == 0) total++;
        }
        for (int i = k; i < digit.length; i++) {
            {
                int t = digit[k];
                digit[k] = digit[i];
                digit[i] = t;
            }
            dfs(digit, k + 1);
            {
                int t = digit[k];
                digit[k] = digit[i];
                digit[i] = t;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_42483341/article/details/107735019