华为机试编程题分享又来啦。本次的部分题目也比较简单,你们自由发挥的余地也很大哦。好了,废话很少说,下面就进入正题喽。java
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。算法
输入:合法坐标为A(或者D或者W或者S) + 数字(两位之内),坐标之间以;分隔。非法坐标点须要进行丢弃。如AA10; A1A; $%$; YAD; 等。编程
下面是一个简单的例子 如:安全
A10;S20;W10;D30;X;A1A;B10A11;;A10;app
处理过程:起点(0,0)工具
+ A10 = (-10,0)测试
+ S20 = (-10,-20)ui
+ W10 = (-10,-10)code
+ D30 = (20,-10)orm
+ x = 无效
+ A1A = 无效
+ B10A11 = 无效
+ 一个空 不影响
+ A10 = (10,-10)
结果 (10, -10)
一行字符串
最终坐标,以,分隔
本题给出了当方向为A、D、W、S时,运动的方向,只须要将X坐标或Y坐标进行加/减操做。在处理过程当中,须要进行非法坐标的识别,当坐标值操做两位时为非法,当坐标中含有字母时为非法,当字符串长度大于3时为非法。将这些非法状况分析清楚,本题的代码也就不难完成了。参考代码以下所示。
package HUAWEI; import java.util.Scanner; public class MovePosition { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { String input = scanner.next(); String[] array = input.split(";"); int beginX = 0; int beginY = 0; for(int i = 0; i < array.length; i++) { String current = array[i]; // 数字位2为之内 if(current.length() > 3 || current.length() == 0) { continue; } String gotoWhich = current.substring(0, 1); String distance = current.substring(1); try { Integer.parseInt(distance); } catch (Exception e) { continue; } if(gotoWhich.equals("A")) { beginX -= Integer.parseInt(distance); } else if(gotoWhich.equals("S")) { beginY -= Integer.parseInt(distance); } else if(gotoWhich.equals("W")) { beginY += Integer.parseInt(distance); } else if(gotoWhich.equals("D")) { beginX += Integer.parseInt(distance); } } System.out.println(beginX + "," + beginY); } scanner.close(); } }
请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。
全部的IP地址划分为 A,B,C,D,E五类
A类地址1.0.0.0~126.255.255.255;
B类地址128.0.0.0~191.255.255.255;
C类地址192.0.0.0~223.255.255.255;
D类地址224.0.0.0~239.255.255.255;
E类地址240.0.0.0~255.255.255.255
私网IP范围是:
10.0.0.0~10.255.255.255
172.16.0.0~172.31.255.255
192.168.0.0~192.168.255.255
子网掩码为前面是连续的1,而后全是0。(例如:255.255.255.32就是一个非法的掩码)
本题暂时默认以0开头的IP地址是合法的,好比0.1.1.2,是合法地址
多行字符串。每行一个IP地址和掩码,用~隔开。
统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。
本题是识别输入的IP地址和掩码是否符合要求,主要考虑不合法的状况,如:当不符合IPV4形式的IP均为不合法,某一区间值大于255不合法,子网掩码第一个0以后还含有1也不合法。考虑清楚这些不合法的问题后,就根据题意进行判断相应的A、B、C、D、E类IP地址的个数。
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int typeA = 0; int typeB = 0; int typeC = 0; int typeD = 0; int typeE = 0; int errorIpOrMaskCode = 0; int privIp = 0; while (scanner.hasNext()) { String ipt = scanner.nextLine(); String[] ipAndMaskCode = ipt.split("~"); String ip = ipAndMaskCode[0]; String maskCode = ipAndMaskCode[1]; // 判断格式 if (!isValidFormat(ip) || !isValidFormat(maskCode)) { errorIpOrMaskCode++; continue; } // 判断掩码是否错误 if (!validMaskCode(maskCode)) { errorIpOrMaskCode++; continue; } // 判断ip类别 String fnStr = ip.substring(0, ip.indexOf(".")); int fn = Integer.valueOf(fnStr); if (fn >= 1 && fn < 127) { // A typeA++; } else if (fn >= 128 && fn < 192) { // B typeB++; } else if (fn >= 192 && fn < 224) { // C typeC++; } else if (fn >= 224 && fn < 240) { // D typeD++; } else if (fn >= 240 && fn <= 255) { // E typeE++; } // 判断是不是私网IP String ipSubStr = ip.substring(ip.indexOf(".") + 1); String snStr = ipSubStr.substring(0, ipSubStr.indexOf(".")); int sn = Integer.valueOf(snStr); if (fn == 10 || (fn == 172 && sn >= 16 && sn <= 31) || (fn == 192 && sn == 168)) { privIp++; } // System.out.printf("%d %d%n", fn, sn); } scanner.close(); System.out.printf("%d %d %d %d %d %d %d%n", typeA, typeB, typeC, typeD, typeE, errorIpOrMaskCode, privIp); } /** * 判断ip和掩码是不是xxx.xxx.xxx.xxx格式Ø * @param ip * @return */ private static boolean isValidFormat(String ip) { boolean res = true; if (ip == null || "".equals(ip)) return false; Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$"); Matcher matcher = pattern.matcher(ip); if (matcher.matches()) { String[] nums = ip.split("\\."); for (String num : nums) { int n = Integer.valueOf(num); if (n < 0 || n > 255) { res = false; break; } } } else { res = false; } return res; } /** * 判断掩码是不是前面全为1后面全为0 的格式 * * @param maskCode * @return */ private static boolean validMaskCode(String maskCode) { boolean res = true; String[] nums = maskCode.split("\\."); StringBuilder sb = new StringBuilder(); for (String num : nums) { int n = Integer.valueOf(num); sb.append(binaryString(n)); } int firstIndexOf0 = sb.indexOf("0"); int lastIndexOf1 = sb.lastIndexOf("1"); if (firstIndexOf0 < lastIndexOf1) { res = false; } return res; } /** * 将整数转成对应的八位二进制字符串 * @param num * @return */ private static String binaryString(int num) { StringBuilder result = new StringBuilder(); int flag = 1 << 7; for (int i = 0; i < 8; i++) { int val = (flag & num) == 0 ? 0 : 1; result.append(val); num <<= 1; } return result.toString(); } }
密码是咱们生活中很是重要的东东,咱们的那么一点不能说的秘密就全靠它了。哇哈哈. 接下来渊子要在密码之上再加一套密码,虽然简单但也安全。
假设渊子原来一个BBS上的密码为zvbo9441987,为了方便记忆,他经过一种算法把这个密码变换成YUANzhi1987,这个密码是他的名字和出生年份,怎么忘都忘不了,并且能够明目张胆地放在显眼的地方而不被别人知道真正的密码。
他是这么变换的,你们都知道手机上的字母: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,就这么简单,渊子把密码中出现的小写字母都变成对应的数字,数字和其余的符号都不作变换,
声明:密码中没有空格,而密码中出现的大写字母则变成小写以后日后移一位,如:X,先变成小写,再日后移一位,不就是y了嘛,简单吧。记住,z日后移是a哦。
输入包括多个测试数据。输入是一个明文,密码长度不超过100个字符,输入直到文件结尾
输出渊子真正的密文
本题能够用Map存储每个小写字母密文对应的明文,Map的查询速度也是极快的。参考代码以下所示。
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { private static Map<Character, Integer> map; static { map = new HashMap<>(); map.put('a', 2); map.put('b', 2); map.put('c', 2); map.put('d', 3); map.put('e', 3); map.put('f', 3); map.put('g', 4); map.put('h', 4); map.put('i', 4); map.put('j', 5); map.put('k', 5); map.put('l', 5); map.put('m', 6); map.put('n', 6); map.put('o', 6); map.put('p', 7); map.put('q', 7); map.put('r', 7); map.put('s', 7); map.put('t', 8); map.put('u', 8); map.put('v', 8); map.put('w', 9); map.put('x', 9); map.put('y', 9); map.put('z', 9); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { String input = scanner.next(); changeInput(input); } scanner.close(); } private static void changeInput(String input) { StringBuffer buffer = new StringBuffer(); for(int i = 0; i < input.length(); i++) { char current = input.charAt(i); if(current >= 'A' && current <= 'Z') { current = Character.toLowerCase(current); current = (char) (current + 1); if(current > 'z') { current = 'a'; } buffer.append(current); } else if(current >= 'a' && current <= 'z') { buffer.append(map.get(current)); } else { buffer.append(current); } } System.out.println(buffer.toString()); } }
每一周的华为笔试都在周三的晚上有条不紊的进行,机会老是留给有准备的人,它就在哪里,只要实力足够,它就是你的。我相信经过不断的练习,本身的算法能力是能有所提升的。加油了,你们。