牛客华为机考题总结

文章目录

  • 一. 栈
    • 1. 火车进站 ****
  • 二. 图论
    • 1. 24点游戏解法(DFS) **
    • 2. 放苹果(DFS或DP)

一. 栈

1. 火车进站 ****

牛客华为机考题总结_第1张图片

import java.util.*;
//本题难度较大
//此题分三步处理
//1.根据入站 求出 所有出站的可能:全排列,回溯算法
//2. 判断出站是否合理:利用栈模拟(每一辆车有两种可能)
//3. 按照字典序输出:把num变成string后 调用collections.sort排序
public class Main {
    public static boolean[] used;
    public static LinkedList<Integer> path = new LinkedList<>();
    public static List<List<Integer>> res = new ArrayList<>();//全排列所有结果
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] in = new int[n];
        used = new boolean[n];
        for (int i = 0; i < n; i++) {
            in[i] = sc.nextInt();
        }
        backTracking(in, n); //求出所有全排列
        List<String> ans = new ArrayList<>();
        for (List<Integer> num : res) {
            if (isValid(num, in)) { //对所有结果判断是否有效
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < num.size();i++) { //变成字符串 方便后续排列处理
                    sb.append(num.get(i)).append(" ");
                }
                ans.add(sb.toString());
            }
        }
        Collections.sort(ans);//排序
        for (String a : ans) {
            System.out.println(a);
        }
    }
    public static void backTracking(int[] nums, int n) {
        if (path.size() == n) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < n; i++) {
            if (used[i] == false) {
                path.add(nums[i]);
                used[i] = true;
                backTracking(nums, n);
                path.removeLast();
                used[i] = false;
            }
        }
    }
    public static boolean isValid(List<Integer> out, int[] in) {
        int i = 0;//指向入站的指针
        int j = 0;//指向出站的指针
        Deque<Integer> stack = new LinkedList<>();
        while (i < in.length) { //小于入站车辆
            //来到站的车辆有两种情况:1是入站并离开;2是停留在站台
            stack.push(in[i]);
            i++;
            while (!stack.isEmpty() && j < out.size() &&
                    stack.peek() == out.get(j)) { //如果与所给出站一致那就出站,需要是while循环
                stack.pop();
                j++;//指针后移
            }
        }
        return stack.isEmpty();
    }

}

二. 图论

1. 24点游戏解法(DFS) **

牛客华为机考题总结_第2张图片

import java.util.Scanner;

public class Main {
    public static boolean[] visited = new boolean[4];
    public static boolean flag = false;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] nums = new int[4];
        for (int i = 0; i < 4; i++) {
            nums[i] = sc.nextInt();
        }
        dfs(nums, 0, 0);
        System.out.println(flag == true);
    }
    public static void dfs(int[] nums, double sum, int count) {
        if (sum == 24 && count == 4) {//count记录用到的数字数量
            flag = true;
            return;
        }
        for (int i = 0; i < 4; i++) {
            if (visited[i] == false) {
                visited[i] = true;
                //对四种情况进行遍历
                dfs(nums, sum + nums[i], count + 1);
                dfs(nums, sum - nums[i], count + 1);
                dfs(nums, sum * nums[i], count + 1);
                dfs(nums, sum / nums[i], count + 1);
                visited[i] = false;//回溯
            }
        }
    }
}

2. 放苹果(DFS或DP)

牛客华为机考题总结_第3张图片

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        System.out.println(dfs(m,n));
    }
    public static int dfs(int m,int n){
        if(m==0 || n==1){
            return 1;
        }
        if(n>m) return dfs(m,m);
        return dfs(m-n,n) + dfs(m,n-1);
    }
}
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        System.out.println(putApple(m, n));
    }
    public static int putApple(int m, int n) {
        int[][] dp = new int[m + 1][n +
                                    1]; //m个苹果 n个盘子 有多少种方法放置
        for (int j = 0; j < n + 1; j++) {
            dp[0][j] = 1;//0个苹果j个盘子
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (j - i > 0) {
                    dp[i][j] = dp[i][i];
                } else {
                    dp[i][j] = dp[i - j][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m][n];
    }
}

你可能感兴趣的:(算法题,java,算法,开发语言,华为机考)