华为笔试题 2022.3.30

1、业务部署芯片

在这里插入图片描述

思路,就硬模拟

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
    int m, n;
    cin >> m;
    cin >> n;

    char arr[n];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    // int m = 5, n = 6;
    // char arr[n] = "ABABAA";

    //以上是输入  m 是芯片数,n是业务数,arr就是一个字符数组,放了一串ABABABAB
    char temp;
    int b[m];
    for (int i = 0; i < m; ++i) {
        b[i] = 0;
    }
    int a1 = 0;
    int index1 = 1;  //未满状态的第一块芯片编号
    int index2 = 1;  //全空状态的第一块芯片编号
    for (int i = 0; i < n - 1; ++i) {
        temp = arr[i];
        if (temp == 'A') {
            if (a1 + 1 == 4) {
                a1 = 0;
                index1 = index2;
            } else {
                a1++;
                if (index1 == index2) {
                    index2++;
                }
            }

        } else if (temp == 'B') {
            if (index1 == index2) {
                index1++;
                index2++;
            } else {
                index2++;
            }
        }
    }
    // 判断最后一个
    if (arr[n - 1] == 'A') {
        if (index1 > m) {
            cout << 0 << endl;
            cout << 0 << endl;
        } else {
            cout << index1 << endl;
            cout << a1 + 1 << endl;
        }

    } else {
        if (index2 > m) {
            cout << 0 << endl;
            cout << 0 << endl;
        } else {
            cout << index2 << endl;
            cout << 1 << endl;
        }
    }
    return 0;
}

第二题,标准的二维地图(带障碍物)两点最短距离

在这里插入图片描述

上dfs

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;
//全局变量
const int MAX_X = 100;
const int MAX_Y = 100;
int min_step = 10000;
int number = 0;
int my_x, my_y;
int map[MAX_X][MAX_Y];
int book[MAX_X][MAX_Y];
int start_x, start_y;
int dest_x, dest_y;

void dfs(int x, int y, int step) {
    /*up, right, down, left*/
    int next[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
    int tx, ty;

    if (x == dest_x && y == dest_y) {
        if (step == min_step) {
            number++;
        }
        if (step < min_step) {
            min_step = step;
            number = 1;
        }

        return;
    }

    for (int i = 0; i < 4; i++) {
        tx = x + next[i][0];
        ty = y + next[i][1];
        if (tx > my_x || ty > my_y || tx < 0 || ty < 0)
            continue;

        if (map[tx][ty] == 0 && book[tx][ty] == 0) {
            book[tx][ty] = 1;
            dfs(tx, ty, step + 1);
            book[tx][ty] = 0;
        }
    }
}

int main() {
    cin >> my_x >> my_y;
    cin >> start_x >> start_y;
    cin >> dest_x >> dest_y;

    int num;
    cin >> num;

    //建一张二维表,初始化权为0
    int x, y;                        //湖泊的位置
    for (int i = 0; i < num; ++i) {  //高山湖泊用1代表
        cin >> x >> y;
        map[x][y] = 1;
    }

    book[start_x][start_y] = 1;
    dfs(start_x, start_y, 0);

    cout << number << " " << min_step << endl;
    return 0;
}

第三题,最大(深)的重复子树

在这里插入图片描述

652. 寻找重复的子树 差不多的题,只要把652的结果里面最长的那个返回就行了

万万没想到他给了一个层序遍历的数组,我不知道怎么变成一棵树,裂开,没做出来

华为OD笔试题是关于使用动态规划解决工作报酬问题的。给定工作总时长t,工作数量n,工作时间数组time和工作报酬数组earnings,需要选择一些工作使得总时长不超过t,并且获得最大的报酬。 有两种解法可以解决这个问题。解法1是使用二维dp数组,解法2是使用一维dp数组进行优化。 解法1中,我们创建一个二维dp数组,dp\[i\]\[j\]表示在前i个工作中,总时长不超过j的情况下能获得的最大报酬。然后使用两层循环遍历工作和时长,根据状态转移方程dp\[i\]\[j\] = max(dp\[i-1\]\[j\], dp\[i-1\]\[j-time\[i-1\]\] + earnings\[i-1\])来更新dp数组。最后返回dp\[-1\]\[-1\]即为最大报酬。 解法2是对解法1的优化,使用一维dp数组。我们只需要保存上一行的dp值,然后从后向前遍历时长,根据状态转移方程dp\[j\] = max(dp\[j\], dp\[j-time\[i-1\]\] + earnings\[i-1\])来更新dp数组。最后返回dp\[-1\]即为最大报酬。 以上是关于华为OD笔试题的解答。 #### 引用[.reference_title] - *1* *3* [华为OD笔试题:工作安排 --- 100分 (思路+python代码)](https://blog.csdn.net/m0_69258561/article/details/130973186)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【100%通过率】华为OD机试真题 Python 实现【分奖金】【2022.11 Q4 新题】](https://blog.csdn.net/misayaaaaa/article/details/128420154)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

中南自动化学院至渝

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

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

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

打赏作者

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

抵扣说明:

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

余额充值