腾讯2021校园招聘编程题

腾讯2021校园招聘编程题

第一题

在这里插入图片描述

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine().trim()); 
        
        while (T-- > 0) {
            int n = Integer.parseInt(br.readLine().trim()); 
            int m = 100000;
            DisjointSetUnion dis = new DisjointSetUnion(m);
            for(int i = 0; i < n; ++i){
                String[] str = br.readLine().trim().split(" ");
                int x = Integer.parseInt(str[0]);
                int y = Integer.parseInt(str[1]);
                dis.unionSet(x-1,y-1);
            }
            
            int max = 0;
            for (int k = 0; k < m; ++k) {
                max = Math.max(max,dis.nodeNum[k]);
            }
            
    
            System.out.println(max);
        }
    }
}

//并查集(管理一系列不想交的集合),具有查询和合并的功能
class DisjointSetUnion {
    int[] f;//存储每个元素的根节点
    int[] nodeNum;//记录当前结点为根节点的结点数量
    int[] rank;//记录每个根节点对应树的深度
    int n;//元素个数

    public DisjointSetUnion(int n) {
        this.n = n;
        this.rank = new int[n];//记录每个根节点对应树的深度
        this.f = new int[n];//存储每个元素的父节点
        this.nodeNum = new int[n];
        for (int i = 0; i < n; i++) {
            this.rank[i] = 1;//初始值设为1
            this.f[i] = i;//一开始将父节点设为自己
            this.nodeNum[i] = 1;
        }
    }

    //压缩路径查询方法,递归实现
    //一层一层访问父节点,直至根节点(根节点的标志就是父节点是本身)。要判断两个元素是否属于同一个集合,只需要看它们的根节点是否相同即可
    public int find(int x) {
        return f[x] == x ? x : (f[x] = find(f[x]));
    }

    //按秩合并方法
    public boolean unionSet(int x, int y) {
        int fx = find(x), fy = find(y);//找到两个结点的根节点
        if (fx == fy) {//两个结点的根节点相同不用合并
            return false;
        }
        if (rank[fx] < rank[fy]) {//以fx为根节点的树的深度小于以fy为根节点的树的深度
            f[fx] = fy;
            nodeNum[fy] += nodeNum[fx];
        } else if (rank[fx] > rank[fy]) {
            f[fy] = fx;
            nodeNum[fx] += nodeNum[fy];
        } else {
            f[fx] = fy;
            rank[fy] += 1;
            nodeNum[fy] += nodeNum[fx];
        }
        return true;
    }
}

第二题

在这里插入图片描述

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine().trim();
        int k = Integer.parseInt(br.readLine().trim());
        
        Set<String> set = new HashSet<>();
        PriorityQueue<String> pq = new PriorityQueue<>((o1, o2) -> o2.compareTo(o1));
        
        for (int len = 1; len <= k; ++len) {
            for (int i = 0; i < s.length() - len + 1; ++i) {
                String substr = s.substring(i, i + len);
                if (set.add(substr)) {
                    if (pq.size() < k) {
                        pq.offer(substr);
                    } else {
                        if (pq.peek().compareTo(substr) > 0) {
                            pq.poll();
                            pq.offer(substr);
                        }
                    }
                }
            }
        }
        System.out.println(pq.peek());
    }
}

第三题

在这里插入图片描述

import java.io.*;
import java.util.*;


public class Main {
    private static double A = 0;
    private static double B = 0;
    private static double C = 0;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine().trim());
        while(T -- > 0){
            String[] str = br.readLine().trim().split(" ");
            A = Double.parseDouble(str[0]);
            B = Double.parseDouble(str[1]);
            C = Double.parseDouble(str[2]);
            if(A*A - 2*A*B*C <= 0){
                System.out.println(0);
            }else{
                double lb = (A - Math.sqrt(A*A - 2*A*B*C)) / B;
                double ub = (A + Math.sqrt(A*A - 2*A*B*C)) / B;
                System.out.println(f(ub) - f(lb));
            }
        }
    }
    
    private static double f(double y) {
        return -y*y*y / (6*A) + y*y /(2*B) - C*y/B;
    }
}

第四题

在这里插入图片描述

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine().trim());
        while (T-- > 0) {
            int Q = Integer.parseInt(br.readLine().trim());
            Queue<Integer> queue = new LinkedList<>();
            for(int i = 0; i < Q; i++){
                String command = br.readLine();
                if(command.startsWith("PUSH")){
                    String[] params = command.split(" ");
                    int elem = Integer.parseInt(params[1]);
                    queue.offer(elem);
                }else if(command.equals("TOP")){
                    if(queue.isEmpty())
                        System.out.println(-1);
                    else
                        System.out.println(queue.peek());
                }else if(command.equals("POP")){
                    if(queue.isEmpty())
                        System.out.println(-1);
                    else
                        queue.poll();
                }else if(command.equals("SIZE")){
                    System.out.println(queue.size());
                }else if(command.equals("CLEAR"))
                    queue.clear();
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Listen·Rain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值