9月18日华为笔试题


20190918 华为笔试:
题目1:
判断数据是否合理,给了三种合理的情况,一个是两位数与一位数交替出现,一个是两头是两位数,中间全是一位数,最后一种情况是两头是一位数,中间全是两位数。

解题思路:
因为看到数据不会太多,直接做了最复杂的判断,就是将三种情况分别判断,最后的结果进行或操作,输出即可。

题目2:
字符串整理并倒序,将字符串中的大写字符全部转为小写,并将空格转为0,最后字符倒序输出。

解题思路:
从后向前操作,结果用StringBuilder不断的追加,最后调用toString方法进行输出。

题目3:
题目很复杂的一道题,但是其实细细看来没那么难,大概就是分了五种情况来进行区别对待,然后使用的数据结构是一个所谓的环式服务器之类的,感觉题目不难,但是最后只AC了80%……

解题思路:
我是用了一个数组来存,构建了多个函数,一个是计算哈希值,一个是找到下一个可用的服务器,一个是添加服务器,设置一个布尔参数来判断是否是新添加的服务器,然后再写一个宕机服务器方法,最后按照情况,对字符串进行处理即可。


总结:
题目不难,第三题一看很吓人,细细分析来其实比较简单。最后AC了2.8,看大佬都在说,可能没什么HC,拿来糊弄人的笔试吧。、
个人感觉题目偏简单,考的东西也不清楚,很基础。。。不过还是期待可以有下一面吧


最后:需要的话我可以楼下贴代码。。。。写的烂,勿喷。


#华为##笔试题目#
全部评论
第一题怎么输入
点赞 回复
分享
发布于 2019-09-18 23:35
我2.2 感觉确实偏简单
点赞 回复
分享
发布于 2019-09-18 23:46
滴滴
校招火热招聘中
官网直投
第一条请教了,我在输入那边遇到了麻烦,不知道什么时候是结束输入
点赞 回复
分享
发布于 2019-09-18 23:50
看到有好多人都在纠结输入,我就贴一下输入的代码吧。。。 public static void main(String[] args) throws IOException { System.setIn(new BufferedInputStream(new FileInputStream("resource/huawei0001.txt"))); Scanner sc = new Scanner(System.in); ArrayList<Boolean> list = new ArrayList<>(); while (sc.hasNext()) { String[] temp = sc.nextLine().split(" "); int[] arr = new int[temp.length]; for (int i = 0; i < arr.length; i++) { arr[i] = Integer.parseInt(temp[i]); } list.add(checkOne(arr) || checkTwo(arr)); } for (int i = 0; i < list.size() - 1; i++) { System.out.print(list.get(i) + " "); } System.out.print(list.get(list.size() - 1)); }
点赞 回复
分享
发布于 2019-09-18 23:56
卧槽 还有文件 我怎么办没有看到
点赞 回复
分享
发布于 2019-09-18 23:59
第三题如果命令1查询的节点没有被插入应该输出什么啊,也没说输出用啥隔开,输入的指令之间是不是也没有分割的?看题看的一脸懵逼,总觉得一些关键点都没说明白,规则倒是简单的很
点赞 回复
分享
发布于 2019-09-19 13:26
没hc不该考难点的题让大家知难而退么...
点赞 回复
分享
发布于 2019-09-22 00:01
package huawei; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; public class Q0003_serverArrange { /* * 0:无服务器 * 1:有服务器 * -1:服务器宕机 * * 初始服务器:以 0位初始,50为间隔, * 增添服务器:以25为初始,若加500不溢出,则加500,否则加50 */ public static void main(String[] args) throws IOException { System.setIn(new BufferedInputStream(new FileInputStream("resource/huawei0003.txt"))); Scanner sc = new Scanner(System.in); String[] command = sc.nextLine().split(":"); int[] servers = new int[1000]; arrageServer(servers, 20, false); int index; String[] temp; switch (command[0]) { case "1": temp = command[1].split("_"); index = Integer.parseInt(temp[temp.length - 1]); System.out.println((index - 1) * 50); break; case "2": index = getHash(command[1]); System.out.println(findServer(servers, index)); break; case "3": //处理宕机服务器 temp = command[1].split(";"); String[] shutdowns = temp[0].split(","); for (int i = 0; i < shutdowns.length; i++) { String[] curTemp = shutdowns[i].split("_"); shutdownServer(servers, Integer.parseInt(curTemp[curTemp.length - 1])); } index = getHash(temp[temp.length - 1]); System.out.println(findServer(servers, index)); break; case "4": temp = command[1].split("_"); index = Integer.parseInt(temp[temp.length - 1]); System.out.println((index - 1) * 50 + 25); break; case "5": temp = command[1].split(";"); String[] addServers = temp[0].split("_"); arrageServer(servers, Integer.parseInt(addServers[addServers.length - 1]), true); index = getHash(temp[1]); System.out.println(findServer(servers, index)); break; } } // 计算token的哈希值 public static int getHash(String str) { int res = 0; for (int i = 0; i < str.length(); i++) { res += (int) str.charAt(i); res %= 999; } return res; } // 查找分配服务器位置 public static int findServer(int[] arr, int index) { if (arr[index] != 0) return index; while (true) { index = (index + 1) % arr.length; if (arr[index] == 1) return index; } } // 设置宕机服务器 public static void shutdownServer(int[] arr, int index) { arr[(index - 1) * 50 + 0] = -1; } // 安排服务器 public static void arrageServer(int[] arr, int num, boolean isAdd) { int gap = 50; if (!isAdd) { //首次添加 for (int i = 0; i < num; i++) { arr[i * gap] = 1; } } else { int index = 25; arr[index] = 1; for (int i = 1; i < num; i++) { index = index + 500 > 1000 ? index - 450 : index + 500; arr[index] = 1; } } } }
点赞 回复
分享
发布于 2019-09-22 23:16
package huawei; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class Q0001_checkNumber { public static void main(String[] args) throws IOException { System.setIn(new BufferedInputStream(new FileInputStream("resource/huawei0001.txt"))); Scanner sc = new Scanner(System.in); ArrayList<Boolean> list = new ArrayList<>(); while (sc.hasNext()) { String[] temp = sc.nextLine().split(" "); int[] arr = new int[temp.length]; for (int i = 0; i < arr.length; i++) { arr[i] = Integer.parseInt(temp[i]); } list.add(checkOne(arr) || checkTwo(arr)); } for (int i = 0; i < list.size() - 1; i++) { System.out.print(list.get(i) + " "); } System.out.print(list.get(list.size() - 1)); } // 验证两位数与一位数交错出现 public static boolean checkOne(int[] arr) { // 判断第一个数是否是一位数 boolean check = arr[0] / 10 == 0; int index = 1; while (index < arr.length) { // 判断当前数是否是1位数 boolean curCheck = arr[index] / 10 == 0; // 若跟前一数据相同,则不符合条件 if (curCheck == check) return false; else check = curCheck; index++; } return true; } // 验证两端是两位数,中间全部为一位数 public static boolean checkTwo(int[] arr) { boolean fisrt = arr[0] / 10 != 0; boolean end = arr[arr.length - 1] / 10 != 0; if (fisrt && end) { // 第一个数据与最后一个数据均为2位数时 if(arr.length == 2) return true; // 判断中间数据是否全部是一位数 for (int i = 1; i <= arr.length - 2; i++) { boolean curCheck = arr[i] / 10 == 0; if (!curCheck) return false; } } else if(!fisrt && !end){ // 第一个数据与最后一个数据均为1位数时 if(arr.length == 2) return true; //判断中间数据是否均为2位数 for (int i = 1; i <= arr.length - 2; i++) { boolean curCheck = arr[i] / 10 != 0; if (!curCheck) return false; } } else { return false; } return true; } }
点赞 回复
分享
发布于 2019-09-26 10:43

相关推荐

3 32 评论
分享
牛客网
牛客企业服务