华为笔试题 20200812

相比其他公司,确实简单,不需要太多技巧,基本功扎实就好。

第一题:模拟就好,每来一个顾客能找钱就找,20块优先用10块去找。


#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int count[] = {0, 0, 0};
  int num = 0;
  int n = 0;
  bool success = true;
  while (cin >> num) {
    n++;
    if (num == 5) {
      count[0]++;
    } else if (num == 10) {
      if (!count[0]) {
        success = false;
        break;
      }
      count[1]++;
      count[0]--;
    } else {
      if (!count[0]) {
        success = false;
        break;
      }
      if (count[1]) {
        count[0]--, count[1]--;
      } else {
        if (count[0] < 3) {
          success = false;
          break;
        } else {
          count[0] -= 3;
        }
      }
    }
    getchar();
  }
  if (success)
    printf("true,%d", n);
  else
    printf("false,%d", n);
  return 0;
}

第二题:简单的 dfs,注意访问过的位置不要重复访问即可。

#include <cstdio>
#include <iostream>
using namespace std;

int matrix[110][110];
int s, m, n;
void dfs(int i, int j) {
  if (i >= m || j >= n) return;
  if (matrix[i][j] == 2) return;
  matrix[i][j] = 2;
  if (j < n - s && matrix[i][j + s]) {
    dfs(i, j + s);
  }
  if (i < m - s && matrix[i + s][j]) {
    dfs(i + s, j);
  }
  if (j >= s && matrix[i][j - s]) {
    dfs(i, j - s);
  }
  if (i >= s && matrix[i - s][j]) {
    dfs(i - s, j);
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  cin >> s >> m >> n;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> matrix[i][j];
    }
  }
  dfs(0, 0);
  cout << (matrix[m - 1][n - 1] == 2) << endl;
  return 0;
}

第三题:类似于 LeetCode 的 Z 字形,但是这个 X 相当于是左右两条线一起放,Z 是一条线,所以感觉这个更麻烦些。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  string str;
  char c;
  while (cin >> c) {
    if (c == ',') break;
    str += c;
  }
  cin >> n;
  // cout << str << n;
  vector<string> ss(n);
  int flag = 1;
  int l = 0, r = n - 1;
  int i = 0;
  while (i < str.length()) {
    if (l == r) {
      ss[l] += str[i++];
      l--, r++;
      flag = 0;
      continue;
    }
    if (l == 0) {
      flag = 1;
    }
    if (flag) {
      ss[l++] += str[i++];
      if (i == str.length()) break;
      ss[r--] += str[i++];
      continue;
    }
    if (!flag) {
      ss[l--] += str[i++];
      if (i == str.length()) break;
      ss[r++] += str[i++];
    }
  }
  string res;
  for (int i = 0; i < n; i++) res += ss[i];
  cout << res << endl;
  return 0;
}



#笔试题目##华为#
全部评论
👍
点赞 回复
分享
发布于 2020-08-12 23:11
你是满分?第二题好多人100没到
点赞 回复
分享
发布于 2020-08-13 07:30
乐元素
校招火热招聘中
官网直投
我想请教一下像第一题这种一次输入一串数字序列的怎么写成处理多个case的形式,我测试了一下答主的程序,发现一直到false才能有输出。
点赞 回复
分享
发布于 2020-08-13 10:21

相关推荐

6 1 评论
分享
牛客网
牛客企业服务