语言: java
牛客网华为机试地址
HJ1 字符串最后一个单词的长度
import java.util.*;
public class Main {
// 解题思路: 求最后一个单词的长度, 那么只需要把最后一个单词提取出来即可.
// 怎么提取? 给两个标位, 首i, 尾j. j默认置为字符串下标0的位置, 此时反向遍历字符串, 当j==0的时候, 并且是字母, 就把单词末尾位置j, 置为此时下标.
// 确认了单词末尾, 在确认单词首位, 继续遍历, 遇到空格, 就可以确定是首位, 如果没有遇到首位, 直接遍历到了字符串起始, 那么这整个字符串就是一个单词.
// 最后输出单词长度.
// tips: 我之前写的是完整思路, 代码思路是可以简化的, 比如把字符串去空格后, j默认就可以置为字符串末尾
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().trim();
int j = 0; // 单词末尾
char c;
for (int i = str.length() - 1; i >= 0; i--) {
c = str.charAt(i);
if (j == 0) {
j = i;//65~90
} else if (c == ' ') {
System.out.println(str.substring(i + 1, j + 1).length());
break;
} else if (i == 0) {
System.out.println(str.substring(i, j + 1).length());
break;
}
}
}
}

HJ2 计算某字母出现次数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] origin = sc.nextLine().toUpperCase().toCharArray();
char target = sc.nextLine().toUpperCase().charAt(0);
int result = 0;
for (int i = 0; i < origin.length; i++) {
if (target == origin[i]) {
result++;
}
}
System.out.println(result);
}
}

HJ3 明明的随机数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int len, temp, i, j, k, j1, k1;
int[] arr;
while(sc.hasNext()){
len = sc.nextInt();
arr = new int[len];
for ( i = 0; i < len; i++) {
arr[i] = sc.nextInt();
// 一边输入, 一边去重和排序 1 2 3 0 0 4
for (j = i - 1, k = i; j >= 0; j--, k--) {
j1=arr[j];
k1=arr[k];
if (j1 > k1) {
temp = j1;
arr[j] = k1;
arr[k] = temp;
} else if (j1 == k1 && k1 > 0) {
arr[j] = 0;
}
}
}
for (i = 0; i < len; i++) {
if ((temp = arr[i]) > 0) {
System.out.println(temp);
}
}
}
}
}

HJ4 字符串分隔
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n, len;
String temp;
while (sc.hasNextLine()) {
temp = sc.nextLine();
if ((len = temp.length()) > 0) {
if (len % 8 > 0) {
temp += "0000000";
n = len / 8 + 1;
}else {
n = len / 8;
}
for (int i = 0; i < n; i++) {
System.out.println(temp.substring(i * 8, i * 8 + 8));
}
}
}
}
}

HJ5 进制转换
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String temp;
char c;
while (sc.hasNextLine()) {
temp = sc.nextLine();
int result = 0;
for (int i = temp.length() - 1; i > 1; i--) {
c = temp.charAt(i);
if (c >= 65) {
result += (c - 55) * Math.pow(16, temp.length() - i - 1);
} else {
result += (c - 48) * Math.pow(16, temp.length() - i - 1);
}
}
System.out.println(result);
}
}
}

HJ6 质数因子
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int temp = 0;
while (n > 1) {
if (n % 2 == 0) {
n = n / 2;
System.out.print(2 + " ");
continue;
}
if (n == 3) {
System.out.print(n + " ");
return;
} else {
for (int i = 3; i < Math.sqrt(n) + 1; i += 2) {
temp = n;
if (n % i == 0) {
n = n / i;
System.out.print(i + " ");
break;
}
}
if (temp == n) {
System.out.print(n + " ");
return;
}
}
}
sc.close();
}
}

HJ7 取近似值
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Float n = sc.nextFloat();
int temp;
if (n - (temp = n.intValue()) >= 0.5) {
System.out.println(temp + 1);
} else {
System.out.println(temp);
}
}
}

强转:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println((int) (new Scanner(System.in).nextFloat() + .5));
}
}

HJ8 合并表记录
import java.util.Scanner;
import java.util.TreeMap;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// 插入后, 变成有序的;
TreeMap<Integer, Integer> treeMap = new TreeMap<>();
for (int i = 0; i < n; i++)
treeMap.merge(sc.nextInt(), sc.nextInt(), Integer::sum);
treeMap.forEach((k, v) -> System.out.println(k + " " + v));
}
}
如何提升速度, 并且降低内存使用?求告知

HJ9 提取不重复的整数
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
LinkedHashSet<Character> set = new LinkedHashSet<>();
for (int i = str.length() - 1; i >= 0; i--) {
char c = str.charAt(i);
set.add(str.charAt(i));
}
set.forEach(System.out::print);
}
}

不用LinkedHashSet
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] chars = new char[str.length()];
for (int i = str.length() - 1; i >= 0; i--) {
chars[str.length() - i - 1] = str.charAt(i);
for (int j = str.length() - i - 2; j >= 0; j--) {
if (str.charAt(i) == chars[j] && chars[j] != ' ') {
chars[str.length() - i - 1] = ' ';
break;
}
}
}
for (int i = 0; i < chars.length; i++) {
if (chars[i] != ' ') {
System.out.print(chars[i]);
}
}
}
}

HJ10 字符个数统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] chars = new char[str.length()];
for (int i = str.length() - 1; i >= 0; i--) {
chars[str.length() - i - 1] = str.charAt(i);
for (int j = str.length() - i - 2; j >= 0; j--) {
if (str.charAt(i) == chars[j] && chars[j] != ' ') {
chars[str.length() - i - 1] = ' ';
break;
}
}
}
int result = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] != ' ') {
result++;
}
}
System.out.println(result);
}
}

import java.util.Scanner;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
HashSet<Character> set = new HashSet<>();
for (int i = 0; i < str.length(); i++) {
set.add(str.charAt(i));
}
System.out.println(set.size());
}
}

HJ11 数字颠倒
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
for (int i = str.length()-1; i >= 0; i--){
System.out.print(str.charAt(i));
}
}
}

HJ12 字符串反转
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
for (int i = str.length()-1; i >= 0; i--){
System.out.print(str.charAt(i));
}
}
}

HJ13 句子逆序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().trim();
char c;
for (int i = str.length() - 1, j = 0; i >= 0; i--) {
c = str.charAt(i);
if (c >= 65 && c <= 90 || (c >= 97 && c <= 122)) {
if (j == 0) {
j = i + 1;
}
if (i == 0) {
System.out.print(str.substring(i, j));
}
} else if (c == ' ') {
if (j == 0) {
System.out.print(" ");
continue;
}
System.out.print(str.substring(i + 1, j) + " ");
j = 0;
}
}
}
}

本文提供了针对牛客网华为机试的多个编程题目解答,包括字符串操作、数值处理及数据结构应用等,使用Java语言实现。
7万+

被折叠的 条评论
为什么被折叠?



