【JAVA】【華為校園招聘筆試-軟體】2020-09-09

愛做夢的魚發表於2020-09-09

前言

華為三道題,100+200+300,100及格,大家做對第一題就好了,祝大家全都有心儀的offer,不要慌,不要焦慮
在這裡插入圖片描述

一、完美排列——玩具(全A)(注意:題目中說:如果不是完美排列,則輸出0,沒注意這種情況的應該A0.6或0.7)

程式碼:暴力就完事了

package huawei0909;

import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/9
 * @Time: 19:04
 * @Version: 1.0
 * @Description: Description
 */
public class First {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int[] perArr = new int[K];
        int[] perArr1 = new int[K];
        for (int i = 0; i < K; i++)
            perArr[i] = sc.nextInt();
        for (int i = 0; i < K; i++)
            perArr1[i] = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] arr1 = new int[n];
        for (int i = 0; i < n; i++)
            arr[i] = sc.nextInt();
        for (int i = 0; i < n; i++)
            arr1[i] = sc.nextInt();
        sc.close();
        /*if (n<K){ //必須判斷,不然A0.6或0.7,因為題目中說:如果不是完美排列,則輸出0,詳情看下面的System.out.println(i + 1);
            System.out.println(0);
            return;
        }*/
        for (int i = 0; i < n; i++) {
            if (arr[i] == perArr[0] && arr1[i] == perArr1[0] && i + K - 1 < n && arr[i + K - 1] == perArr[K - 1] && arr1[i + K - 1] == perArr1[K - 1]) {
                boolean flag = true;
                int index = i;
                for (int j = 1; j < K - 1; j++) {
                    index++;
                    if (!(arr[index] == perArr[j] && arr1[index] == perArr1[j])) {
                        flag = false;
                        break;
                    }
                }
                if (flag) { //輸出可能為0,如果沒考慮到,則A0.6或0.7,因為題目中說:如果不是完美排列,則輸出0
                    System.out.println(i + 1);
                    return;
                }
            }
        }
        System.out.println(0); //必須有,不然A0.6或0.7,因為題目中說:如果不是完美排列,則輸出0,詳情看下面的System.out.println(i + 1);
    }
}

二、最長的水溝(全A)

package huawei0909;

import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/9
 * @Time: 19:36
 * @Version: 1.0
 * @Description: Description
 */
public class Second {
    public static int[][] matrix;
    public static int[][] dp;
    public static int[][] k = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    public static int n, m, ans;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        matrix = new int[n + 1][m + 1];
        dp = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                matrix[i][j] = sc.nextInt();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                ans = Math.max(ans, dfs(i, j));
        System.out.println(ans + 1);
    }

    public static int dfs(int x, int y) {
        if (dp[x][y] != 0)
            return dp[x][y];
        for (int i = 0; i <= 3; i++) {
            int tx = x + k[i][0];
            int ty = y + k[i][1];
            if (!(tx < 1 || ty < 1 || tx > n || ty > m || matrix[tx][ty] >= matrix[x][y]))
                dp[x][y] = Math.max(dp[x][y], 1 + dfs(tx, ty));
        }
        return dp[x][y];
    }
}

三、最大異或路徑(A3.33)

相關文章