华为笔试题(8)

来源:互联网 发布:极简主义 知乎 编辑:程序博客网 时间:2024/05/16 04:39

一、
1、对输入的字符串进行加解密,并输出。
2、加密方法为:
当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
其他字符不做变化。
3、解密方法为加密的逆过程
输入描述:
输入说明
输入一串要加密的密码
输入一串加过密的密码
输出描述:
输出说明
输出加密后的字符
输出解密后的字符
示例1
输入
abcdefg
BCDEFGH
输出
BCDEFGH
abcdefg

public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        //枚举法,无脑手速快        while(sc.hasNext()) {            String encode = sc.next();            String decode = sc.next();            System.out.println(encoding(encode.toCharArray()));            System.out.println(decoding(decode.toCharArray()));        }        sc.close();    }    public static String encoding(char[] chs){        for(int i = 0; i < chs.length; i++) {            if(chs[i] == 'z') {                chs[i] = 'A';            }else if(chs[i] == 'Z') {                chs[i] = 'a';            }else if(chs[i] == '9') {                chs[i] = '0';            }else if(chs[i] >= '0' && chs[i] <= '8') {                chs[i]++;            }else if(chs[i] >= 'a' && chs[i] <= 'y') {                chs[i] += 'A' - 'a' + 1;            }else if(chs[i] >= 'A' && chs[i] <= 'Y') {                chs[i] -= 'A' - 'a' - 1;            }        }        return new String(chs);    }    public static String decoding(char[] chs){        for(int i = 0; i < chs.length; i++) {            if(chs[i] == 'A') {                chs[i] = 'z';            }else if(chs[i] == 'a') {                chs[i] = 'Z';            }else if(chs[i] == '0') {                chs[i] = '9';            }else if(chs[i] >= '1' && chs[i] <= '9') {                chs[i]--;            }else if(chs[i] >= 'B' && chs[i] <= 'Z') {                chs[i] -= 'A' - 'a' + 1;            }else if(chs[i] >= 'b' && chs[i] <= 'z') {                chs[i] += 'A' - 'a' - 1;            }        }        return new String(chs);    }}

二、
按照指定规则对输入的字符串进行处理。
详细描述:
将输入的两个字符串合并。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。
这里的下标意思是字符在字符串中的位置。
对排序后的字符串进行操作
如果字符为‘0’——‘9’或者‘A’——‘F’或者‘a’——‘f’,
则对他们所代表的16进制的数进行BIT倒序的操作,并转换为相应的大写字符。
如字符为‘4’,为0100b,则翻转后为0010b,也就是2。转换后的字符为‘2’;
如字符为‘7’,为0111b,则翻转后为1110b,也就是e。转换后的字符为大写‘E’。
输入描述:
输入两个字符串
输出描述:
输出转化后的结果
示例1
输入
dec fab
输出
5D37BF

public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        while(sc.hasNext()) {            String str1 = sc.next();            String str2 = sc.next();            String str = str1 + str2;            char[] chs = str.toCharArray();            sort(chs);            System.out.println(reverse(chs));        }        sc.close();    }    //分别对奇数和偶数做插入排序    public static void sort(char[] chs){        for(int i = 0; i < 2; i++) {            for(int j = i + 2; j < chs.length; j += 2) {                int index = j;                char tmp = chs[j];                while(index - 2 >= 0 && chs[index - 2] > tmp) {                    chs[index] = chs[index - 2];                    index -= 2;                }                chs[index] = tmp;            }              }    }    //照着题目要求一步一步来    public static String reverse(char[] chs) {        StringBuffer sb = new StringBuffer();        for(int i = 0; i < chs.length; i++) {            if(chs[i] >= '0' && chs[i] <= '9' ||                     chs[i] >= 'a' && chs[i] <= 'f'||                    chs[i] >= 'A' && chs[i] <= 'F') {                int temp = Integer.parseInt(chs[i]+"", 16);//转16进制                String binary = Integer.toBinaryString(temp);//转2进制,并在前面补零,保证每位                binary = "0000" + binary;//前面补0,之后截取,保证每个数都有四位                binary = binary.substring(binary.length() - 4);//截取                StringBuffer tmp = new StringBuffer(binary);                tmp = tmp.reverse();//逆转                int binaryInt = Integer.parseInt(tmp.toString(), 2);//转为2进制数                String reverse = Integer.toHexString(binaryInt).toUpperCase();//转换为大写字符                sb.append(reverse);            }else{                sb.append(chs[i]);            }        }        return sb.toString();    }}

三、
对字符串中的所有单词进行倒排。
说明:
1、每个单词是以26个大写或小写英文字母构成;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
4、每个单词最长20个字母;
输入描述:
输入一行以空格来分隔的句子
输出描述:
输出句子的逆序
示例1
输入
I am a student
输出
student a am I

public class Main{    public static void main(String[] args) {        Scanner sc=new Scanner(System.in);        while(sc.hasNext()) {            String str = sc.nextLine();            String[] strs = reverse(str).split("[^a-zA-z]");            for(int i = 0; i < strs.length; i++) {                strs[i] = reverse(strs[i]);            }            for(int i = 0; i < strs.length - 1; i++) {                if(!strs[i].matches("")) {                    System.out.print(strs[i]+" ");                }            }            if(strs[strs.length - 1] != "") {                System.out.println(strs[strs.length - 1]);            }        }        sc.close();    }    private static String reverse(String str) {        char[] chs = str.toCharArray();        for(int i = 0, j = chs.length - 1; i < j; i++, j--) {            char tmp = chs[i];            chs[i] = chs[j];            chs[j] = tmp;        }        return new String(chs);    }}

四、
输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37…70,71,72,73…)的个数
输入描述:
一个正整数N。(N不大于30000)
输出描述:
不大于N的与7有关的数字个数,例如输入20,与7有关的数字包括7,14,17.
示例1
输入
20
输出
3

public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        while(sc.hasNext()){            int n = sc.nextInt();            System.out.println(process(n));        }        sc.close();    }   public static int process(int n){        int res = 0;        for(int i = 1; i <= n; i++) {            if(i % 7 == 0) {                res++;            }else {                if(String.valueOf(i).contains("7")) {                    res++;                }            }        }        return res;    }}

五、
Lily上课时使用字母数字图片教小朋友们学习英语单词,
每次都需要把这些图片按照大小(ASCII码值从小到大)排列收好。
输入描述:
Lily使用的图片包括”A”到”Z”、”a”到”z”、”0”到”9”。输入字母或数字个数不超过1024。
输出描述:
Lily的所有图片按照从小到大的顺序输出
示例1
输入
Ihave1nose2hands10fingers
输出
0112Iaadeeefghhinnnorsssv

public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        //只有数字和字母,可以用桶排序        while(sc.hasNext()){            String str = sc.next();            int[] bit = new int[128];            int len = str.length();            for(int i = 0; i < len; i++) {                bit[str.charAt(i)]++;            }            StringBuffer sb = new StringBuffer();            for(int i = 0; i < 128; i++) {                while(bit[i] != 0) {                    sb.append((char)i);                    bit[i]--;                }            }            System.out.println(sb.toString());        }        sc.close();    }}

六、
有一种技巧可以对数据进行加密,它使用一个单词作为它的密匙。下面是它的工作原理:
首先,选择一个单词作为密匙,如TRAILBLAZERS。
如果单词中包含有重复的字母,只保留第1个,其余几个丢弃。
现在,修改过的那个单词属于字母表的下面,如下所示:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
T R A I L B Z E S C D F G H J K M N O P Q U V W X Y
上面其他用字母表中剩余的字母填充完整。在对信息进行加密时,信息中的每个字母被固定于顶上那行,
并用下面那行的对应字母一一取代原文的字母(字母字符的大小写状态应该保留)。
因此,使用这个密匙,Attack AT DAWN(黎明时攻击)就会被加密为Tpptad TP ITVH。
输入描述:
先输入key和要加密的字符串
输出描述:
返回加密后的字符串
示例1
输入
nihao ni
输出
le

public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        while(sc.hasNext()){            String key = sc.nextLine();            String password = sc.nextLine();            //建立对照表            List<Character> list = new LinkedList<>();            //去重            for (int i = 0; i < key.length(); i++){                if (!list.contains(key.charAt(i))){                    list.add(key.charAt(i));                }            }            //加上剩余的            for (int i = 97; i < 123; i++){                if (!list.contains((char) i)){                    list.add((char) i);                }            }            //开始翻译            StringBuffer sb = new StringBuffer();            for (int i = 0; i < password.length(); i++){                sb.append(list.get(password.charAt(i) - 'a'));            }            System.out.println(sb.toString());        }        sc.close();    }}

七、
假设一个球从任意高度自由落下,每次落地后反跳回原高度的一半;
再落下, 求它在第5次落地时,共经历多少米?第5次反弹多高?
输入描述:
输入起始高度,int型
输出描述:
分别输出第5次落地时,共经过多少米,第5次反弹多高
示例1
输入
1
输出
2.875
0.03125

public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        while(sc.hasNext()){            int init = sc.nextInt();            double through = init;            int n = 4;            double tmp = init;            while(n-- > 0) {                tmp /= 2;                through += tmp * 2;            }            System.out.println(through);            System.out.println(tmp / 2);        }        sc.close();    }}

八、
输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
输入描述:
输入一行字符串,可以有空格
输出描述:
统计其中英文字符,空格字符,数字字符,其他字符的个数
示例1
输入
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\/;p0-=\][
输出
26
3
10
12

public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        while(sc.hasNext()){            String str = sc.nextLine();            char chs[] = str.toCharArray();            int letter = 0, space = 0, digit = 0, other = 0;            for(int i = 0; i < chs.length; i++) {                if(chs[i] >= 'a' && chs[i] <= 'z' || chs[i] >= 'A' && chs[i] <= 'Z') {                    letter++;                }else if(chs[i] >= '0' && chs[i] <= '9') {                    digit++;                }else if(chs[i] == ' ') {                    space++;                }else {                    other++;                }            }            System.out.println(letter);            System.out.println(space);            System.out.println(digit);            System.out.println(other);        }        sc.close();    }}

九、
子网掩码是用来判断任意两台计算机的IP地址是否属于同一子网络的根据。
子网掩码与IP地址结构相同,是32位二进制数,其中网络号部分全为“1”和主机号部分全为“0”。
利用子网掩码可以判断两台主机是否中同一子网中。
若两台主机的IP地址分别与它们的子网掩码相“与”后的结果相同,则说明这两台主机在同一子网中。
示例:
I P 地址  192.168.0.1
子网掩码  255.255.255.0
转化为二进制进行运算:
I P 地址 11010000.10101000.00000000.00000001
子网掩码 11111111.11111111.11111111.00000000
AND运算
    11000000.10101000.00000000.00000000
转化为十进制后为:
    192.168.0.0
I P 地址  192.168.0.254
子网掩码  255.255.255.0
转化为二进制进行运算:
I P 地址 11010000.10101000.00000000.11111110
子网掩码 11111111.11111111.11111111.00000000
AND运算
     11000000.10101000.00000000.00000000
转化为十进制后为:
     192.168.0.0
通过以上对两台计算机IP地址与子网掩码的AND运算后,我们可以看到它运算结果是一样的。
均为192.168.0.0,所以这二台计算机可视为是同一子网络。
* 功能: 判断两台计算机IP地址是同一子网络。
* 输入参数: String Mask: 子网掩码,格式:“255.255.255.0”;
* String ip1: 计算机1的IP地址,格式:“192.168.0.254”;
* String ip2: 计算机2的IP地址,格式:“192.168.0.1”;
*
* 返回值: 0:IP1与IP2属于同一子网络; 1:IP地址或子网掩码格式非法; 2:IP1与IP2不属于同一子网络
输入描述:
输入子网掩码、两个ip地址
输出描述:
得到计算结果
示例1
输入
255.255.255.0 192.168.224.256 192.168.10.4
输出
1

public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        while(sc.hasNext()){            String mask = sc.next();            String ip1 = sc.next();            String ip2 = sc.next();            System.out.println(process(mask, ip1, ip2));        }        sc.close();    }    private static int process(String mask, String ip1, String ip2) {        String[] masks = mask.split("\\.");        String[] ip1s = ip1.split("\\.");        String[] ip2s = ip2.split("\\.");        if(masks.length != 4 || ip1s.length != 4 || ip2s.length != 4) {            return 1;        }        //先非法判断        for(int i = 0; i < 4; i++) {            int maskInt = Integer.valueOf(masks[i]);            int ip1Int = Integer.valueOf(ip1s[i]);            int ip2Int = Integer.valueOf(ip2s[i]);            if(maskInt < 0 || maskInt > 255 ||                    ip1Int < 0 || ip1Int > 255 ||                    ip2Int < 0 || ip2Int > 255) {                return 1;            }        }        for(int i = 0; i < 4; i++) {            int maskInt = Integer.valueOf(masks[i]);            int ip1Int = Integer.valueOf(ip1s[i]);            int ip2Int = Integer.valueOf(ip2s[i]);            if((ip1Int & maskInt) != (ip2Int & maskInt)) {                return 2;            }        }        return 0;    }}

十、
有一只兔子,从出生后第3个月起每个月都生一只兔子,
小兔子长到第三个月后每个月又生一只兔子,假如兔子都不死,问每个月的兔子总数为多少?
输入描述:
输入int型表示month
输出描述:
输出兔子总数int型
示例1
输入
9
输出
34

public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        /* 0  1  2  3  4  5  6  7  8  9         * 0  1  1  2  3  5  8  13 21 34         * f(n) = f(n-1) + f(n-2) while(n >= 4)         */        while(sc.hasNext()){            int n = sc.nextInt();            //斐波那契数列类似问题,用动态规划法            int[] dp = new int[n + 1];            dp[0] = 0;            dp[1] = 1;            for(int i = 2; i <= n; i++) {                dp[i] = dp[i - 1] + dp[i - 2];            }            System.out.println(dp[n]);        }        sc.close();    }}
原创粉丝点击