写在前面
小记一下之前参加华为2018届勇敢星实习生招聘的笔试题(研发类)。目前已经在华为公司实习了近2个月。 本文原发于个人csdn博客:http://m.blog.csdn.net/Jacky_chenjp/article/details/73433824
我是在微信上投的Android研发实习生岗,很快就收到通知,于3.24晚上参加的在线笔试。今年华为的实习生在线笔试是外包给牛客网来做的。
华为的研发笔试好像一直以来都是三道题,100+200+300一共600分。个人感觉,相比于其他互联网公司的笔试题,华为的题目难度还是和很简单的,都是些很基础的算法实现。博主花了一个小时候出头,全AC了。
笔试题
贴一下题目。当时因为想好会写博客,截了下题图。华为的笔试分了N多场,每场题都不一样,但大抵都是这个难度。
题1:整数翻转求和
题1-1.JPG题1-2.JPG
这题有点小坑,输入的时候,两个数字之间有个英文逗号。一开始写的时候,死都过不了,折腾了好久。后来加了个逗号就过了。题目本身不难,贴上我的AC代码,用C写的:
int reverse(int x) {
int a = 0;
do {
a = a * 10 + (x % 10);
x /= 10;
} while(x > 0);
return a;
}
int reverseAdd(int a, int b) {
if (a < 1 || a > 70000 || b < 1 || b > 70000) {
return -1;
}
return reverse(a) + reverse(b);
}
#include "stdio.h"
int main() {
int a, b;
scanf("%d,%d", &a, &b);
printf("%d\n", reverseAdd(a, b));
return 0;
}
题2:掷骰子
题2-1.JPG题2-2.JPG
这题思路也很简单,读到啥就转啥就行了。注意到,每一次翻转的时候,有两个方向的是不会变的,其他的四个方向也只是机械地进行交换。贴上AC代码,用Java写的:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main pMain = new Main();
pMain.input();
}
private void input() {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String x = in.nextLine();
solve(x);
}
in.close();
}
private void solve(String x) {
char[] a = x.toCharArray();
int[] res = {0,1,2,3,4,5,6};
for (int i=0; i<a.length; i++) {
char swch = a[i];
if (swch == 'L') {
int temp = res[1];
res[1] = res[5];
res[5] = res[2];
res[2] = res[6];
res[6] = temp;
} else if (swch == 'R') {
int temp = res[1];
res[1] = res[6];
res[6] = res[2];
res[2] = res[5];
res[5] = temp;
} else if (swch == 'B') {
int temp = res[3];
res[3] = res[6];
res[6] = res[4];
res[4] = res[5];
res[5] = temp;
} else if (swch == 'F') {
int temp = res[3];
res[3] = res[5];
res[5] = res[4];
res[4] = res[6];
res[6] = temp;
} else if (swch == 'A') {
int temp = res[1];
res[1] = res[4];
res[4] = res[2];
res[2] = res[3];
res[3] = temp;
} else if (swch == 'C') {
int temp = res[1];
res[1] = res[3];
res[3] = res[2];
res[2] = res[4];
res[4] = temp;
}
}
for (int i=1; i<=6; i++) {
System.out.print(res[i]);
}
System.out.println("");
}
}
题3:当出差遇上大雾
题3-1.JPG题3-2.JPG
题3-3.JPG
这题思路也很简单,其实就是个DFS。不过想着容易,真正码完调试还是着实费了些功夫的,写的代码时候,细心很重要。贴上AC代码,用C写的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int inf = 0x7f7f7f7f;
int d[50];
int a[][7] = {
{0},
{0, 0, 2, 10, 5, 3, -1},
{0, -1, 0, 12, -1, -1, 10},
{0, -1, -1, 0, -1, 7, -1},
{0, 2, -1, -1, 0, 2, -1},
{0, 4, -1, -1, 1, 0, -1},
{0, 3, -1, 1, 0, 2, 0},
};
int x, y;
int res;
int used[10];
int ans = inf;
int ansp;
int ansd[50];
void dfs(int now, int cost, int p) {
if (now == x) {
if (cost < ans) {
ans = cost;
for (int i = 0; i < p; i++) {
ansd[i] = d[i];
}
ansp = p;
}
return;
}
for (int i = 1; i <= 6; i++) {
if (i == y) {
continue;
}
if (used[i] == 0 && a[now][i] >= 0) {
used[i] = 1;
d[p] = i;
dfs(i, cost + a[now][i], p + 1);
used[i] = 0;
}
}
}
int main() {
static char emp[] = "";
static char col[] = ", ";
scanf("%d%d", &x, &y);
if (x == 5) {
printf("0\n[]\n");
return 0;
}
if (y == 5) {
printf("1000\n[]\n");
return 0;
}
d[0] = 5;
used[5] = 1;
dfs(5, 0, 1);
char *p = emp;
if (ans == inf) {
printf("1000\n[]\n");
} else {
printf("%d\n[", ans);
for (int i = 0; i < ansp; i++) {
printf("%s%d", p, ansd[i]);
p = col;
}
printf("]\n");
}
}
网友评论