华为机试题库_在线编程+题解_牛客题霸_牛客网 (nowcoder.com)
HJ1 字符串最后一个单词的长度 题目大意:
计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。
思路:
从后往前找,但是好像没必要我这么复杂,测试集好像确保了都是字母和空格。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int len = str.length(), k = 0 ; for (int i = len - 1 ; i >= 0 ; i--) { while (i >= 0 && Character.isAlphabetic(str.charAt(i))) { k++; i--; } if (k != 0 ) { System.out.println(k); break ; } } if (k == 0 ) { System.out.println(0 ); } } }
HJ2 计算字符个数 题目大意:
写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字母,然后输出输入字符串中该字母的出现次数。不区分大小写,字符串长度小于500。
代码:
1 2 3 4 5 6 7 8 9 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine().toLowerCase(); String s = sc.nextLine().toLowerCase(); System.out.println(str.length() - str.replaceAll(s,"" ).length()); } }
HJ3 明明的随机数 思路:
去重、排序 ==》 TreeSet
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int N = sc.nextInt(); TreeSet<Integer> set = new TreeSet<>(); while (N > 0 ) { set.add(sc.nextInt()); N--; } for (int id : set) { System.out.println(id); } } } }
HJ4 字符串分隔 题目大意:
连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str = sc.next(); StringBuffer sb = new StringBuffer(str); if (str.length() == 0 ) { continue ; } int delta = 8 - str.length() % 8 ; while (delta > 0 && delta < 8 ) { sb.append("0" ); delta--; } str = sb.toString(); while (str.length() > 0 ) { System.out.println(str.substring(0 , 8 )); str = str.substring(8 ); } } } }
HJ5 进制转换 题目大意:
写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String hex = sc.nextLine().toUpperCase(); if ("0X" .equals(hex.substring(0 ,2 ))) { hex = hex.substring(2 ); } int dec = 0 , order = 1 ; for (int i = hex.length() - 1 ; i >= 0 ; i--) { dec += charToInt(hex.charAt(i)) * order; order *= 16 ; } System.out.println(dec); } } public static int charToInt (char ch) { switch (ch) { case 'A' : return 10 ; case 'B' : return 11 ; case 'C' : return 12 ; case 'D' : return 13 ; case 'E' : return 14 ; case 'F' : return 15 ; default : return ch - '0' ; } } }
HJ6 质数因子 题目大意:
功能:输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )
最后一个数后面也要有空格
思路:
最小的质数是 2,于是我们从因子 factor = 2
开始看 num
能否被 factor
给整除。(这里不用判断每个 factor
是不是质数的原因是,如果不是质数,那么一定在之前就被比它小的质数给分解了,比如说 4,在之前已经被分解成了 2 * 2,并且加上了判断的话会增加时间复杂度。)
但是这个题恶心的地方在于,它给的测试样例会有超大的质数,所以需要在最后加一个额外处理,除到最后没除完,剩下的直接输出。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import java.util.*;public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); long num = sc.nextLong(); for (long factor = 2 ; num > 1 && factor * factor <= num; factor++) { while (num % factor == 0 ) { System.out.print(factor + " " ); num /= factor; } } if (num != 1 ) { System.out.print(num + " " ); } System.out.println(); } }