秋招突击——9/10、9\11——算法练习——携程笔试练习——2024年秋招第一批笔试

news2025/4/10 2:15:36

文章目录

    • 引言
    • 笔试准备
      • 2024年秋招研发第一批
        • 第一题
        • 第二题
          • 第二次实现
        • 第三题
        • 第四题
        • 第五题
          • 参考实现
    • 总结

引言

  • 准备全力冲携程,好好做算法,去线下面试!
  • 今天就好好做做携程往年的笔试!

笔试准备

2024年秋招研发第一批

第一题

在这里插入图片描述

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息

public class Main {
    public static boolean isPrime(int n) {
        for (int i = 2; i <= Math.sqrt(n); i ++) {
            if (n % i == 0)  return false;
        }
        return true;
    }

    static int res;
    static boolean[] visited;
    public static void dfs(int n, int idx, List<Integer> temp) {
        if (idx == n) {
            // 已经到达了终点,直接添加对应的元素
            res ++;
           // System.out.println(temp.toString());
            return;
        }

        for (int i = 1; i <= n; i ++) {
            if (!visited[i]) {
                // 没有访问过
                if (temp.isEmpty() || !isPrime(i  + temp.get(temp.size() - 1))) {
                    visited[i] = true;
                    temp.add(i);
                    dfs(n, idx + 1, temp);
                    temp.remove(temp.size() - 1);
                    visited[i] = false;
                }
            }
        }
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            visited = new boolean[n + 1];
            dfs(n, 0, new ArrayList<Integer>());
            System.out.println(res);
        }
    }
}

总结

  • 明明是一个dfs,还是比较简单的dfs,但是有很多细节没注意到位,很难受!

在这里插入图片描述

第二题

在这里插入图片描述
在这里插入图片描述

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static boolean judge(int[] a,int[] b,int[] c) {
        // 判断是否为对应符合条件的三个点
        if((a[0] == b[0] && a[1] == c[1]) || (a[0] ==c[0] && a[1] == b[1])) return true;
        if((b[0] == a[0] && b[1] == c[1]) || (b[0] ==c[0] && b[1] == a[1])) return true;
        if((c[0] == b[0] && c[1] == a[1]) || (c[0] ==a[0] && c[1] == b[1])) return true;
        return false;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int m = in.nextInt();
            int n = in.nextInt();
            char[][] chars = new char[m][n];
            List<int[]> listY = new ArrayList<>();
            List<int[]> listO = new ArrayList<>();
            List<int[]> listU = new ArrayList<>();

            for(int i = 0;i < m;i ++){
                String str = in.next();
                chars[i] = str.toCharArray();
                for(int j = 0;j < chars[i].length;j ++){
                    if(chars[i][j] == 'y') listY.add(new int[]{i,j});
                    if(chars[i][j] == 'o') listO.add(new int[]{i,j});
                    if(chars[i][j] == 'u') listU.add(new int[]{i,j});

                }
            }

            // 开始具体的逻辑处理
            int count = 0;
            for(int[] pointY : listY){
                for(int[] pointO:listO){
                    for(int[] pointU:listU){
                        if(judge(pointY,pointO,pointU)) {
                            count ++;
                        }
                    }
                }
            }
            System.out.println(count );
        }
    }
}

在这里插入图片描述
总结

  • 仅仅通过三组,这里直接暴力,可以使用dp做一下,下次再试试看,因为每一个中心点,都要记录一下之前有多少u、o和y,明天再做
第二次实现
  • 计算每一行或者每一列的前n项的y、o、u的累加和,然后在计算乘积就行了!
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static boolean judge(int[] a, int[] b, int[] c) {
        // 判断是否为对应符合条件的三个点
        if ((a[0] == b[0] && a[1] == c[1]) || (a[0] == c[0] &&
                                               a[1] == b[1])) return true;
        if ((b[0] == a[0] && b[1] == c[1]) || (b[0] == c[0] &&
                                               b[1] == a[1])) return true;
        if ((c[0] == b[0] && c[1] == a[1]) || (c[0] == a[0] &&
                                               c[1] == b[1])) return true;
        return false;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int m = in.nextInt();
            int n = in.nextInt();
            char[][] chars = new char[m][n];
            int[][] rowCount = new int[m][3];  // 0表示y,1表示o,2表示u
            int[][] colCount = new int[n][3];  // 0表示y,1表示o,2表示u

            // 这个是遍历的标签
            for (int i = 0; i < m; i ++) {
                String str = in.next();
                chars[i] = str.toCharArray();
                for (int j = 0; j < chars[i].length; j ++) {
                    if (chars[i][j] == 'y') {
                        rowCount[i][0] ++;
                        colCount[j][0] ++;
                    }
                    if (chars[i][j] == 'o') {
                        rowCount[i][1] ++;
                        colCount[j][1] ++;
                    }
                    if (chars[i][j] == 'u') {
                        rowCount[i][2] ++;
                        colCount[j][2] ++;
                    }
                }
            }

            // 开始具体的逻辑处理
            long count = 0;
            for (int i = 0; i < m; i ++) {
                for (int j = 0; j < n; j ++) {
                    if (chars[i][j] == 'y') {
                        count = count + rowCount[i][1] * colCount[j][2] + rowCount[i][2] *
                                colCount[j][1] ;
                    }
                    if (chars[i][j] == 'o') {
                        count = count + rowCount[i][0] * colCount[j][2] + rowCount[i][2] *
                                colCount[j][0] ;

                    }
                    if (chars[i][j] == 'u') {
                        count = count + rowCount[i][1] * colCount[j][0] + rowCount[i][0] *
                                colCount[j][1] ;
                    }
                }
            }
            System.out.println(count );
        }
    }
}

在这里插入图片描述

第三题
  • 编写一个SQL查询,返回每个商品的销售总量,先按照商品类别升序排序,再按销售总量降序排列,同时包括商品名称和销售总量。此外,还需要在结果中包含每个商品在其所属类别内的排名,排名相同的商品可以按照 product_id 升序排序。

初始版本

  • 这个题目一开始只能写成下面那个初始化的版本,并不知道怎么在同一类别中再进行排名。
SELECT products.name as product_name, sum(quantity) as total_sales , products.category as category_rank
FROM orders
    join products on products.product_id = orders.product_id 
group by products.name , products.category ,orders.product_id;

最终版本

SELECT
    products.name as product_name,
    SUM(orders.quantity) AS total_sales,
    RANK() OVER (
        PARTITION BY
            products.category
        ORDER BY
            SUM(orders.quantity) DESC,
            products.product_id ASC
    ) AS category_rank
FROM
    products
    JOIN orders ON products.product_id = orders.product_id
GROUP BY
    products.name,
    products.category,
    products.product_id
ORDER BY
    products.category ASC,
    total_sales DESC,
    products.product_id ASC;

复习的知识

  • Rank关键字
    • 用来给结果集中的每一行分配一个排名,通过over来确定排名如何运用。
    • 需要使用order by来指定排名顺序
      在这里插入图片描述
  • 通过partion by将数据进行分组,然后按组内的数据进行排名。上述要求中,是按照同类别的销售总量进行的排序,所以需要增加一个partion by关键字

 rank() over (
        partition by products.category 
        order by sum(quantity) DESC , products.product_id ASC) as category_rank
第四题

在这里插入图片描述

个人实现

  • 无限次加一和减一操作,总量sum是不变的,直接计算一下平均值,看看能不能范围内,借此判定是否可以操作。然后计算所有小于左边界差值的累加和以及所有大于右边界的差值的累加和,然后选一个最大值就行了!
  • 这个代码写的很快,但是剩下的样例不知道怎么过了,感觉没啥问题!
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int T = in.nextInt();
        while (T-- > 0) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int l = in.nextInt();
            int r = in.nextInt();
            int[] nums = new int[n];
            long sum = 0L;
            for(int i = 0;i < n;i ++)   {
                nums[i] = in.nextInt();
                sum += nums[i];
            }
            long avg = (long)sum / n;
            if(avg > r || avg < l)  System.out.println(-1);
            else{
                long subCount = 0;
                long addCount = 0;
                for(int x:nums){
                    if(x < l)   addCount += (l - x);
                    if(x > r)   subCount += (x - r);
                }

                System.out.println(Math.max(addCount , subCount));
            } 

            // 现在进一步进行判定

        }
    }
}
  • 忽然间想到,我是使用long保存的平均值,会报错,具体如下
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int T = in.nextInt();
        while (T-- > 0) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int l = in.nextInt();
            int r = in.nextInt();
            int[] nums = new int[n];
            long sum = 0L;
            for(int i = 0;i < n;i ++)   {
                nums[i] = in.nextInt();
                sum += nums[i];
            }
            double avg = sum / (double)n;
            if(avg > r || avg < l)  System.out.println(-1);
            else{
                long subCount = 0;
                long addCount = 0;
                for(int x:nums){
                    if(x < l)   addCount += (l - x);
                    if(x > r)   subCount += (x - r);
                }

                System.out.println(Math.max(addCount , subCount));
            } 

            // 现在进一步进行判定

        }
    }
}

在这里插入图片描述

第五题

在这里插入图片描述
个人实现

  • 这个先简单点,直接用滑动窗口去做,然后统计一下每一次滑动窗口是否符合条件!但是问题在于,如何写出一个统计函数,计算每一个子串是不是好串!怎么写?需要记录一下状态,也就是每一个序列中,到当前序列而言,对应的零和一的数量,然后在进行计算!
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.next();
            char chars[] = str.toCharArray();

            // 使用滑动窗口进行遍历
            int count0 = 0;
            int count1 = 0;
            int res = 0;
            int n = chars.length;
            for (int l = 0, r = 0; r < n; r++) {
                if (chars[r] == '0') count0 ++;
                if (chars[r] == '1') count1 ++;
                // 移动l,保证l到r是一个好子串
                while (l <= r && count0 < count1) {
                    if (chars[l] == '0') count0 --;
                    else count1 --;
                    l ++;
                }
                if (count0 > count1)
                    res ++;
            }
            System.out.println(res);
        }
    }
}

总结

  • 写的快,但是通过的样例也少了,如果按照正常的考试时间安排,能够通过三个题,差不多可以进面。第四题,应该还有其他的方法,这里直接看解析!

再次尝试,使用状态记录

  • 效果属实一般,全部超时!
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.next();
            char chars[] = str.toCharArray();

            // 使用滑动窗口进行遍历
            int count0 = 0;
            int count1 = 0;
            int res = 0;
            int n = chars.length;
            int[][][] dp = new
            int[n][n][3];  // 第一个数字表示1合法序列,0未非法序列,后续两个分别是0和1的数量
            for (int l = 0; l < n; l++) {
                for (int r = l ; r < n; r ++) {
                    if (l == r) {
                        if (chars[l] == '0') {
                            dp[l][r][0] = 1;
                            dp[l][r][1] = 1;
                            dp[l][r][2] = 0;
                            res ++;
                        } else {
                            dp[l][r][0] = 0;
                            dp[l][r][1] = 0;
                            dp[l][r][2] = 1;
                        }
                    }
                    // 两者不相等,直接进行判断
                    else {
                        // 更新一下0和1的数量
                        if (chars[r] == '0') {
                            dp[l][r][1] = dp[l][r - 1][1] + 1;
                        } else {
                            dp[l][r][2] = dp[l][r - 1][2] + 1;
                        }

                        if (dp[l][r - 1][0] == 0) {
                            // 上一个状态的子串是非法的
                            dp[l][r][0] = 0;
                        } else {
                            if (dp[l][r][1] > dp[l][r][2]) {
                                // 0的数量大于1,那么直接更新
                                dp[l][r][0] = 1;
                                res ++;

                            } else {
                                dp[l][r][0] = 0;
                            }
                        }
                    }
                }
            }
            System.out.println(res);
        }
    }
}
参考实现
  import java.util.*; 
 
    
 
  class Main { 
 
      static final int MAXN = (int) (1e5 + 10); 
 
      static char[] chs = new char[MAXN]; 
 
      static long res = 0; 
 
      public static void main(String[] args) { 
 
    
 
          Scanner scanner = new Scanner(System.in); 
 
          String s = scanner.next(); 
 
          int n = s.length(); 
 
          for(int i = 1; i <= n; i++) { 
 
              chs[i] = s.charAt(i - 1); 
 
          } 
 
          int cnt1 = 0, cnt0 = 0; 
 
          for(int l = 1, r = 1; r <= n; r++) { 
 
              if(chs[r] == '0') 
 
                  cnt0++; 
 
              else 
 
                  cnt1++; 
 
    
 
              while(cnt1 >= cnt0 && l <= r) { 
 
                  if(chs[l] == '0') 
 
                      cnt0--; 
 
                  else 
 
                      cnt1--; 
 
                  l++; 
 
              } 
 
              res += (r - l + 1); 
 
              res -= 2L * cnt1; 
 
          } 
 
          System.out.println(res); 
 
      } 
 
  }

总结

  • 基本思路都是滑动窗口的,但是最后那里去除不合法子串那里,有点问题,感觉有点疑惑,然后计算所有合法子串那里确实世界计算r-l +1 的结果,因为开头变了,子串的状态就变了!

总结

  • 提前练了一下携程的笔试,感觉还行,基本上能过个三四题!继续加油!冲!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2143559.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

<<编码>> 第 14 章 反馈与触发器(1)--振荡器 示例电路

继电器振荡器 info::操作说明 无需操作, 保持控制开关常闭以形成振荡 如需停止振荡, 则断开控制开关 注: 要看到灯闪烁的效果, 右上角 “仿真速度” 控制杆应设置为一个较低的位置(靠左侧) 另: 因继电器内部开关跳动动画效果耗时太长, 即便设置为较低的仿真速度也无法观察到开关…

有效的对嵌入式操作系统进行消毒处理

这篇论文的标题是《Effectively Sanitizing Embedded Operating Systems》&#xff0c;作者是 Jianzhong Liu, Yuheng Shen, Yiru Xu, Hao Sun, Heyuan Shi, Yu Jiang。论文主要研究了嵌入式操作系统的安全性问题&#xff0c;并提出了一种名为 EmbSan 的嵌入式系统消毒器&#…

计算机的错误计算(九十六)

摘要 探讨 的计算精度问题。 计算机的错误计算&#xff08;五十五&#xff09;与&#xff08;七十八&#xff09;分别列出了 IEEE 754-2019 中的一些函数与运算。下面再截图给出其另外3个运算。 例1. 已知 x-0.9999999999966 . 计算 不妨在Python下计算&#xff0c;则有&am…

TI DSP TMS320F280025 Note11:F280025时钟系统

TMS320F280025 F280025时钟系统 ` 文章目录 TMS320F280025 F280025时钟系统TMS32F280025时钟系统框图**时钟系统框图分析**时钟源主内部振荡器(INTOSC2)用内部振荡器(INTOSC1)派生的时钟振荡器时钟(OSCCLK)系统锁相环输出时钟(PLLRAWCLK)设备时钟域系统时钟(PLLSYSCLK)CPU时钟(…

PyTorch 激活函数及非线性变换详解

激活函数是深度学习模型的重要组成部分&#xff0c;它们引入非线性&#xff0c;从而使模型能够更好地拟合复杂的数据模式。本文将详细介绍激活函数的作用、常见类型、经典应用示例&#xff0c;并比较它们的优缺点。 激活函数的作用 激活函数的主要作用是引入非线性变换&#…

12 Java文件处理之写入、读取:IO流(中):高级流(缓冲流、转换流、序列化流和反序列化流、打印流)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、缓冲流1 字节缓冲流(1)BufferedInputStream:字节缓冲输入流构造方法---- BufferedInputStream(InputStream in):创建一个使用默认缓冲区大小的缓冲输入流。---- BufferedInputStream(In…

算法题目复习(0909-0917)

1. 连续子序列和 pdd的算法题&#xff0c;根本不记得怎么做 给一个数组&#xff0c;有正数和负数&#xff0c;算出连续子序列的和最大为多少 int maxSubArraySum(vector<int>& nums) {int maxSoFar nums[0];int maxEndingHere nums[0];for (size_t i 1; i <…

说说几款耳机

从前&#xff0c;大约在戴森推出他们那款奇特的发明——戴森耳机与空气净化器组合一年后&#xff0c;人们仍对这个奇怪的产品感到困惑。这款穿戴式空气净化耳机更像是一个实验&#xff0c;缺乏实际用途。回想起那时的评测&#xff0c;大家一致认为这是有史以来最无意义的产品之…

IDEA 2024.3 EAP新特征早览!

0 前言 IntelliJ IDEA 2024.3 第一个 EAP 版本已发布&#xff0c;提前体验 下一个重大版本的一部分改进。 持续关注 EAP 更新&#xff0c;未来几周内将推出更多 IntelliJ IDEA 新功能。尝试这些新功能&#xff0c;分享您的反馈&#xff0c;共同完善 IDE。 1 AI 助手 1.1 内…

Web3入门指南:从基础概念到实际应用

Web3&#xff0c;即“去中心化的第三代互联网”&#xff0c;正在逐步改变我们对互联网的传统认知。从最初的静态网页&#xff08;Web1.0&#xff09;到互动平台和社交媒体为主的互联网&#xff08;Web2.0&#xff09;&#xff0c;Web3的目标是让用户重新掌握对数据和数字资产的…

比特币10年价格数据(2014-2024)分析(基础)

数据入口&#xff1a;【每周挑战】比特币10年价格数据可视化和量化分析 - Heywhale.com 本数据集包含 2014 - 2024 的比特币美元价格数据&#xff0c;具体包含比特币每日的开盘价、最高价、最低价、收盘价以及成交量等关键信息。数据说明如下&#xff1a; 字段说明Date日期&a…

PMP--一模--解题--151-160

文章目录 11.风险管理--规划风险应对--机会应对策略--上报151、 [单选] 早在执行阶段&#xff0c;项目经理就发现&#xff0c;事业环境因素&#xff08;EEF&#xff09;最近发生的变化将使实施成本大幅减少&#xff0c;而且还将缩减项目进度计划&#xff0c;项目经理该如何应对…

《沈阳体育学院学报》

《沈阳体育学院学报》创刊于1982年&#xff0c;是由沈阳体育学院主办&#xff0c;面向国内外公开发行的体育类学术期刊&#xff1b;国际标准刊号为ISSN 1004-0560&#xff0c;国内刊号为CN 21-1081/G8&#xff1b;双月刊&#xff0c;单月中旬出版。 《沈阳体育学院学报》是中文…

Django_Vue3_ElementUI_Release_004_使用nginx部署

1. nginx安装配置 1.1 下载nginx Download nginx 1.2 测试一下 1.3 进入nginx用命令操作 2. 部署 2.1 前端部署 2.1.1 修改nginx监听配置 …conf/nginx.conf http {... # 这里不进行修改server {listen 8010; # 监听 80 端口server_name 192.168.10.24; # 输入服务器 ip…

java进销存系统源码:管店云进销存解决方案

在当今数字化转型的大背景下&#xff0c;企业对高效、可靠的进销存管理系统的需求日益增长。Java作为一种广泛使用的编程语言&#xff0c;以其成熟的技术栈和强大的生态系统&#xff0c;成为了开发高性能进销存系统的首选语言之一。本文将介绍一款基于Java进销存系统源码的“管…

[乱码]确保命令行窗口与主流集成开发环境(IDE)统一采用UTF-8编码,以规避乱码问题

文章目录 一、前言二、命令行窗口修改编码为UTF-8三、Visual Studio 2022修改编码为UTF-8四、Eclipse修改编码为UTF-8五、DevCPP修改编码为UTF-8六、Sublime Text修改编码为UTF-8七、PyCharm、IDEA、VS Code及Python自带解释器修改编码为UTF-8 一、前言 在学习的征途中&#x…

AG32 MCU与内置FPGA的FLASH空间如何划分

AG32与内置FPGA的FLASH空间如何划分 关于芯片flash 大小&#xff1a; 不管所选型号的flash 是多大&#xff0c;请注意最后100K 是留给fpga 使用的。 如果使用的芯片是256K 的flash 空间&#xff0c;那么就是156K 程序100K fpga&#xff0c;用户程序不能 超过156K。如果超过1…

网络流之最大流(dinic算法模板+模板题)

dinic算法&#xff1a;时间复杂度O(), n 代表点的个数&#xff0c;m 代表边的个数。 const int N1e55; struct Edge{int to,w,next; }edge[N*2];//双向边 int head[N],d[N],cur[N]; int n,m,s,t,cnt1;// 从 2 , 3 开始配对 void add(int u,int v,int w){edge[cnt]{v,w,head[…

VirtualBox 7.1.0 发布下载 - 开源跨平台虚拟化软件

VirtualBox 7.1.0 (macOS, Linux, Windows) - 开源跨平台虚拟化软件 Oracle VM VirtualBox 7 请访问原文链接&#xff1a;https://sysin.org/blog/virtualbox-7/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org 2024 年 9 月 …

软考中级软件设计师——数据结构与算法基础学习笔记

软考中级软件设计师——数据结构与算法基本概念 什么是数据数据元素、数据项数据结构逻辑结构物理结构&#xff08;存储结构&#xff09; 算法什么是算法五个特性算法效率的度量时间复杂度空间复杂度 什么是数据 数据是信息的载体&#xff0c;是描述客观事物属性的数、字符及所…