华为校招机测(8-25)

时间:2021-08-31
本文章向大家介绍华为校招机测(8-25),主要包括华为校招机测(8-25)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

第一题:  最大值子矩阵

题目给出一个矩阵,里面的值有正有负,

求一个子矩阵,使得这个子矩阵框住的值最大,并返回最大值。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int rows = sc.nextInt();//行数
        int cols = sc.nextInt();//列数
        int res = Integer.MIN_VALUE;
        int[][]matrix = new int[rows][cols];
        for(int i=0; i<=rows-1; ++i){
            for(int j=0; j<=cols-1; ++j){
                matrix[i][j] = sc.nextInt();
            }
        }
        for(int begin = 0; begin<=rows-1; ++begin){//开始
            int[] ring = new int [cols];
            int[] dp = new int[cols];
            for(int end = begin; end<= rows-1; ++end){//结束
                for(int j=0; j<cols; ++j)
                    ring[j] += matrix[end][j];//列元素和
                res = Math.max(res,ring[0]);
                dp[0] = ring[0];
                for(int j=1; j<cols; ++j){
                    if(dp[j-1]<0)dp[j] = ring[j];
                    else dp[j] = dp[j-1] + ring[j];
                    res = Math.max(res,dp[j]);//取最大
                }
            }
        }
        System.out.println(res);
    }
}
//3 4
//-3 5 -1 5
//2 4 -2 4
//-1 3 -1 3

第二题:  逃跑路径长度

给出一个矩阵,里面是秒数,

从左上出发,到右下结束,每走一格,所有秒数都减一

求逃跑路径的长度(路径长度==逃跑总时间,一秒一格)

import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Node{
    int x;
    int y;
    int hop;
    public Node(int x, int y, int hop) {
        this.x = x;
        this.y = y;
        this.hop = hop;
    }
}

public class Main {
    static int row,col;
    static int[] dx={-1,1,0,0};
    static int[] dy={0,0,1,-1};

    private static int getAns(int[][] grid){
        if (grid[0][0]<=0){
            return -1;
        }
        int[][] minTime=new int[row][col];
        for (int i = 0; i < row; i++) {
            Arrays.fill(minTime[i],Integer.MAX_VALUE);
        }
        Queue<Node> queue=new LinkedList<>();
        queue.add(new Node(0,0,0));
        while (!queue.isEmpty()){
            Node node=queue.poll();
            int x=node.x;
            int y=node.y;
            int hop=node.hop;
//            System.out.println(x+" "+y+" "+hop);
            if (minTime[x][y]<=hop||grid[x][y]<hop)continue;
            minTime[x][y]=hop;
            if (x==row-1&&y==col-1)return hop;
            for (int i = 0; i < dx.length; i++) {
                int nextX=dx[i]+x;
                int nextY=dy[i]+y;
                if (!inGrid(nextX,nextY))continue;
                queue.add(new Node(nextX,nextY,hop+1));
            }
        }
        return -1;
    }
    private static boolean inGrid(int x,int y){
        return x>=0&&x<row&&y>=0&&y<col;
    }

    //Main主函数
    public static void main(String[] args) {
        Scanner s=new Scanner(new BufferedInputStream(System.in));
        row=s.nextInt();
        col=s.nextInt();
        int[][] grid=new int[row][col];
        for (int i = 0; i <row ; i++) {
            for (int j = 0; j < col; j++) {
                grid[i][j]=s.nextInt();
            }
        }
        System.out.println(getAns(grid));
    }
}
//如果想快速取分,直接return row+col-2, 可以得到90%的分

第三题:  任务规划(AOV)

题目给出多个任务,包括每个任务需要的时间,

以及所有任务依赖的其他任务,依赖的其他任务完成后才能执行本任务,

求完成所有任务所需的时间,如果任务循环依赖等待则返回-1

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        ArrayList<HashSet<Integer>> task = new ArrayList<HashSet<Integer>>();//存task任务依赖
        ArrayList<Integer> time = new ArrayList<>();//存时间
        for(int i=0; i<=num-1; ++i){
            String str1 = sc.next();
            String str2 = sc.next();
            String[] strs = str1.split(",");
            HashSet<Integer> task1 = new HashSet<>();
            task.add(task1);

            for(String s:strs){
                task.get(i).add(Integer.valueOf(s));
            }
            //任务
            time.add(Integer.valueOf(str2));
        }

        HashSet<Integer> finish = new HashSet<>();//完成的任务
        finish.add(-1);
        HashSet<Integer> run = new HashSet<>();

        int res = 0;

        for(;res<=1000;res++){
            //检查finish
            for(Integer f:finish){
                for(int i=0;i<=num-1;++i){
                    task.get(i).remove(f);
                }
            }
            for(int i=0;i<=num-1;++i){
                if(task.get(i).size()==0)run.add(i);
            }
            for(Integer r:run){
                int newValue = time.get(r)-1;
                time.set(r,newValue);
                if(time.get(r)==0)finish.add(r);
            }
            if(finish.size()==num+1){
                res++;
                break;
            }
        }
        if(res == 1001)res =-1;
        System.out.println(res);
    }
}
//6
//3,5 2
//5 3
//4 5
//1 2
//0 3
//-1 1

////16



//3
//-1 1
//2 2
//1 3

////-1


//3
//-1 1
//-1 2
//-1 3

////3

原文地址:https://www.cnblogs.com/qyf2199/p/15209589.html