华为机试HJ1-HJ5
HJ1 字符串最后一个单词的长度
题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,非空,长度小于5000。
输出描述:
整数N,最后一个单词的长度。
示例1
输入
hello world
输出
5
- 调用split方法
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] s = str.split(" "); //正则表达式实用性更强( str.split("\\s+"))
int length = s[s.length - 1].length();
System.out.println(length);
}
- 从尾遍历
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int length = str.length();
int count = 0;
for (int i = length - 1; i >= 0; i--) {
if (str.charAt(i)==' ') { // 或者 if (str.substring(i, i + 1).equals(" ")) {
break;
}
count++;
}
System.out.println(count);
}
HJ2 计算字符个数
题目描述
写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
输入描述:
第一行输入一个有字母和数字以及空格组成的字符串,第二行输入一个字符。
输出描述:
输出输入字符串中含有该字符的个数。
示例1
输入
ABCDEF
A
输出
1
- 遍历字符串
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().toLowerCase();
Character c = sc.nextLine().toLowerCase().charAt(0);
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
count++;
}
}
System.out.println(count);
}
}
- 调用replaceAll方法
import java.util.Scanner;
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();
int count = str.length() - str.replaceAll(s,"").length();
System.out.println(count);
}
}
HJ3 明明的随机数
题目描述
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作(同一个测试用例里可能会有多组数据,希望大家能正确处理)。
Input Param
n 输入随机数的个数
inputArray n个随机整数组成的数组
Return Value
OutputArray 输出处理后的随机整数
注:测试用例保证输入参数的正确性,答题者无需验证。测试用例不止一组。
样例输入解释:
样例有两组测试
第一组是3个数字,分别是:2,2,1。
第二组是11个数字,分别是:10,20,40,32,67,40,20,89,300,400,15。
输入描述:
输入多行,先输入随机整数的个数,再输入相应个数的整数
输出描述:
返回多行,处理后的结果
示例1
输入
3
2
2
1
11
10
20
40
32
67
40
20
89
300
400
15
输出
1
2
10
15
20
32
40
67
89
300
400
- 调用Arrays.sort方法
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
if (i == 0 || array[i] != array[i - 1]) {// 第一个数字或者不等于前一个数字都可以输出
System.out.println(array[i]);
}
}
}
}
}
- 利用TreeSet排序去重的性质
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
TreeSet<Integer> treeSet = new TreeSet<>();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
treeSet.add(sc.nextInt());
}
for (Integer num : treeSet) {
System.out.println(num);
}
}
}
}
HJ4 字符串分隔
题目描述
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(输入2次,每个字符串长度小于100)
输出描述:
输出到长度为8的新字符串数组
示例1
输入
abc
123456789
输出
abc00000
12345678
90000000
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
StringBuilder sb = new StringBuilder(str);
int size = sb.length();
int addZero = 8 - size % 8;
while (addZero > 0 && addZero < 8) {//addzero为[1,7]时,需要加零
sb.append('0');
addZero--;
}
String str1 = sb.toString();
while (str1.length() > 0) {
System.out.println(str1.substring(0, 8));
str1 = str1.substring(8);
}
}
}
}
HJ5 进制转换
题目描述
写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入 )
输入描述:
输入一个十六进制的数值字符串。
输出描述:
输出该数值的十进制字符串。
示例1
输入
0xA
输出
10
- 调用Integer.valueOf方法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
String result = Integer.valueOf(str.substring(2), 16).toString();
System.out.println(result);
}
}
}