https://blog.csdn.net/weixin_43428283/article/details/127361330
https://blog.csdn.net/fengyuyeguirenenen/article/details/125245803
https://blog.csdn.net/pengzongjy/category_12114900.html
https://blog.csdn.net/misayaaaaa/article/details/126965954

入门

HJ7 取近似值

  1. // https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
  2. // 描述
  3. // 写出一个程序,接受一个正浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于 0.5 ,向上取整;小于 0.5 ,则向下取整。
  4. // 数据范围:保证输入的数字在 32 位浮点数范围内
  5. // 输入描述:
  6. // 输入一个正浮点数值
  7. // 输出描述:
  8. // 输出该数值的近似整数值
  9. // 输入:5.5
  10. // 输出:6
  11. // 说明:
  12. // 0.5>=0.5,所以5.5需要向上取整为6
  13. // 输入:2.499
  14. // 输出:2
  15. // 说明:0.499<0.5,2.499向下取整为2
  16. import java.util.Scanner;
  17. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  18. public class Main {
  19. public static void main(String[] args) {
  20. Scanner in = new Scanner(System.in);
  21. // 注意 hasNext 和 hasNextLine 的区别
  22. while (in.hasNext()) {
  23. int result = 0;
  24. double a = in.nextDouble();
  25. // 获取小数后第一位数字,判断是否大于等于5
  26. int b = (int)(a * 10) % 10;
  27. if(b >= 5){
  28. result = (int)a + 1;
  29. }else{
  30. result = (int)a;
  31. }
  32. System.out.println(result);
  33. }
  34. }
  35. }

HJ9 提取不重复的整数

  1. // https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1
  2. // 描述
  3. // 输入一个 int 型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
  4. // 保证输入的整数最后一位不是 0 。
  5. // 数据范围: 1 ≤ n ≤ 10^8
  6. // 输入描述:
  7. // 输入一个int型整数
  8. // 输出描述:
  9. // 按照从右向左的阅读顺序,返回一个不含重复数字的新的整数
  10. // 输入:9876673
  11. // 输出:37689
  12. import java.util.*;
  13. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  14. public class Main {
  15. public static void main(String[] args) {
  16. Scanner in = new Scanner(System.in);
  17. // 注意 hasNext 和 hasNextLine 的区别
  18. while (in.hasNextInt()) { // 注意 while 处理多个 case
  19. int a = in.nextInt();
  20. // HashSet元素乱序
  21. // LinkedHashSet保证元素添加顺序(!!!)
  22. // TreeSet元素按自然顺序排序
  23. Set<Character> set = new LinkedHashSet<>();
  24. String str = String.valueOf(a);
  25. for(int i = str.length() - 1; i >= 0; i--){
  26. set.add(str.charAt(i));
  27. }
  28. str = "";
  29. for(Character ch : set){
  30. str = str + ch;
  31. }
  32. System.out.println(Integer.parseInt(str));
  33. }
  34. }
  35. }

HJ46 截取字符串

  1. // https://www.nowcoder.com/practice/a30bbc1a0aca4c27b86dd88868de4a4a
  2. // 描述
  3. // 输入一个字符串和一个整数 k ,截取字符串的前k个字符并输出
  4. // 数据范围:字符串长度满足 1 \le n \le 1000 \1≤n≤1000 , 1 \le k \le n \1≤k≤n
  5. // 输入描述:
  6. // 1.输入待截取的字符串
  7. // 2.输入一个正整数k,代表截取的长度
  8. // 输出描述:
  9. // 截取后的字符串
  10. // 输入:abABCcDEF
  11. // 6
  12. // 输出:abABCc
  13. import java.util.Scanner;
  14. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  15. public class Main {
  16. public static void main(String[] args) {
  17. Scanner in = new Scanner(System.in);
  18. // 注意 hasNext 和 hasNextLine 的区别
  19. while (in.hasNext()) { // 注意 while 处理多个 case
  20. String str = in.next();
  21. int num = in.nextInt();
  22. StringBuilder result = new StringBuilder();
  23. for(int i = 0; i < num; i++){
  24. result = result.append(str.charAt(i));
  25. }
  26. System.out.println(result);
  27. }
  28. }
  29. }

55555 - HJ58 输入n个整数,输出其中最小的k个

  1. // https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
  2. // 描述
  3. // 输入n个整数,找出其中最小的k个整数并按升序输出
  4. // 本题有多组输入样例
  5. // 数据范围:1 ≤ n ≤ 1000,输入的整数满足1 ≤ val ≤ 10000
  6. // 输入描述:
  7. // 第一行输入两个整数n和k
  8. // 第二行输入一个整数数组
  9. // 输出描述:
  10. // 从小到大输出最小的k个整数,用空格分开
  11. // 输入:5 2
  12. // 1 3 5 7 2
  13. // 输出:1 2
  14. import java.util.*;
  15. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  16. public class Main {
  17. public static void main(String[] args) {
  18. Scanner in = new Scanner(System.in);
  19. // 注意 hasNext 和 hasNextLine 的区别
  20. while (in.hasNextLine()) {
  21. // 必须使用nextLine,而不是next
  22. String a = in.nextLine,而不是next();
  23. String b = in.nextLine();
  24. String[] as = a.split(" ");
  25. int nums = Integer.parseInt(as[0]);
  26. int k = Integer.parseInt(as[1]);
  27. // 字符串数组 转 整数数组
  28. String[] str = b.split(" ");
  29. int[] ints = new int[str.length];
  30. for(int i = 0; i < str.length; i++){
  31. ints[i] = Integer.parseInt(str[i]);
  32. }
  33. Arrays.sort(ints);
  34. for(int i = 0; i < k && i < nums; i++){
  35. System.out.print(ints[i] + " ");
  36. }
  37. }
  38. }
  39. }

HJ101 输入整型数组和排序标识,对其元素按照升序或降序进行排序

  1. // https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
  2. // 描述
  3. // 输入整型数组和排序标识,对其元素按照升序或降序进行排序
  4. // 数据范围:1 ≤ n ≤ 1000,元素大小满足0 ≤ val ≤ 100000
  5. // 输入描述:
  6. // 第一行输入数组元素个数
  7. // 第二行输入待排序的数组,每个数用空格隔开
  8. // 第三行输入一个整数0或1。0代表升序排序,1代表降序排序
  9. // 输出描述:
  10. // 输出排好序的数字
  11. // 输入:8
  12. // 1 2 4 9 3 55 64 25
  13. // 0
  14. // 输出:1 2 3 4 9 25 55 64
  15. // 输入:5
  16. // 1 2 3 4 5
  17. // 1
  18. // 输出:
  19. // 5 4 3 2 1
  20. import java.util.*;
  21. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  22. public class Main {
  23. public static void main(String[] args) {
  24. Scanner in = new Scanner(System.in);
  25. // 注意 hasNext 和 hasNextLine 的区别
  26. while (in.hasNextLine()) {
  27. // 必须都使用in.nextLine()接收参数才行,不能一个使用in.nextLine(),其他使用in.next()
  28. int nums = Integer.parseInt(in.nextLine());
  29. String str = in.nextLine();
  30. int flag = Integer.parseInt(in.nextLine());
  31. // 字符串数组 转 整数数组
  32. String[] strs = str.split(" ");
  33. // 此处必须使用包装类,否则无法使用Arrays.sort进行降序排列
  34. Integer[] ints = new Integer[nums];
  35. for (int i = 0; i < nums; i++) {
  36. ints[i] = Integer.parseInt(strs[i]);
  37. }
  38. if (flag == 0) {
  39. Arrays.sort(ints);
  40. } else {
  41. Arrays.sort(ints, (o1, o2) -> o2 - o1);
  42. }
  43. for (int i = 0; i < nums; i++) {
  44. System.out.print(ints[i] + " ");
  45. }
  46. }
  47. }
  48. }

简单

HJ1 字符串最后一个单词的长度

  1. // https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
  2. // 描述
  3. // 计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
  4. // 输入描述:
  5. // 输入一行,代表要计算的字符串,非空,长度小于5000。
  6. // 输出描述:
  7. // 输出一个整数,表示输入字符串最后一个单词的长度。
  8. // 示例1
  9. // 输入:hello nowcoder
  10. // 输出:8
  11. // 说明:最后一个单词为nowcoder,长度为8
  12. import java.util.Scanner;
  13. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  14. public class Main {
  15. public static void main(String[] args) {
  16. Scanner sc = new Scanner(System.in);
  17. String str = sc.nextLine();
  18. String[] st = str.split(" ");
  19. int len1 = st[st.length - 1].length();
  20. System.out.println(len1);
  21. }
  22. }

HJ2 计算某字符出现次数

  1. // https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
  2. // 描述
  3. // 写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
  4. // 数据范围: 1 \le n \le 1000 \1≤n≤1000
  5. // 输入描述:
  6. // 第一行输入一个由字母、数字和空格组成的字符串,第二行输入一个字符(保证该字符不为空格)。
  7. // 输出描述:
  8. // 输出输入字符串中含有该字符的个数。(不区分大小写字母)
  9. // 输入:ABCabc
  10. // A
  11. // 输出:2
  12. import java.util.Scanner;
  13. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  14. public class Main {
  15. public static void main(String[] args) {
  16. Scanner sc = new Scanner(System.in);
  17. String str = sc.nextLine();
  18. String s = sc.nextLine();
  19. int count = 0;
  20. for(int i = 0; i < str.length(); i++){
  21. if(s.equalsIgnoreCase(str.charAt(i) + "")){
  22. count++;
  23. }
  24. }
  25. System.out.println(count);
  26. }
  27. }

HJ4 字符串分隔

  1. // https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
  2. // 描述
  3. // • 输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
  4. // • 长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
  5. // 输入描述:
  6. // 连续输入字符串(每个字符串长度小于等于100)
  7. // 输出描述:
  8. // 依次输出所有分割后的长度为8的新字符串
  9. // 输入:abc
  10. // 输出:abc0000
  11. import java.util.Scanner;
  12. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  13. public class Main {
  14. public static void main(String[] args) {
  15. Scanner sc = new Scanner(System.in);
  16. String str = sc.nextLine();
  17. int len = str.length();
  18. if (len % 8 == 0) {
  19. for (int i = 0; i < len; i = i + 8) {
  20. System.out.println(str.substring(i, i + 8));
  21. }
  22. } else {
  23. int temp = len % 8;
  24. for (int i = 0; i < 8 - temp; i++) {
  25. str = str + "0";
  26. }
  27. len = str.length();
  28. for (int i = 0; i < len; i = i + 8) {
  29. System.out.println(str.substring(i, i + 8));
  30. }
  31. }
  32. }
  33. }

55555 - HJ5 进制转换

  1. // https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. String str = in.next();
  10. // 八进制数以数字0开头、十六进制以0x开头
  11. if(str.charAt(0) == '0'){
  12. int number = Integer.parseInt(str.substring(2), 16);
  13. System.out.println(number);
  14. }else {
  15. int number = Integer.parseInt(str, 16);
  16. System.out.println(number);
  17. }
  18. }
  19. }
  20. }

55555 - HJ6 质数因子

  1. // https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
  2. // 描述:功能:输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )
  3. // 输入描述:输入一个整数
  4. // 输出描述:按照从小到大的顺序输出它的所有质数的因子,以空格隔开。
  5. // 示例1
  6. // 输入:180
  7. // 输出:2 2 3 3 5
  8. import java.util.Scanner;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNextInt()) {
  15. long num = in.nextLong();
  16. // 一个正整数只有一个质因子是大于其平方根,这个质因子就是它本身
  17. long k = (long) Math.sqrt(num);
  18. for (long i = 2; i <= k; i++) {
  19. while (num % i == 0) {
  20. System.out.print(i + " ");
  21. num = num / i;
  22. }
  23. }
  24. System.out.println(num == 1 ? "": num + "");
  25. }
  26. }
  27. }

55555 - HJ8 合并表记录

  1. // https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()){
  9. int nums = in.nextInt();
  10. // HashMap执行结果未按照index升序
  11. // Map<Integer, Integer> map = new HashMap<>();
  12. // TreeMap默认按照key进行升序排序,也可以自定义排序规则
  13. Map<Integer, Integer> map = new TreeMap<>();
  14. for (int i = 0; i < nums; i++) {
  15. int key = in.nextInt();
  16. int value = in.nextInt();
  17. // map.put(key, map.getOrDefault(key, 0) + value);
  18. if (map.containsKey(key)){
  19. map.put(key, map.get(key) + value);
  20. }else {
  21. map.put(key, value);
  22. }
  23. }
  24. // 遍历输出方式一:
  25. // for(Integer key : map.keySet()){
  26. // System.out.println(key + " " + map.get(key));
  27. // }
  28. // 遍历输出方式二:
  29. for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  30. System.out.println(entry.getKey() + " " + entry.getValue());
  31. }
  32. }
  33. }
  34. public void test() {
  35. Scanner in = new Scanner(System.in);
  36. // 注意 hasNext 和 hasNextLine 的区别
  37. while (in.hasNextLine()) {
  38. int nums = Integer.parseInt(in.nextLine());
  39. // 使用数组会报错内存溢出
  40. // int[] result = new int[11111112];
  41. Map<Integer, Integer> map = new TreeMap<>();
  42. for(int i = 0; i < nums; i++){
  43. String b = in.nextLine();
  44. String[] strs = b.split(" ");
  45. int key = Integer.parseInt(strs[0]);
  46. int value = Integer.parseInt(strs[1]);
  47. // result[index] = result[index] + value;
  48. if (map.containsKey(key)){
  49. map.put(key, map.get(key) + value);
  50. }else {
  51. map.put(key, value);
  52. }
  53. }
  54. for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  55. System.out.println(entry.getKey() + " " + entry.getValue());
  56. }
  57. }
  58. }
  59. }

HJ10 字符个数统计

  1. // https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) { // 注意 while 处理多个 case
  9. String str = in.next();
  10. Set<Character> set = new HashSet<>();
  11. for(int i = 0; i < str.length(); i++){
  12. char ch = str.charAt(i);
  13. set.add(ch);
  14. }
  15. System.out.println(set.size());
  16. }
  17. }
  18. }

HJ11 数字颠倒

  1. // https://www.nowcoder.com/practice/ae809795fca34687a48b172186e3dafe
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) { // 注意 while 处理多个 case
  9. int num = in.nextInt();
  10. String str = String.valueOf(num);
  11. for(int i = str.length() - 1; i >= 0; i--){
  12. System.out.print(str.charAt(i));
  13. }
  14. }
  15. }
  16. }

HJ12 字符串反转

  1. // https://www.nowcoder.com/practice/e45e078701ab4e4cb49393ae30f1bb04
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) { // 注意 while 处理多个 case
  9. String str = in.next();
  10. for(int i = str.length() - 1; i >= 0; i--){
  11. System.out.print(str.charAt(i));
  12. }
  13. }
  14. }
  15. }

HJ13 句子逆序

  1. // https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 必须使用hasNextLine,而不能使用hasNext,使用hasNext,只会读取第一个字符串
  8. while (in.hasNextLine()) {
  9. String str = in.nextLine();
  10. String[] strs = str.split(" ");
  11. for(int i = strs.length - 1; i >= 0; i--){
  12. System.out.print(strs[i] + " ");
  13. }
  14. }
  15. }
  16. }

55555 - HJ14 字符串排序

  1. // https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. List<String> list = new ArrayList<>();
  10. int num = in.nextInt();
  11. for(int i = 0; i < num; i++){
  12. String str = in.next();
  13. list.add(str);
  14. }
  15. // 按照字典序排列
  16. Collections.sort(list);
  17. // 按照字典序逆序排列
  18. Collections.sort(list, (o1, o2) -> o2.compareTo(o1));
  19. for(String str : list){
  20. System.out.println(str);
  21. }
  22. }
  23. }
  24. }

55555 - HJ15 求int型正整数在内存中存储时1的个数

进行转换 - 1

https://blog.csdn.net/besto229/article/details/75796033
https://blog.csdn.net/u012027907/article/details/77683813

  1. // https://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) {
  9. int number = in.nextInt();
  10. // 将十进制数转换为二进制数
  11. String binary = Integer.toBinaryString(number);
  12. // 将十进制数转换为八进制数
  13. String octal = Integer.toOctalString(number);
  14. // 将十进制数转换为十六进制数
  15. String hex = Integer.toHexString(number);
  16. // 将其他进制数转换为十进制数,2表示需转换的字符串为二进制
  17. int num = Integer.parseInt("100", 2); // 4
  18. int count = 0;
  19. for(int i = 0; i < binary.length(); i++){
  20. if(binary.charAt(i) == '1') count++;
  21. }
  22. System.out.println(count);
  23. }
  24. }
  25. // 方法二:
  26. public void test() {
  27. Scanner in = new Scanner(System.in);
  28. // 注意 hasNext 和 hasNextLine 的区别
  29. while (in.hasNextInt()) {
  30. int number = in.nextInt();
  31. //【1】需要一个长度为32的int数组来存储结果二进制
  32. int[] bit = new int[32];
  33. //【2】循环,把原始数除以2取得余数,这个余数就是二进制数,原始的数等于商。
  34. // 商如果不能再除以二,结束循环。
  35. int i = 0;
  36. while(number > 1){
  37. // 获取除以2的余数
  38. int b = number % 2;
  39. // 数字除以2的商 赋值给当前数字
  40. number = number / 2;
  41. bit[i++] = b;
  42. if(number < 2){
  43. // 已经不能再把数除以2,就把该数直接放到数组的下一位
  44. bit[i+1] = number;
  45. }
  46. }
  47. //【3】翻转数组,得到的才是最终二进制数组(此处求1的个数可以进行翻转,直接求值)
  48. for(int j = 0; j < bit.length / 2; j++){
  49. // 第一个数的值设置为最后一个数的值
  50. // 第二次的时候,i是1,把第二个数的值,赋值为倒数第二个
  51. int temp = bit[j];
  52. bit[j] = bit[bit.length - 1 - j];
  53. bit[bit.length - 1 - j] = temp;
  54. }
  55. int count = 0;
  56. for(int k = 0; k < bit.length; k++){
  57. if(bit[k] == 1) count++;
  58. }
  59. System.out.println(count);
  60. }
  61. }
  62. }

55555 - HJ21 简单密码

  1. // https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. // 定义map容器存储按键对应数字字符的容器
  6. private static Map<String, String> map = new HashMap<>();
  7. // 静态初始化、加载map容器
  8. static{
  9. map.put("1", "1");
  10. map.put("abc", "2");
  11. map.put("def", "3");
  12. map.put("ghi", "4");
  13. map.put("jkl", "5");
  14. map.put("mno", "6");
  15. map.put("pqrs", "7");
  16. map.put("tuv", "8");
  17. map.put("wxyz", "9");
  18. map.put("0", "0");
  19. }
  20. public static void main(String[] args) {
  21. Scanner scanner = new Scanner(System.in);
  22. while(scanner.hasNext()){
  23. String str = scanner.nextLine();
  24. char[] chars = str.toCharArray();
  25. // 构造buffer容器存储转换后的字符串
  26. StringBuffer buffer = new StringBuffer();
  27. for(char ch : chars){
  28. //【1】小写字母转换为数字
  29. if(ch >= 'a' && ch <= 'z'){
  30. // map容器中的key与字符进行校验并加密
  31. Set<String> keys = map.keySet();
  32. for(String key : keys){
  33. if(key.contains(String.valueOf(ch))){
  34. buffer.append(map.get(key));
  35. }
  36. }
  37. //【2】如果是A - Y的大写字母则需要将其+32位转换成小写再向后移1位
  38. // 大写字母的ASCII范围:65-90
  39. // 小写字母的ASCII范围:97-122
  40. // 数字的ASCII范围:48-57
  41. }else if(ch >= 'A' && ch <= 'Y'){
  42. char newChar = (char)(ch + 32 + 1);
  43. buffer.append(newChar);
  44. //【2.1】如果是Z则加密成a
  45. }else if(ch == 'Z'){
  46. buffer.append("a");
  47. //【3】数字和其它的符号都不做变换
  48. }else{
  49. buffer.append(ch);
  50. }
  51. }
  52. System.out.print(buffer.toString());
  53. }
  54. }
  55. public void test() {
  56. Scanner in = new Scanner(System.in);
  57. // 注意 hasNext 和 hasNextLine 的区别
  58. while (in.hasNext()) {
  59. char[] chars = in.next().toCharArray();
  60. // 明文密码
  61. char[] char1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
  62. // 明文密码 加密后的数组,根据索引与明文密码char1一一对应
  63. char[] char2 = "bcdefghijklmnopqrstuvwxyza222333444555666777788899990123456789".toCharArray();
  64. for(int i = 0; i < chars.length; i++){
  65. // 大写字母转小写
  66. if(chars[i] >= 'A' && chars[i] <= 'Z'){
  67. chars[i] = char2[chars[i] - 'A'];
  68. // 小写字母转数字。前面是26个小写字母,所以要从索引26开始
  69. }else if(chars[i] >= 'a' && chars[i] <= 'z'){
  70. chars[i] = char2[chars[i] - 'a' + 26];
  71. }
  72. }
  73. StringBuilder result = new StringBuilder();
  74. for(Character ch : chars){
  75. result.append(ch);
  76. }
  77. System.out.println(result);
  78. }
  79. }
  80. }

HJ22 汽水瓶

  1. // https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) { // 注意 while 处理多个 case
  9. int num = in.nextInt();
  10. if(num == 0){
  11. break;
  12. }
  13. int result = 0;
  14. while(num > 1){
  15. if(num == 2){
  16. num++;
  17. }
  18. // 每轮循环获取的汽水瓶数:num/3
  19. result = result + num / 3;
  20. // 每轮循环后剩余的空瓶子数:新兑换的汽水形成的空瓶子 + 当前循环剩余的空瓶子
  21. num = num / 3 + num % 3;
  22. // if(num % 3 == 0){
  23. // num = num / 3;
  24. // result = result + num;
  25. // }else if(num % 3 == 1){
  26. // result = result + num / 3;
  27. // num = num / 3 + 1;
  28. // }else if(num % 3 == 2){
  29. // result = result + num / 3;
  30. // num = num / 3 + 2;
  31. // }
  32. }
  33. System.out.println(result);
  34. }
  35. }
  36. }

HJ23 删除字符串中出现次数最少的字符

  1. // https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. String str = in.next();
  10. //【1】统计每个字母的数量
  11. Map<Character, Integer> map = new HashMap<>();
  12. for(int i = 0; i < str.length(); i++){
  13. int value = map.getOrDefault(str.charAt(i), 0);
  14. map.put(str.charAt(i), value + 1);
  15. }
  16. // //【2】找到数量最少的字符数量
  17. // Collection<Integer> values = map.values();
  18. // int min = Collections.min(values);
  19. // //【3】用空字符串替换该字母
  20. // for(Character ch : map.keySet()) {
  21. // if (map.get(ch) == min){
  22. // str = str.replaceAll(String.valueOf(ch), "");
  23. // }
  24. // }
  25. // System.out.println(str);
  26. //【2】找到数量最少的字符数量
  27. int minConut = Integer.MAX_VALUE;
  28. for (int times : map.values()) {
  29. minConut = Math.min(minConut, times);
  30. }
  31. //【3】剔除最少数量的字符
  32. StringBuilder res = new StringBuilder();
  33. for(char ch : str.toCharArray()) {
  34. if (map.get(ch) != minConut) {
  35. res.append(ch);
  36. }
  37. }
  38. System.out.println(res.toString());
  39. // 需要有序的map对结果进行收集
  40. // Map<Character, Integer> map1 = new LinkedHashMap<>();
  41. // map.entrySet().stream().sorted((o1, o2) -> o1.getValue() - o2.getValue())
  42. // .forEach(e -> map1.put(e.getKey(), e.getValue()));
  43. }
  44. }
  45. public void test() {
  46. Scanner in = new Scanner(System.in);
  47. // 注意 hasNext 和 hasNextLine 的区别
  48. while (in.hasNext()) {
  49. String str = in.next();
  50. //【1】统计输入字符串中每个字符的个数(小写字母有32位)
  51. int[] chars = new int[32];
  52. for(int i = 0; i < str.length(); i++){
  53. char ch = str.charAt(i);
  54. chars[ch - 'a']++;
  55. }
  56. //【2】求出字符串中字符出现最少的数量
  57. int min = Integer.MAX_VALUE;
  58. for(int num : chars){
  59. if(num > 0){
  60. min = Math.min(min, num);
  61. }
  62. }
  63. //【3】外层遍历输入的字符串,内层遍历字符对应的数量
  64. StringBuffer result = new StringBuffer();
  65. for(int i = 0; i < str.length(); i++){
  66. for(int j = 0; j < chars.length; j++){
  67. // 当遍历字符 与 数组下标对应字符相等,并且字符数量大于最小值时,加入结果集
  68. if(str.charAt(i) == (char)(j + 97) && chars[j] > min){
  69. result.append(str.charAt(i));
  70. break;
  71. }
  72. }
  73. }
  74. System.out.println(result.toString());
  75. }
  76. }
  77. }

HJ31 单词倒排

  1. // https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. String str = in.nextLine();
  10. for(int i = 0; i < str.length(); i++){
  11. char ch = str.charAt(i);
  12. if(ch >= 'a' && ch <= 'z'){
  13. }else if(ch >= 'A' && ch <= 'Z'){
  14. }else{
  15. str = str.replace(ch, ' ');
  16. }
  17. }
  18. String[] strs = str.split(" ");
  19. for(int i = strs.length - 1; i >= 0; i--){
  20. System.out.print(strs[i].trim() + " ");
  21. }
  22. }
  23. }
  24. }

HJ34 图片整理

  1. // https://www.nowcoder.com/practice/2de4127fda5e46858aa85d254af43941
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. String str = in.next();
  10. List<Character> list = new ArrayList<>();
  11. for(int i = 0; i < str.length(); i++){
  12. list.add(str.charAt(i));
  13. }
  14. Collections.sort(list);
  15. for(Character ch : list){
  16. System.out.print(ch);
  17. }
  18. }
  19. }
  20. }

HJ35 蛇形矩阵 - 1111122222

对角线赋值

  1. // https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
  2. // 蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。
  3. // 例如,当输入5时,应该输出的三角形为:
  4. // 1 3 6 10 15
  5. // 2 5 9 14
  6. // 4 8 13
  7. // 7 12
  8. // 11
  9. // 输入描述:输入正整数N(N不大于100)
  10. // 输出描述:输出一个N行的蛇形矩阵
  11. // 输入:4
  12. // 输出:
  13. // 1 3 6 10
  14. // 2 5 9
  15. // 4 8
  16. // 7
  17. import java.util.Scanner;
  18. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  19. public class Main {
  20. public static void main(String[] args) {
  21. Scanner in = new Scanner(System.in);
  22. // 注意 hasNext 和 hasNextLine 的区别
  23. while (in.hasNextInt()) {
  24. int num = in.nextInt();
  25. int[][] result = new int[num][];
  26. // 初始化第一个值
  27. int value = 1;
  28. for(int i = 0; i < num; i++){
  29. // 第i行有n-i个元素
  30. result[i] = new int[num - i];
  31. for(int j = 0; j <= i; j++){
  32. // 对第i个对角线赋值(从左下到右上赋值:行数依次向上减少、列数依次向右增加)
  33. result[i-j][j] = value;
  34. value++;
  35. }
  36. }
  37. for(int[] rows : result){
  38. for(int col : rows){
  39. System.out.print(col + " ");
  40. }
  41. System.out.println();
  42. }
  43. }
  44. }
  45. }

55555 - HJ37 统计每个月兔子的总数

  1. // https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
  2. // 有一种兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第三个月后每个月又生一只兔子。
  3. // 例子:假设一只兔子第3个月出生,那么它第5个月开始会每个月生一只兔子。
  4. // 一月的时候有一只兔子,假如兔子都不死,问第n个月的兔子总数为多少?
  5. // 输入:3
  6. // 输出:2
  7. // 输入:5
  8. // 输出:5
  9. import java.util.Scanner;
  10. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  11. public class Main {
  12. public static void main(String[] args) {
  13. Scanner in = new Scanner(System.in);
  14. // 注意 hasNext 和 hasNextLine 的区别
  15. while (in.hasNextInt()) {
  16. int num = in.nextInt();
  17. int k1 = 1; // 第一个月,刚出生的兔子数量
  18. int k2 = 0; // 第二个月,不可生育的兔子数量
  19. int k3 = 0; // 第三个月,可以生育的兔子数量
  20. // 从第二个月开始,到第num个月结束
  21. for(int i = 2; i <= num; i++){
  22. // 每增加一个月,可以生育的兔子数量 = 先前可生育数量 + 先前不可生育数量
  23. k3 = k3 + k2;
  24. // 每增加一个月,不可生育的兔子数量 = 先前刚出生数量
  25. k2 = k1;
  26. // 每增加一个月,刚出生兔子数量 = 先前可以生育兔子数量
  27. k1 = k3;
  28. }
  29. System.out.println(k1 + k2 + k3);
  30. }
  31. }
  32. }

55555 - HJ40 统计字符

  1. // https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. String str = in.nextLine();
  10. // HashMap是无序的,LinkedHashMap是有序的,且默认为插入顺序
  11. Map<String, Integer> map = new LinkedHashMap<>();
  12. map.put("yinwen", 0);
  13. map.put("kongge", 0);
  14. map.put("shuzi", 0);
  15. map.put("qita", 0);
  16. for(int i = 0; i < str.length(); i++){
  17. char ch = str.charAt(i);
  18. if(ch >= 'a' && ch <= 'z'){
  19. map.put("yinwen", map.getOrDefault("yinwen", 0) + 1);
  20. }else if(ch >= 'A' && ch <= 'Z'){
  21. map.put("yinwen", map.getOrDefault("yinwen", 0) + 1);
  22. }else if(ch >= '0' && ch <= '9'){
  23. map.put("shuzi", map.getOrDefault("shuzi", 0) + 1);
  24. }else if(ch == ' '){
  25. map.put("kongge", map.getOrDefault("kongge", 0) + 1);
  26. }else{
  27. map.put("qita", map.getOrDefault("qita", 0) + 1);
  28. }
  29. }
  30. for (Map.Entry<String, Integer> entry : map.entrySet()) {
  31. System.out.println(entry.getValue());
  32. }
  33. }
  34. }
  35. }

HJ51 输出单向链表中倒数第k个结点

  1. // https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. String num = in.nextLine();
  10. String[] strs = in.nextLine().split(" ");
  11. int target = Integer.parseInt(in.nextLine());
  12. //【1】字符串数组 转换为 整数数组
  13. int[] nums = new int[strs.length];
  14. for(int i = 0; i < strs.length; i++){
  15. nums[i] = Integer.parseInt(strs[i]);
  16. }
  17. //【2】构建单向链表,先构建尾结点,再从尾到头依次构建节点
  18. ListNode lastNode = new ListNode(nums[nums.length-1], null);
  19. ListNode head = lastNode;
  20. for(int i = nums.length - 2; i >= 0; i--){
  21. head = new ListNode(nums[i], lastNode);
  22. lastNode = head;
  23. }
  24. ListNode slow = head;
  25. ListNode fast = head;
  26. //【3】从头到尾找到第k个节点
  27. for(int i = 0; i < target; i++){
  28. fast = fast.next;
  29. }
  30. //【4】同时移动快慢指针,当快指针到达尾部时,慢指针刚好指向倒数第k个节点(快慢指针之间距离始终保持k个节点)
  31. while (fast != null){
  32. fast = fast.next;
  33. slow = slow.next;
  34. }
  35. System.out.println(slow.value);
  36. }
  37. }
  38. }
  39. class ListNode{
  40. int value;
  41. ListNode next;
  42. public ListNode(){
  43. }
  44. public ListNode(int value, ListNode next){
  45. this.value = value;
  46. this.next = next;
  47. }
  48. }

HJ53 杨辉三角的变形 - 1111122222

  1. // https://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43
  2. // 以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数、左上角数和右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。
  3. // 求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3,输入2则输出-1
  4. // 输入描述:输入一个int整数
  5. // 输出描述:输出返回的int值
  6. // 输入:4
  7. // 输出:3
  8. import java.util.Scanner;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNextInt()) {
  15. int num = in.nextInt();
  16. // 规律:-1 -1 2 3 2 4 2 3 2 4 2 3 2 4 2 3 2 4 2 3 2 4......
  17. if(num == 1 || num == 2){
  18. System.out.println(-1);
  19. }else if(num % 4 == 0){
  20. System.out.println(3);
  21. }else if((num-2) % 4 == 0){
  22. System.out.println(4);
  23. }else if(num % 4 == 1 || num % 4 == 3){
  24. System.out.println(2);
  25. }
  26. }
  27. }
  28. }

牛客算法 - 图1

55555 - HJ54 表达式求值 - HJ50 四则运算

  1. // https://www.nowcoder.com/practice/9566499a2e1546c0a257e885dfdbf30d
  2. // 输入:400+5
  3. // 输出:405
  4. https://blog.nowcoder.net/n/7d2b83a154964702889c14af4750a0e9?f=comment
  5. https://blog.nowcoder.net/n/098ac570a79c42afa423edc79bb7c22b?f=comment
  6. https://blog.nowcoder.net/n/3031988aa4d546ebb511b4b1743b7d46?f=comment
  7. import java.util.*;
  8. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  9. public class Main {
  10. public static void main(String[] args) {
  11. Scanner in = new Scanner(System.in);
  12. // 注意hasNext 和 hasNextLine的区别
  13. while (in.hasNext()) {
  14. String str = in.next();
  15. str = str.replace("[", "(");
  16. str = str.replace("{", "(");
  17. str = str.replace("}", ")");
  18. str = str.replace("]", ")");
  19. System.out.println(slove(str));
  20. }
  21. }
  22. public static int slove(String str) {
  23. Stack<Integer> stack = new Stack<>();
  24. char[] chars = str.toCharArray();
  25. // 初始化符号为'+'
  26. char sign = '+';
  27. // 记录数字(主要是记录大于9的数字)
  28. int number = 0;
  29. for (int index = 0; index < str.length(); index++) {
  30. char ch = chars[index];
  31. //【1】当前字符是数字,拼接数字
  32. if (Character.isDigit(ch)) {
  33. number = number * 10 + (ch - '0');
  34. }
  35. //【2】当前字符是左括号
  36. if (ch == '(') {
  37. // 下标移动到括号后一位字符
  38. int right = index + 1;
  39. //【2.1】统计左括号的数量
  40. int count = 1;
  41. while (count > 0) {
  42. if (chars[right] == '(') count++; // 遇到左括号,左括号数+1
  43. if (chars[right] == ')') count--; // 遇到右括号,抵消一个左括号,形成闭环,左括号数-1
  44. right++;
  45. }
  46. //【2.2】递归求解当前已经闭环括号中的表达式,每次递归都是将元素放入到的一个新栈中,返回值就是ans
  47. // index+1:当前闭环括号的起始位置,index是第一个左括号位置
  48. // right-1:当前闭环括号中最后一个右括号的位置,right是右括号的下一个字符位置
  49. number = slove(str.substring(index+1, right-1));
  50. // 因为for循环要index++,所以此处要先回退一步到最后一个右括号的位置
  51. index = right - 1;
  52. }
  53. //【3】当前字符是运算符,或者通过步骤2、当前的index可能是字符串末尾上的右括号,将已知的数字处理后放进栈
  54. if (!Character.isDigit(ch) || index == str.length() - 1) {
  55. //【3.1】左边的符号若是'+', 直接放入栈
  56. if (sign == '+') {
  57. stack.push(number);
  58. //【3.2】左边的符号若是'-', 数字取相反后,再放入栈
  59. }else if (sign == '-') {
  60. stack.push(-1 * number);
  61. //【3.3】左边的符号若是'*', 将该数字与栈中弹出一个数字相乘后,再放入栈
  62. }else if (sign == '*') {
  63. stack.push(stack.pop() * number);
  64. //【3.4】左边的符号若是'/', 栈中弹出一个数字与该数字相除后,再放入栈
  65. }else if (sign == '/') {
  66. stack.push(stack.pop() / number);
  67. }
  68. // 更新运算符
  69. sign = ch;
  70. // 当前数字已运算完毕,必须重置为0
  71. number = 0;
  72. }
  73. }
  74. // 栈中数字求和得到结果
  75. int ans = 0;
  76. while (!stack.isEmpty()) {
  77. ans += stack.pop();
  78. }
  79. return ans;
  80. }
  81. }

HJ56 完全数计算 - 0

  1. // https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) {
  9. int num = in.nextInt();
  10. int count = 0;
  11. for(int index = 1; index <= num; index++){
  12. int sum = 0;
  13. for(int i = 1; i <= index / 2; i++){
  14. // 判断i能否被index整除
  15. if(index % i == 0){
  16. sum = sum + i;
  17. }
  18. }
  19. if(sum == index){
  20. count++;
  21. }
  22. }
  23. System.out.println(count);
  24. }
  25. }
  26. }

55555 - HJ60 查找组成一个偶数最接近的两个素数

  1. // https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
  2. // 任意一个偶数(大于2)都可以由2个素数组成,组成偶数的2个素数有很多种情况,
  3. // 本题目要求输出组成指定偶数的两个素数差值最小的素数对
  4. // 输入:20
  5. // 输出:
  6. // 7
  7. // 13
  8. import java.util.Scanner;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNextInt()) {
  15. int num = in.nextInt();
  16. // 两个素数差值最小的素数对:肯定位于靠近num中点,所以从中点开始遍历查找
  17. // i越来越小,num-i就越来越大。i肯定不大于num-i
  18. for(int i = num/2; i >= 2; i--){
  19. if(isPrime(i) && isPrime(num - i)){
  20. System.out.println(i);
  21. System.out.println(num - i);
  22. break;
  23. }
  24. }
  25. }
  26. }
  27. // 判断素数的方法
  28. public static boolean isPrime(int num){
  29. for(int i = 2; i <= Math.sqrt(num); i++){
  30. if(num % i == 0){
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. }

HJ61 放苹果 - 11111

  1. // https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
  2. // 把m个同样的苹果放在n个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?
  3. // 注意:如果有7个苹果和3个盘子,(5,1,1)和(1,5,1)被视为是同一种分法
  4. // 数据范围:0 <= m <= 10,1 ≤ n ≤ 10
  5. // 输入:7 3
  6. // 输出:8
  7. import java.util.Scanner;
  8. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  9. public class Main {
  10. public static void main(String[] args) {
  11. Scanner in = new Scanner(System.in);
  12. // 注意 hasNext 和 hasNextLine 的区别
  13. while (in.hasNextInt()) {
  14. int m = in.nextInt();
  15. int n = in.nextInt();
  16. //【1】dp[i][j]表示i个苹果、放入n个盘子中的放置方案
  17. int[][] dp = new int[m+1][n+1];
  18. //【2】若只有一个盘子,则只有一个放置方案
  19. for(int i = 1; i <= m; i++){
  20. dp[i][1] = 1;
  21. }
  22. //【3】若只有一个苹果、或者没有苹果,则只有一个分配方案
  23. for(int j = 1; j <= n; j++){
  24. dp[0][j] = 1;
  25. dp[1][j] = 1;
  26. }
  27. for(int i = 2; i <= m; i++){
  28. for(int j = 2; j <= n; j++){
  29. if(i < j){
  30. //【4.1】若苹果比盘子数量少,则一定存在空盘子,所以去除这些空盘子,不影响放置方案数
  31. dp[i][j] = dp[i][i];
  32. }else{
  33. //【4.2】dp[i][j-1]:空置一个盘子,所有苹果只能放到j-1个盘子
  34. // dp[i-j][j]:不空置盘子,所有盘子至少放一个苹果,等价于所有盘子都少放一个苹果的方案数
  35. dp[i][j] = dp[i][j-1] + dp[i-j][j];
  36. }
  37. }
  38. }
  39. //【5】输出m个苹果放到n个盘子的放置方案数
  40. System.out.println(dp[m][n]);
  41. }
  42. }
  43. }

HJ62 查找输入整数二进制中1的个数 - 0

  1. // https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) {
  9. int num = in.nextInt();
  10. String binary = Integer.toBinaryString(num);
  11. int count = 0;
  12. for(int i = 0; i < binary.length(); i++){
  13. if(binary.charAt(i) == '1'){
  14. count++;
  15. }
  16. }
  17. System.out.println(count);
  18. }
  19. }
  20. }

HJ72 百钱买百鸡问题 - 1

  1. // https://www.nowcoder.com/practice/74c493f094304ea2bda37d0dc40dc85b
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. // 15x + 9y + z = 300
  6. // x + y + z = 100
  7. // 14x + 8y = 200
  8. // 7x + 4y = 100
  9. // x -> [0, 20] y -> [0, 33]
  10. public static void main(String[] args) {
  11. Scanner in = new Scanner(System.in);
  12. // 注意 hasNext 和 hasNextLine 的区别
  13. while (in.hasNextInt()) {
  14. // 看清楚题意:现要求你打印出所有花一百元买一百只鸡的方式,是打印100,而不是num
  15. int num = in.nextInt();
  16. for( int x = 0; x <= 100; x+=4){
  17. int y = (100 - 7*x) / 4;
  18. int z = 100 - x - y;
  19. if((y >= 0 && y <= 100) && (z >= 0 && z <= 100)){
  20. System.out.println(x + " " + y + " " + z);
  21. }
  22. }
  23. // for(int x = 0; x < 20; x++){
  24. // for(int y = 33; y >= 0; y--){
  25. // if(7*x + 4*y == 100){
  26. // int z = 100 - x - y;
  27. // System.out.println(x + " " + y + " " + z);
  28. // }
  29. // }
  30. // }
  31. }
  32. }
  33. }

55555 - HJ73 计算日期到天数转换

  1. // https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. String str = in.nextLine();
  10. String[] strs = str.split(" ");
  11. int year = Integer.valueOf(strs[0]);
  12. int month = Integer.valueOf(strs[1]);
  13. int day = Integer.valueOf(strs[2]);
  14. int[] month_day = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  15. int total = day;
  16. // 累加当前月的前面所有整数月的天数
  17. for(int i = 0; i < month - 1; i++){
  18. total += month_day[i];
  19. }
  20. // 闰年且月份超过2月。单独加1天
  21. if(month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){
  22. total += 1;
  23. }
  24. System.out.println(total);
  25. }
  26. }
  27. public void test02() {
  28. Scanner in = new Scanner(System.in);
  29. // 注意 hasNext 和 hasNextLine 的区别
  30. while (in.hasNextLine()) {
  31. String str = in.nextLine();
  32. String[] strs = str.split(" ");
  33. int year = Integer.valueOf(strs[0]);
  34. int month = Integer.valueOf(strs[1]);
  35. int day = Integer.valueOf(strs[2]);
  36. Calendar c1 = Calendar.getInstance(); // 实例化
  37. c1.set(year, month-1, day); // 注意月份从0开始
  38. System.out.println(c1.get(Calendar.DAY_OF_YEAR));
  39. }
  40. }
  41. public void test01() {
  42. Scanner in = new Scanner(System.in);
  43. // 注意 hasNext 和 hasNextLine 的区别
  44. while (in.hasNextLine()) {
  45. String str = in.nextLine();
  46. String[] strs = str.split(" ");
  47. int year = Integer.valueOf(strs[0]);
  48. int month = Integer.valueOf(strs[1]);
  49. int day = Integer.valueOf(strs[2]);
  50. int secondMonth = 29;
  51. // 公历年份是4的倍数,且不是100的倍数,为普通闰年。公历年份是整百数,且必须是400的倍数才是世纪闰年
  52. if(year % 4 != 0 || (year % 100 == 0 && year % 400 != 0)){
  53. secondMonth = 28;
  54. }
  55. if(month == 1){
  56. System.out.println(day);
  57. }
  58. else if(month == 2){
  59. System.out.println(day + 31);
  60. }
  61. else if(month == 3){
  62. System.out.println(day + 31 + secondMonth);
  63. }
  64. else if(month == 4){
  65. System.out.println(day + 31 + secondMonth + 31);
  66. }
  67. else if(month == 5){
  68. System.out.println(day + 31 + secondMonth + 31 + 30);
  69. }
  70. else if(month == 6){
  71. System.out.println(day + 31 + secondMonth + 31 + 30 + 31);
  72. }
  73. else if(month == 7){
  74. System.out.println(day + 31 + secondMonth + 31 + 30 + 31 + 30);
  75. }
  76. else if(month == 8){
  77. System.out.println(day + 31 + secondMonth + 31 + 30 + 31 + 30 + 31);
  78. }
  79. else if(month == 9){
  80. System.out.println(day + 31 + secondMonth + 31 + 30 + 31 + 30 + 31 + 31);
  81. }
  82. else if(month == 10){
  83. System.out.println(day + 31 + secondMonth + 31 + 30 + 31 + 30 + 31 + 31 + 30);
  84. }
  85. else if(month == 11){
  86. System.out.println(day + 31 + secondMonth + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
  87. }
  88. else if(month == 12){
  89. System.out.println(day + 31 + secondMonth + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
  90. }
  91. }
  92. }
  93. }

55555 - HJ76 尼科彻斯定理

  1. // https://www.nowcoder.com/practice/dbace3a5b3c4480e86ee3277f3fe1e85
  2. // 描述:验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。
  3. // 例如:
  4. // 1^3=1
  5. // 2^3=3+5
  6. // 3^3=7+9+11
  7. // 4^3=13+15+17+19
  8. // 输入一个正整数m(m≤100),将m的立方写成m个连续奇数之和的形式输出。
  9. // 输入描述:输入一个int整数
  10. // 输出描述:输出分解后的string
  11. // 示例1
  12. // 输入:6
  13. // 输出:31+33+35+37+39+41
  14. import java.util.Scanner;
  15. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  16. public class Main {
  17. public static void main(String[] args) {
  18. Scanner in = new Scanner(System.in);
  19. // 注意 hasNext 和 hasNextLine 的区别
  20. while (in.hasNextInt()) {
  21. int num = in.nextInt();
  22. //【1】计算中间数字
  23. int mid = num * num;
  24. //【2】根据中间数字计算最左边的数字
  25. // int left = 0;
  26. // if(num % 2 == 0){ // 偶数,mid-1表示中间数字左边的第一个数字
  27. // left = (mid - 1) - 2 * (num/2 - 1);
  28. // }else {
  29. // left = mid - 2 * (num / 2);
  30. // }
  31. int left = mid - (num - 1);
  32. // 第一项为n(n-1)+1,最后一项为n(n+1)-1
  33. // int left = num * (num - 1) + 1;
  34. StringBuffer str = new StringBuffer(String.valueOf(left));
  35. for(int i = 1; i < num; i++){
  36. left = left + 2;
  37. str.append("+" + left);
  38. }
  39. System.out.println(str.toString());
  40. }
  41. }
  42. }

HJ80 整型数组合并 - 0

  1. // https://www.nowcoder.com/practice/c4f11ea2c886429faf91decfaf6a310b
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. // Set<Integer> set = new HashSet<>();
  10. // 无序不可重复,但是可以按照元素的大小顺序自动排序
  11. Set<Integer> set = new TreeSet<>();
  12. int num1 = Integer.parseInt(in.nextLine());
  13. String[] str1 = in.nextLine().split(" ");
  14. for(int i = 0; i < num1; i++){
  15. set.add(Integer.parseInt(str1[i]));
  16. }
  17. int num2 = Integer.parseInt(in.nextLine());
  18. String[] str2 = in.nextLine().split(" ");
  19. for(int i = 0; i < num2; i++){
  20. set.add(Integer.parseInt(str2[i]));
  21. }
  22. // set集合 转化为数组
  23. // int[] result = new int[set.size()];
  24. // int i = 0;
  25. // for (Integer num : set) {
  26. // result[i] = num;
  27. // i++;
  28. // }
  29. // Arrays.sort(result);
  30. // for(int k = 0; k < result.length; k++){
  31. // System.out.print(result[k]);
  32. // }
  33. // 无序不可重复,但是可以按照元素的大小顺序自动排序
  34. for(Integer num : set){
  35. System.out.print(num);
  36. }
  37. }
  38. }
  39. }

55555 - HJ81 字符串字符匹配

  1. // https://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. String str = in.nextLine();
  10. String str1 = in.nextLine();
  11. int count = 0;
  12. for(int i = 0; i < str.length(); i++){
  13. if(str1.contains(String.valueOf(str.charAt(i)))){
  14. count++;
  15. }
  16. }
  17. System.out.println(count == str.length());
  18. // int count = 0;
  19. // for(int i = 0; i < str.length(); i++){
  20. // for(int j = 0; j < str1.length(); j++){
  21. // if(str.charAt(i) == str1.charAt(j)){
  22. // count++;
  23. // break;
  24. // }
  25. // }
  26. // }
  27. // System.out.println(count == str.length());
  28. }
  29. }
  30. }

HJ83 二维数组操作

  1. // https://www.nowcoder.com/practice/2f8c17bec47e416897ce4b9aa560b7f4
  2. // 输入:
  3. // 4 9
  4. // 5 1 2 6
  5. // 0
  6. // 8
  7. // 2 3
  8. // 4 7
  9. // 4 2 3 2
  10. // 3
  11. // 3
  12. // 4 7
  13. // 输出:
  14. // 0
  15. // -1
  16. // 0
  17. // -1
  18. // 0
  19. // 0
  20. // -1
  21. // 0
  22. // 0
  23. // -1
  24. // 说明:
  25. 本组样例共有2组样例输入。
  26. 第一组样例:
  27. 1.初始化数据表为49列,成功
  28. 2.交换第51列和第26列的数据,失败。因为行的范围应该是(0,3),不存在第5行。
  29. 3.在第0行上方添加一行,成功。
  30. 4.在第8列左边添加一列,失败。因为列的总数已经达到了9的上限。
  31. 5.查询第2行第3列的值,成功。
  32. 第二组样例:
  33. 1.初始化数据表为47列,成功
  34. 2.交换第42列和第32列的数据,失败。因为行的范围应该是(0,3),不存在第4行。
  35. 3.在第3行上方添加一行,成功。
  36. 4.在第3列左边添加一列,成功。
  37. 5.查询第47列的值,失败。因为虽然添加了一行一列,但数据表会在添加后恢复成47列的形态,
  38. 所以行的区间仍然在[0,3],列的区间仍然在[0,6],无法查询到(4,7)坐标。
  39. import java.util.Scanner;
  40. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  41. public class Main {
  42. public static void main(String[] args) {
  43. Scanner in = new Scanner(System.in);
  44. // 注意 hasNext 和 hasNextLine 的区别
  45. while (in.hasNextInt()) {
  46. //【1】表格的行列值
  47. int rows = in.nextInt();
  48. int cols = in.nextInt();
  49. if (rows > 9 || cols > 9) {
  50. System.out.println(-1);
  51. } else {
  52. System.out.println(0);
  53. }
  54. //【2】要交换的两个单元格的行列值
  55. int x1 = in.nextInt();
  56. int y1 = in.nextInt();
  57. int x2 = in.nextInt();
  58. int y2 = in.nextInt();
  59. // 所有输入坐标操作,都是从0开始的
  60. if (x1 >= rows || y1 >= cols || x2 >= rows || y2 >= cols) {
  61. System.out.println(-1);
  62. } else {
  63. System.out.println(0);
  64. }
  65. //【3】输入要插入的行的数值
  66. // 插入后总行数不能超过9行,被插入的行数不能大于总行数(row是从0开始,rows是从1开始)
  67. int row = in.nextInt();
  68. if (rows + 1 > 9 || row >= rows) {
  69. System.out.println(-1);
  70. } else {
  71. System.out.println(0);
  72. }
  73. //【4】输出插入列是否成功
  74. // 插入后总列数不能超过9行,被插入的列数不能大于总列数
  75. int col = in.nextInt();
  76. if (cols + 1 > 9 || col >= cols) {
  77. System.out.println(-1);
  78. } else {
  79. System.out.println(0);
  80. }
  81. //【5】输出查询单元格数据是否成功
  82. int x = in.nextInt();
  83. int y = in.nextInt();
  84. if (x >= rows || y >= cols) {
  85. System.out.println(-1);
  86. } else {
  87. System.out.println(0);
  88. }
  89. }
  90. }
  91. }

HJ84 统计大写字母个数 - 0

  1. // https://www.nowcoder.com/practice/434414efe5ea48e5b06ebf2b35434a9c
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. String str = in.nextLine();
  10. int count = 0;
  11. for(int i = 0; i < str.length(); i++){
  12. if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'){
  13. count++;
  14. }
  15. }
  16. System.out.println(count);
  17. }
  18. }
  19. }

55555 - HJ85 最长回文子串

  1. // https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. String str = in.nextLine();
  10. int max = 0;
  11. int length = str.length();
  12. for (int i = 0; i < length; i++){
  13. for (int j = i + 1; j <= length; j++) {
  14. String test = str.substring(i, j);
  15. if (isPalindromic(test)) {
  16. max = Math.max(max, test.length());
  17. }
  18. }
  19. }
  20. System.out.println(max);
  21. }
  22. }
  23. public static boolean isPalindromic(String s) {
  24. int length = s.length();
  25. for (int i = 0; i < length / 2; i++) {
  26. if (s.charAt(i) != s.charAt(length - 1 - i)) {
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32. }

55555 - HJ86 求最大连续bit数

  1. // https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
  2. // 描述:求一个int类型数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
  3. // 输入描述:输入一个int类型数字
  4. // 输出描述:输出转成二进制之后连续1的个数
  5. // 示例1
  6. // 输入:200
  7. // 输出:2
  8. // 说明:200的二进制表示是11001000,最多有2个连续的1
  9. import java.util.Scanner;
  10. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  11. public class Main {
  12. public static void main(String[] args) {
  13. Scanner in = new Scanner(System.in);
  14. // 注意 hasNext 和 hasNextLine 的区别
  15. while (in.hasNextInt()) {
  16. int num = in.nextInt();
  17. String binary = Integer.toBinaryString(num);
  18. int max = 0;
  19. int count = 0;
  20. int left = 0;
  21. while(left < binary.length()){
  22. if(binary.charAt(left) == '1'){
  23. count++;
  24. max = Math.max(max, count);
  25. }else{
  26. count = 0;
  27. }
  28. left++;
  29. }
  30. System.out.println(max);
  31. }
  32. }
  33. }

55555 - HJ87 密码强度等级

  1. // https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. String str = in.next();
  10. int[] result = new int[5];
  11. int length = str.length();
  12. if(length <= 4){
  13. result[0] = 5;
  14. }else if(length >= 5 && length <= 7){
  15. result[0] = 10;
  16. }else{
  17. result[0] = 25;
  18. }
  19. int fuhaoSize = 0;
  20. for(int i = 0; i < str.length(); i++){
  21. char ch = str.charAt(i);
  22. if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
  23. result[1] = 10;
  24. }else if(ch >= '0' && ch <= '9'){
  25. result[2] = Math.min(result[2] + 10, 20);
  26. }else if(ch >= '!' && ch <= '/'){
  27. fuhaoSize++;
  28. }else if(ch >= ':' && ch <= '@'){
  29. fuhaoSize++;
  30. }else if(ch >= '[' && ch <= '`'){
  31. fuhaoSize++;
  32. }else if(ch >= '{' && ch <= '~'){
  33. fuhaoSize++;
  34. }
  35. }
  36. if(str.equals(str.toLowerCase())){
  37. result[1] = result[1];
  38. }else if(str.equals(str.toUpperCase())) {
  39. result[1] = result[1];
  40. }else{
  41. result[1] = 20;
  42. }
  43. if(fuhaoSize == 1){
  44. result[3] = 10;
  45. }else if(size > 1){
  46. result[3] = 25;
  47. }
  48. if(result[1] == 20 && result[2] > 0 && result[3] > 0){
  49. result[4] = 5;
  50. }else if(result[1] == 10 && result[2] > 0 && result[3] > 0){
  51. result[4] = 3;
  52. }else if(result[1] == 10 && result[2] > 0){
  53. result[4] = 2;
  54. }
  55. int count = 0;
  56. for (int i : result) {
  57. count = count + i;
  58. }
  59. if(count >= 90){
  60. System.out.println("VERY_SECURE");
  61. }else if(count >= 80){
  62. System.out.println("SECURE");
  63. }else if(count >= 70){
  64. System.out.println("VERY_STRONG");
  65. }else if(count >= 60){
  66. System.out.println("STRONG");
  67. }else if(count >= 50){
  68. System.out.println("AVERAGE");
  69. }else if(count >= 25){
  70. System.out.println("WEAK");
  71. }else{
  72. System.out.println("VERY_WEAK");
  73. }
  74. }
  75. }
  76. }

HJ91 走方格的方案数 - 0

  1. // https://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) {
  9. int n = in.nextInt();
  10. int m = in.nextInt();
  11. // n为横向的格子数,m为竖向的格子数,转换为线条数为格子数+1
  12. int[][] dp = new int[n+1][m+1];
  13. for(int i = 0; i <= n; i++){
  14. dp[i][0] = 1;
  15. }
  16. for(int i = 0; i <= m; i++){
  17. dp[0][i] = 1;
  18. }
  19. for(int i = 1; i <= n; i++){
  20. for(int j = 1; j <= m; j++){
  21. dp[i][j] = dp[i-1][j] + dp[i][j-1];
  22. }
  23. }
  24. System.out.println(dp[n][m]);
  25. }
  26. }
  27. }

HJ94 记票统计 - 11111

  1. // https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextLine()) {
  9. int num1 = Integer.parseInt(in.nextLine());
  10. String[] strs1 = in.nextLine().split(" ");
  11. // LinkedHashMap可以将key按照输入的顺序进行排序
  12. Map<String, Integer> map = new LinkedHashMap<>();
  13. for(String s : strs1){
  14. map.put(s, 0);
  15. }
  16. map.put("Invalid", 0);
  17. int num2 = Integer.parseInt(in.nextLine());
  18. String[] strs2 = in.nextLine().split(" ");
  19. for(String s : strs2){
  20. if(map.containsKey(s)){
  21. map.put(s, map.get(s) + 1);
  22. }else{
  23. map.put("Invalid", map.get("Invalid") + 1);
  24. }
  25. }
  26. for(Map.Entry<String, Integer> entry : map.entrySet()) {
  27. if(entry.getKey() != "Invalid"){
  28. System.out.println(entry.getKey() + " : " + entry.getValue());
  29. }
  30. }
  31. System.out.println("Invalid" + " : " + map.get("Invalid"));
  32. }
  33. }
  34. }

HJ96 表示数字 - 0

  1. // https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. String str = in.next();
  10. int length = str.length();
  11. StringBuffer result = new StringBuffer();
  12. for(int i = 0; i < length; i++){
  13. char ch = str.charAt(i);
  14. if(ch >= '0' && ch <= '9'){
  15. result.append("*" + ch);
  16. i++;
  17. while(i < length && (str.charAt(i) >= '0' && str.charAt(i) <= '9')){
  18. result.append(str.charAt(i));
  19. i++;
  20. }
  21. result.append("*");
  22. // 注意:此时的i已经到达数字的后一位非数字字符
  23. if(i < length){
  24. result.append(str.charAt(i));
  25. }
  26. }else{
  27. result.append(ch);
  28. }
  29. }
  30. System.out.println(result.toString());
  31. }
  32. }
  33. }

HJ97 记负均正 - 0

  1. // https://www.nowcoder.com/practice/6abde6ffcc354ea1a8333836bd6876b8
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) {
  9. int num = in.nextInt();
  10. int[] nums = new int[num];
  11. int count = 0;
  12. int sum = 0;
  13. int count1 = 0;
  14. for(int i = 0; i < num; i++){
  15. nums[i] = in.nextInt();
  16. if(nums[i] < 0){
  17. count++;
  18. }else if(nums[i] > 0){
  19. count1++;
  20. sum = sum + nums[i];
  21. }
  22. }
  23. // 保留一位小数
  24. double result = count1 == 0 ? 0.0 : (double)sum/(double)count1;
  25. System.out.println(count + " " + String.format("%.1f", result));
  26. }
  27. }
  28. }

HJ99 自守数 - 0

  1. // https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) {
  9. int num = in.nextInt();
  10. int count = 0;
  11. for(int i = 0; i <= num; i++){
  12. String str = String.valueOf(i * i);
  13. if(test(str, String.valueOf(i))){
  14. count++;
  15. }
  16. }
  17. System.out.println(count);
  18. }
  19. }
  20. // 判断target是否包含在str的尾部
  21. public static boolean test(String str, String target){
  22. for(int i = str.length()-1, j = target.length()-1; i >= 0 && j >= 0; i--,j--){
  23. if(str.charAt(i) != target.charAt(j)){
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. }

55555 - HJ100 等差数列

  1. // https://www.nowcoder.com/practice/f792cb014ed0474fb8f53389e7d9c07f
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNextInt()) {
  9. int num = in.nextInt();
  10. int result = 2;
  11. for(int i = 1; i < num; i++){
  12. result = result + (2 + i * 3);
  13. }
  14. System.out.println(result);
  15. }
  16. }
  17. }

55555 - HJ102 字符统计

map根据value排序

  1. // https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
  2. import java.util.*;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. String[] str = in.next().split("");
  10. // TreeMap根据其键的自然顺序排序,或者根据map创建时提供的Comparator排序
  11. Map<String, Integer> map = new TreeMap<String, Integer>();
  12. for (String s : str) {
  13. map.put(s, map.getOrDefault(s, 0) + 1);
  14. }
  15. // 找到最多字符的数量
  16. int max = 0;
  17. for (int value : map.values()) {
  18. max = Math.max(max, value);
  19. }
  20. while (max > 0){
  21. for (String key : map.keySet()){
  22. if(map.get(key) == max){
  23. System.out.print(key);
  24. }
  25. }
  26. max--;
  27. }
  28. }
  29. }
  30. public void test() {
  31. Scanner in = new Scanner(System.in);
  32. // 注意 hasNext 和 hasNextLine 的区别
  33. while (in.hasNext()) {
  34. String[] str = in.next().split("");
  35. Map<String, Integer> map = new TreeMap<String, Integer>();
  36. for (String s : str) {
  37. map.put(s, map.getOrDefault(s, 0) + 1);
  38. }
  39. List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
  40. Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
  41. @Override
  42. public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
  43. return o2.getValue() - o1.getValue();
  44. }
  45. });
  46. for (Map.Entry<String, Integer> entry : list) {
  47. System.out.print(entry.getKey());
  48. }
  49. }
  50. }
  51. }

55555 - HJ105 记负均正II

  1. // https://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. int temp = 0;
  8. int countF = 0; // 负数的个数
  9. int countZ = 0; // 非负数的个数
  10. double sum = 0; // 所有非负数的总和
  11. // 注意 hasNext 和 hasNextLine 的区别
  12. while (in.hasNextInt()) {
  13. temp = in.nextInt();
  14. if(temp < 0){
  15. countF++;
  16. }else{
  17. countZ++;
  18. sum += temp;
  19. }
  20. }
  21. System.out.println(countF);
  22. if(countZ == 0){
  23. System.out.println("0.0");
  24. }else{
  25. // System.out.printf("%.1f", sum / countZ);
  26. System.out.println(String.format("%.1f", sum / countZ));
  27. }
  28. }
  29. }

HJ106 字符逆序

  1. // https://www.nowcoder.com/practice/cc57022cb4194697ac30bcb566aeb47b
  2. // 输入:I am a student
  3. // 输出:tneduts a ma I
  4. import java.util.Scanner;
  5. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  6. public class Main {
  7. public static void main(String[] args) {
  8. Scanner in = new Scanner(System.in);
  9. // 注意 hasNext 和 hasNextLine 的区别
  10. while (in.hasNextLine()) {
  11. String str = in.nextLine();
  12. for(int i = str.length() - 1; i >= 0; i--){
  13. System.out.print(str.charAt(i));
  14. }
  15. }
  16. }
  17. }

HJ108 求最小公倍数 - 11111

  1. // https://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. // 方法一:两个数相乘除以最大公约数就是最小公倍数
  6. // 方法二:更相减损法是拿两个数中的较大值减去较小值,然后在减数、被减数、差之间选取两个较小值继续相减,直到减数和被减数相等,得出的数就是最大公约数
  7. public static void main(String[] args) {
  8. Scanner in = new Scanner(System.in);
  9. // 注意 hasNext 和 hasNextLine 的区别
  10. while (in.hasNextInt()) {
  11. int num1 = in.nextInt();
  12. int num2 = in.nextInt();
  13. int result = 0;
  14. if(num1 % num2 == 0){
  15. result = num1;
  16. }else if(num2 % num1 == 0){
  17. result = num2;
  18. }else{
  19. result = (num1 * num2) / test(num1, num2);
  20. }
  21. System.out.println(result);
  22. }
  23. }
  24. public static int test(int num1, int num2){
  25. return num2 == 0 ? num1 : test(num2, num1 % num2);
  26. // if(num1 == num2){
  27. // return num2;
  28. // }else if(num1 > num2){
  29. // int differ = num1 - num2;
  30. // return test(num2, differ);
  31. // }else{
  32. // int differ = num2 - num1;
  33. // return test(num1, differ);
  34. // }
  35. }
  36. }

image.png

中等

66666 - HJ16 购物单

  1. // https://www.nowcoder.com/practice/f9c6f980eeec43ef85be20755ddbeaf4
  2. 参考解答:https://blog.nowcoder.net/n/e82df6de55184248a2964a260b5c08ee?f=comment
  3. import java.util.Scanner;
  4. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner in = new Scanner(System.in);
  8. // 注意 hasNext 和 hasNextLine 的区别
  9. while (in.hasNextInt()) {
  10. int money = in.nextInt();
  11. int goodNum = in.nextInt();
  12. Goods[] goods = new Goods[goodNum];
  13. for(int i = 0; i < goodNum; i++){
  14. goods[i] = new Goods(); // 必须先对其实例化后,后续才能调用其属性
  15. }
  16. for(int i = 0; i < goodNum; i++) {
  17. int price = in.nextInt(); // 商品价格
  18. int priority = in.nextInt(); // 商品重要度
  19. int isMain = in.nextInt(); // 当前商品是否为主件
  20. goods[i].val = price;
  21. goods[i].sat = priority * price; // 该物品的满意度 = 物品的价格 * 重要度
  22. // 该物品是主件
  23. if (isMain == 0) {
  24. goods[i].main = true;
  25. // 该物品是附件。goods[isMain-1]:isMain-1表示主件的编号
  26. // goods[isMain-1].attach1 == -1表示该主件的1号附件还未购买,若1号附件已经购买了,则放到2号附件上
  27. } else if (goods[isMain-1].attach1 == -1) {
  28. goods[isMain - 1].attach1 = i;
  29. } else {
  30. goods[isMain - 1].attach2 = i;
  31. }
  32. }
  33. // dp[i][j]表示前i-1件物品,容量为j时的最大满意度
  34. int[][] dp = new int[goodNum + 1][money + 1];
  35. for(int i = 1; i <= goodNum; i++){
  36. for(int value = 0; value <= money; value++){
  37. //【1】与【2】顺序不能颠倒
  38. //【1】不购买该物品。goods[i-1]
  39. dp[i][value] = dp[i-1][value];
  40. //【2】若当前商品不是主件,则直接跳过当次循环,继续下一轮循环。因为不能单独购买附件
  41. if(!goods[i-1].main){
  42. continue;
  43. }
  44. //【3】购买该物品。首先需要判断当前的总价钱 大于 该商品的价格,才能购买下来
  45. if(value >= goods[i-1].val){
  46. // dp[i-1][value-goods[i-1].val]表示购买当前商品之前(且已经为购买当前商品预留了足够多的钱)的最大满意度
  47. // 其中value-goods[i-1].val表示要想购买当前商品,最多能花费的钱(即要为当前商品保留的钱:goods[i-1].val)
  48. // goods[i-1].sat表示当前商品的满意度
  49. dp[i][value] = Math.max(dp[i][value], dp[i-1][value-goods[i-1].val] + goods[i-1].sat);
  50. }
  51. //【4】购买该物品及附件1
  52. if(goods[i-1].attach1 != -1 && value >= goods[i-1].val + goods[goods[i-1].attach1].val){
  53. // weight表示未购买当前商品及附件1之前(且已经为购买当前商品及附件1预留了足够多的钱)的最大满意度
  54. // value - goods[i-1].val - goods[goods[i-1].attach1].val表示购买当前商品及附件1,先前最多能够花费的钱
  55. int weight = dp[i-1][value - goods[i-1].val - goods[goods[i-1].attach1].val];
  56. // goods[i-1].sat表示当前商品的满意度
  57. // goods[goods[i-1].attach1].sat表示当前商品附件1的满意度
  58. dp[i][value] = Math.max(dp[i][value], weight + goods[i-1].sat + goods[goods[i-1].attach1].sat);
  59. }
  60. //【5】购买该物品及附件2
  61. if(goods[i-1].attach2 != -1 && value >= goods[i-1].val + goods[goods[i-1].attach2].val){
  62. int weight = dp[i-1][value - goods[i-1].val - goods[goods[i-1].attach2].val];
  63. // goods[i-1].sat表示当前商品的满意度
  64. // goods[goods[i-1].attach1].sat表示当前商品附件2的满意度
  65. dp[i][value] = Math.max(dp[i][value], weight + goods[i-1].sat + goods[goods[i-1].attach2].sat);
  66. }
  67. //【6】购买该物品及附件1及附件2
  68. if(goods[i-1].attach1 != -1 && goods[i-1].attach2 != -1 && value >= goods[i-1].val + goods[goods[i-1].attach1].val + goods[goods[i-1].attach2].val){
  69. // weight表示未购买当前商品及附件1与附件2之前(且已经为购买当前商品及附件1与附件2预留了足够多的钱)的最大满意度
  70. // value - goods[i-1].val - goods[goods[i-1].attach1].val - goods[goods[i-1].attach2].val表示购买当前商品及附件1与附件2,先前最多能够花费的钱
  71. int weight = dp[i-1][value - goods[i-1].val - goods[goods[i-1].attach1].val - goods[goods[i-1].attach2].val];
  72. dp[i][value] = Math.max(dp[i][value], weight + goods[i-1].sat + goods[goods[i-1].attach1].sat + goods[goods[i-1].attach2].sat);
  73. }
  74. }
  75. }
  76. System.out.println(dp[goodNum][money]);
  77. }
  78. }
  79. }
  80. class Goods {
  81. int val; // 表示该物品的价格
  82. int sat; // 表示该物品的满意度 = 物品的价格 * 重要度
  83. boolean main = false; // 表示该物品是主件还是附件
  84. int attach1 = -1; // 定义附件1的编号
  85. int attach2 = -1; // 定义附件2的编号
  86. }

55555 - HJ17 坐标移动

  1. // https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
  2. // 下面是一个简单的例子 如:
  3. // A10;S20;W10;D30;X;A1A;B10A11;;A10;
  4. // 处理过程:
  5. // 起点(0,0)
  6. // + A10 = (-10,0)
  7. // + S20 = (-10,-20)
  8. // + W10 = (-10,-10)
  9. // + D30 = (20,-10)
  10. // + x = 无效
  11. // + A1A = 无效
  12. // + B10A11 = 无效
  13. // + 一个空 不影响
  14. // + A10 = (10,-10)
  15. // 结果 (10, -10
  16. import java.util.*;
  17. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  18. public class Main {
  19. public static void main(String[] args) {
  20. Scanner in = new Scanner(System.in);
  21. // 注意 hasNext 和 hasNextLine 的区别
  22. while (in.hasNext()) {
  23. String[] strs = in.next().split(";");
  24. //起始位置ints
  25. int[] ints = new int[]{0, 0};
  26. for(String str : strs){
  27. if(str.length() > 3 || str.length() < 2) continue;
  28. // 判断首位是固定大写字母
  29. if(str.charAt(0) != 'A' && str.charAt(0) != 'D'
  30. && str.charAt(0) != 'W' && str.charAt(0) != 'S') continue;
  31. // 判断后两位是数字
  32. if(str.length() == 2 && !Character.isDigit(str.charAt(1))) continue;
  33. if(str.length() == 3
  34. && (!Character.isDigit(str.charAt(1)) || !Character.isDigit(str.charAt(2)))) continue;
  35. // 取后两位数字
  36. int num = Integer.valueOf(str.substring(1));
  37. if(str.charAt(0) == 'A'){
  38. ints[0] = ints[0] - num;
  39. }else if(str.charAt(0) == 'D'){
  40. ints[0] = ints[0] + num;
  41. }else if(str.charAt(0) == 'S'){
  42. ints[1] = ints[1] - num;
  43. }else{
  44. ints[1] = ints[1] + num;
  45. }
  46. }
  47. // Arrays.toString(ints)结果为[10, -10] 需要转换为10,10
  48. System.out.println(Arrays.toString(ints).replace("[", "").replace("]", "").replace(" ", ""));
  49. }
  50. }
  51. }

HJ20 密码验证合格程序 - 11111

  1. // https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. String str = in.next();
  10. //【1】验证长度是否超过8位
  11. if(str.length() < 8){
  12. System.out.println("NG");
  13. continue;
  14. }
  15. //【2】验证包括大小写字母.数字.其它符号, 以上四种至少三种
  16. int count1 = 0;
  17. int count2 = 0;
  18. int count3 = 0;
  19. int count4 = 0;
  20. for(int i = 0; i < str.length(); i++){
  21. char ch = str.charAt(i);
  22. if(ch >= '0' && ch <= '9'){
  23. count1 = 1;
  24. }else if(ch >= 'a' && ch <= 'z'){
  25. count2 = 1;
  26. }else if(ch >= 'A' && ch <= 'Z'){
  27. count3 = 1;
  28. }else{
  29. count4 = 1;
  30. }
  31. }
  32. if(count1 + count2 + count3 + count4 < 3){
  33. System.out.println("NG");
  34. continue;
  35. }
  36. //【3.1】校验是否有重复子串
  37. // if(getString(str, 0, 3)){
  38. // System.out.println("NG");
  39. // continue;
  40. // }
  41. //【3.2】校验是否有重复子串
  42. int num = 0;
  43. for(int i = 0; i <= str.length() - 3; i++){
  44. for(int j = i+3; j <= str.length() - 3; j++){
  45. if((str.charAt(i) == str.charAt(j))
  46. && (str.charAt(i+1) == str.charAt(j+1))
  47. && (str.charAt(i+2) == str.charAt(j+2))){
  48. num++;
  49. }
  50. // 一旦出现重复的子字符串(长度为3),就及时跳出for循环,避免做无用功
  51. if(num != 0) break;
  52. }
  53. // 一旦出现重复的子字符串(长度为3),就及时跳出for循环,避免做无用功
  54. if(num != 0) break;
  55. }
  56. if(num != 0){
  57. System.out.println("NG");
  58. continue;
  59. }
  60. System.out.println("OK");
  61. }
  62. }
  63. // 校验是否有重复子串
  64. private static boolean getString(String str, int left, int right) {
  65. if(right >= str.length()) {
  66. return false;
  67. }
  68. // [0, left, right, str.length]
  69. // str.substring(right):right为起始位置,字符串末尾为结束位置
  70. // str.substring(left, right):left为起始位置,right-1为结束位置
  71. if(str.substring(right).contains(str.substring(left, right))) {
  72. return true;
  73. }else{
  74. return getString(str, left + 1, right + 1);
  75. }
  76. }
  77. }

HJ24 合唱队 - 1

  1. // https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4
  2. // 输入:8
  3. // 186 186 150 200 160 130 197 200
  4. // 输出:4
  5. // 说明:由于不允许改变队列元素的先后顺序,所以最终剩下的队列应该为186 200 160 130或150 200 160 130
  6. // 合唱队形:比如1 2 3 4 5 3 2,由低到高、再由高到低
  7. import java.util.Scanner;
  8. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  9. public class Main {
  10. public static void main(String[] args) {
  11. Scanner in = new Scanner(System.in);
  12. // 注意 hasNext 和 hasNextLine 的区别
  13. while (in.hasNextLine()) {
  14. int num = Integer.parseInt(in.nextLine());
  15. String[] strs = in.nextLine().split(" ");
  16. int[] nums = new int[num];
  17. for(int i = 0; i < num; i++){
  18. nums[i] = Integer.parseInt(strs[i]);
  19. }
  20. //【1】left[i]表示从起始位置到第i个元素,递增子序列最长长度
  21. int[] left = new int[num];
  22. for(int i = 0; i < num; i++){
  23. left[i] = 1;
  24. for(int j = 0; j < i; j++){
  25. if(nums[i] > nums[j]){
  26. // 最大值存在依次比较的过程
  27. left[i] = Math.max(left[i], left[j]+1);
  28. }
  29. }
  30. }
  31. //【2】right[i]表示从第i个元素到末尾位置,递减子序列最长长度
  32. int[] right = new int[num];
  33. for(int i = num-1; i >= 0; i--){
  34. right[i] = 1;
  35. for(int j = num-1; j > i; j--){
  36. if(nums[i] > nums[j]){
  37. right[i] = Math.max(right[i], right[j]+1);
  38. }
  39. }
  40. }
  41. //【3】result[i]表示最高点在nums[i],其左边递增、右边递减的子序列最长长度
  42. int[] result = new int[num];
  43. int max = 0;
  44. for(int i = 0; i < num; i++){
  45. result[i] = left[i] + right[i] - 1; // 左右两边同时加了nums[i],所以长度要减1
  46. max = Math.max(max, result[i]);
  47. }
  48. // (num - max)表示最少需要剔除的元素
  49. System.out.println(num - max);
  50. }
  51. }
  52. }

55555 - HJ26 字符串排序

  1. // // https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
  2. // 规则 1 :英文字母从 A 到 Z 排列,不区分大小写。
  3. // 如,输入: Type 输出: epTy
  4. // 规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。
  5. // 如,输入: BabA 输出: aABb
  6. // 规则 3 :非英文字母的其它字符保持原来的位置。
  7. // 如,输入: By?e 输出: Be?y
  8. // 输入:A Famous Saying: Much Ado About Nothing (2012/8).
  9. // 输出:A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
  10. import java.util.*;
  11. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  12. public class Main {
  13. public static void main(String[] args) {
  14. Scanner in = new Scanner(System.in);
  15. // 注意 hasNext 和 hasNextLine 的区别
  16. while (in.hasNextLine()) {
  17. String str = in.nextLine();
  18. //【1】先将所有大小写英文字母收集起来
  19. List<Character> letters = new ArrayList<>();
  20. for (char ch : str.toCharArray()) {
  21. // 判断是否为英文字母
  22. if (Character.isLetter(ch)) {
  23. letters.add(ch);
  24. }
  25. }
  26. //【2】将英文字母进行排序
  27. letters.sort(new Comparator<Character>() {
  28. public int compare(Character o1, Character o2) {
  29. return Character.toLowerCase(o1) - Character.toLowerCase(o2);
  30. }
  31. });
  32. //【3】若是非英文字母则直接添加
  33. StringBuilder result = new StringBuilder();
  34. for (int i = 0, j = 0; i < str.length(); i++) {
  35. //【3.1】添加英文字母
  36. if (Character.isLetter(str.charAt(i))) {
  37. result.append(letters.get(j));
  38. j++;
  39. //【3.2】添加非英文字母
  40. }else {
  41. result.append(str.charAt(i));
  42. }
  43. }
  44. System.out.println(result.toString());
  45. }
  46. }
  47. }

55555 - HJ27 查找兄弟单词

  1. // https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
  2. // 输入:6 cab ad abcd cba abc bca abc 1
  3. // 输出:3
  4. // bca
  5. // 说明:
  6. // abc的兄弟单词有cab cba bca,所以输出3
  7. // 经字典序排列后,变为bca cab cba,所以第1个字典序兄弟单词为bca
  8. import java.util.*;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNext()) {
  15. int num = in.nextInt();
  16. //【1】list集合接收字典单词,并进行排序
  17. List<String> list = new ArrayList<>();
  18. for(int i = 0; i < num; i++){
  19. list.add(in.next());
  20. }
  21. Collections.sort(list);
  22. //【2】获取目标单词的兄弟单词
  23. String target = in.next();
  24. int count = 0;
  25. for(int i = 0; i < list.size(); ){
  26. String str = list.get(i);
  27. // 兄弟单词不能是自己,兄弟单词之间排序后相同
  28. if(!str.equals(target) && check(str, target)){
  29. count++;
  30. i++; // 必须在这个位置加1
  31. }else{
  32. list.remove(i);
  33. }
  34. }
  35. System.out.println(count);
  36. //【3】输出按照字典顺序排序后的第k个兄弟单词,此时的list集合中都存放的是target的兄弟单词
  37. int n = in.nextInt();
  38. String result = list.size() >= n ? list.get(n-1) : "";
  39. if(!result.isEmpty()){
  40. System.out.println(result);
  41. }
  42. }
  43. }
  44. // 比较两个字符串是否相等
  45. public static boolean check(String str, String target){
  46. char[] strs = str.toCharArray();
  47. char[] targets = target.toCharArray();
  48. Arrays.sort(strs);
  49. Arrays.sort(targets);
  50. // 比较两个数组是否相等
  51. return Arrays.equals(strs, targets);
  52. }
  53. }

55555 - HJ29 字符串加解密

  1. // https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
  2. // 输入:abcdefg
  3. // BCDEFGH
  4. // 输出:BCDEFGH
  5. // abcdefg
  6. import java.util.Scanner;
  7. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  8. public class Main {
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. // 注意 hasNext 和 hasNextLine 的区别
  12. while (in.hasNext()) {
  13. // 未加密的英文字母
  14. char[] sources = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
  15. // 已加密的英文字母
  16. char[] enciphers = "bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890".toCharArray();
  17. //【1】加密过程
  18. char[] chars1 = in.next().toCharArray();
  19. for(int i = 0; i < chars1.length; i++){
  20. char ch = chars1[i];
  21. if(ch >= 'A' && ch <= 'Z'){
  22. chars1[i] = enciphers[ch - 'A'];
  23. }else if(ch >= 'a' && ch <= 'z'){
  24. chars1[i] = enciphers[ch - 'a' + 26];
  25. }else{
  26. chars1[i] = enciphers[ch - '0' + 52];
  27. }
  28. }
  29. StringBuffer result1 = new StringBuffer();
  30. for(char ch : chars1){
  31. result1.append(ch);
  32. }
  33. //【2】解密过程
  34. char[] chars2 = in.next().toCharArray();
  35. for(int i = 0; i < chars2.length; i++){
  36. char ch = chars2[i];
  37. if(ch >= 'b' && ch <= 'z'){
  38. chars2[i] = sources[ch - 'b'];
  39. }else if(ch == 'a'){
  40. chars2[i] = 'Z';
  41. }else if(ch >= 'B' && ch <= 'Z'){
  42. chars2[i] = sources[ch - 'B' + 26];
  43. }else if(ch == 'A' && ch <= 'Z'){
  44. chars2[i] = 'z';
  45. }else if(ch >= '1' && ch <= '9'){
  46. chars2[i] = sources[ch - '1' + 52];
  47. }else{
  48. chars2[i] = '9';
  49. }
  50. }
  51. StringBuffer result2 = new StringBuffer();
  52. for(char ch : chars2){
  53. result2.append(ch);
  54. }
  55. System.out.println(result1);
  56. System.out.println(result2);
  57. }
  58. }
  59. }

HJ32 密码截取

  1. // https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
  2. // 输入:ABBBA
  3. // 输出:5
  4. // 输入:12HHHHA
  5. // 输出:4
  6. import java.util.Scanner;
  7. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  8. public class Main {
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. // 注意 hasNext 和 hasNextLine 的区别
  12. while (in.hasNext()) {
  13. String str = in.next();
  14. int max = 1;
  15. for(int i = 0; i < str.length(); i++){
  16. for(int j = i+1; j < str.length(); j++){
  17. if(check(str, i, j)){
  18. max = Math.max(max, j - i + 1);
  19. }
  20. }
  21. }
  22. System.out.println(max);
  23. }
  24. }
  25. public static boolean check(String str, int left, int right){
  26. while(left < right){
  27. if(str.charAt(left) != str.charAt(right)){
  28. return false;
  29. }
  30. left++;
  31. right--;
  32. }
  33. return true;
  34. }
  35. }

55555 - HJ33 整数与IP地址间的转换

  1. // https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
  2. // 举例:一个ip地址为10.0.3.193
  3. // 每段数字 相对应的二进制数
  4. // 10 00001010
  5. // 0 00000000
  6. // 3 00000011
  7. // 193 11000001
  8. // 组合起来即为:00001010 00000000 00000011 11000001,转换为10进制数就是:167773121,即该IP地址转换后的数字就是它了
  9. // 输入:10.0.3.193
  10. // 167969729
  11. // 输出:167773121
  12. // 10.3.3.193
  13. import java.util.*;
  14. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  15. public class Main {
  16. public static void main(String[] args) {
  17. Scanner in = new Scanner(System.in);
  18. // 注意 hasNext 和 hasNextLine 的区别
  19. while (in.hasNext()) {
  20. String[] strs = in.next().split("\\.");
  21. //【1】IP地址 转 整数
  22. StringBuilder sb = new StringBuilder();
  23. for (String str : strs) {
  24. //【1.1】整数 转 二进制
  25. String st = Integer.toBinaryString(Integer.parseInt(str));
  26. //【1.2】二进制凑足8位(不足8位的在前面加0)
  27. while (st.length() < 8) st = "0" + st;
  28. sb.append(st);
  29. }
  30. //【1.3】字符串 转 长整形(避免int越界)
  31. long result = Long.parseLong(sb.toString(), 2);
  32. System.out.println(result);
  33. //【2】整数 转 IP地址
  34. long num = in.nextLong();
  35. //【2.1】整数 转 二进制,二进制凑足32位(不足32位的在前面加0)
  36. String str = Long.toBinaryString(num);
  37. while (str.length() < 32) str = "0" + str;
  38. //【2.2】每8位分割二进制字符串
  39. StringBuffer buffer = new StringBuffer();
  40. for(int i = 0; i <= str.length() - 8; i += 8){
  41. //【2.3】截取字符串,并转换为十进制
  42. String sub = str.substring(i, i+8);
  43. int ints = Integer.parseInt(sub, 2);
  44. buffer.append(ints + ".");
  45. }
  46. //【2.4】删除最后一个多余的"."
  47. buffer.deleteCharAt(buffer.length() - 1);
  48. System.out.println(buffer.toString());
  49. }
  50. }
  51. }

55555 - HJ36 字符串加密

  1. // https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
  2. // 输入:nihao
  3. // ni
  4. // 输出:le
  5. // 小写字母原始字母表:[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]
  6. // 通过key:nihao加密后的字母表:[n, i, h, a, o, b, c, d, e, f, g, j, k, l, m, p, q, r, s, t, u, v, w, x, y, z]
  7. // 根据字母(ni)在原始字母表target中的索引,在加密后的字母表list中查找:le
  8. import java.util.*;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNext()) {
  15. //【1】题意中要求输入的字符串中仅包含小写字母,target表示字母表
  16. String[] target = "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".toLowerCase().split(" ");
  17. //【2】list存放加密后的字母表,先根据target初始化list
  18. List<String> list = new ArrayList<>(Arrays.asList(target));
  19. String[] keys = in.next().split("");
  20. //【2.1】根据key对字母表进行加密。必须要逆序
  21. for(int i = keys.length-1; i >= 0; i--){
  22. list.remove(keys[i]); // 先删除指定字母
  23. list.add(0, keys[i]); // 再在首位添加指定字母
  24. }
  25. //【3】根据字母在原始字母表target中的索引,在加密后的字母表list中查找
  26. String strs = in.next();
  27. StringBuilder result = new StringBuilder();
  28. for (int i = 0; i < strs.length(); i++) {
  29. int index = strs.charAt(i) - 'a'; // index表示该字母在target中的索引位置
  30. result.append(list.get(index)); // 通过该索引位置,在加密后的字母表list中获取
  31. }
  32. System.out.println(result);
  33. }
  34. }
  35. }

HJ38 求小球落地5次后所经历的路程和第5次反弹的高度

  1. // https://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446
  2. // 假设一个球从任意高度自由落下,每次落地后反跳回原高度的一半; 再落下, 求它在第5次落地时,共经历多少米?第5次反弹多高?
  3. // 分别输出第5次落地时,共经过多少米以及第5次反弹多高
  4. // 输入:1
  5. // 输出:2.875
  6. // 0.03125
  7. import java.util.Scanner;
  8. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  9. public class Main {
  10. public static void main(String[] args) {
  11. Scanner in = new Scanner(System.in);
  12. // 注意 hasNext 和 hasNextLine 的区别
  13. while (in.hasNextInt()) {
  14. double num = (double)in.nextInt();
  15. // 第一次落地经过的距离:num
  16. // 第二次落地经过的距离:num/2 * 2 (先反弹到num/2,再触地)
  17. // 第三次落地经过的距离:num/2
  18. // 第四次落地经过的距离:num/2 / 2
  19. // 第五次落地经过的距离:num/2 / 2 / 2
  20. System.out.println(num + num + num / 2 + num / 2 / 2 + num / 2 / 2 / 2);
  21. // 每次落地后,反弹高度都是先前num的一半。num / 2 / 2 / 2 / 2 / 2
  22. System.out.println(num / Math.pow(2, 5));
  23. }
  24. }
  25. }

66666 - HJ41 称砝码

  1. // https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
  2. // 输入:
  3. // 2
  4. // 1 2
  5. // 2 1
  6. // 输出:5
  7. // 说明:可以表示出0,1,2,3,4五种重量。
  8. import java.util.*;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNextInt()) {
  15. // 砝码种类数
  16. int num = in.nextInt();
  17. //【1】每类砝码的重量
  18. int[] weight = new int[num];
  19. for (int i = 0; i < num; i++) {
  20. weight[i] = in.nextInt();
  21. }
  22. //【2】每类砝码的个数
  23. int[] nums = new int[num];
  24. for (int i = 0; i < num; i++) {
  25. nums[i] = in.nextInt();
  26. }
  27. //【3】存放所有可能的结果,不用担心重复问题
  28. Set<Integer> set = new HashSet<>();
  29. set.add(0);
  30. //【4】遍历每类砝码
  31. for (int i = 0; i < num; i++) {
  32. //【4.1】当前已有的结果集
  33. List<Integer> list = new ArrayList<>(set);
  34. //【4.2】遍历当前类型砝码的个数(可以分别取0 - nums[i]个当前类型砝码)
  35. for (int j = 0; j <= nums[i]; j++) {
  36. for (Integer res : list) {
  37. //【4.3】已有的结果 + 当前类砝码的总重量(单个重量*数量)
  38. // 砝码单个重量不能变,数量可以变化
  39. res = res + weight[i] * j;
  40. // 然后加入结果集
  41. set.add(res);
  42. }
  43. }
  44. }
  45. System.out.println(set.size());
  46. }
  47. }
  48. }

66666 - HJ43 迷宫问题

  1. // https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
  2. 输入:
  3. 5 5
  4. 0 1 0 0 0
  5. 0 1 1 1 0
  6. 0 0 0 0 0
  7. 0 1 1 1 0
  8. 0 0 0 1 0
  9. 输出:
  10. (0,0)
  11. (1,0)
  12. (2,0)
  13. (2,1)
  14. (2,2)
  15. (2,3)
  16. (2,4)
  17. (3,4)
  18. (4,4)
  19. import java.util.*;
  20. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  21. public class Main {
  22. public static void main(String[] args) {
  23. Scanner in = new Scanner(System.in);
  24. // 注意 hasNext 和 hasNextLine 的区别
  25. while (in.hasNextInt()) {
  26. int rows = in.nextInt();
  27. int cols = in.nextInt();
  28. //【1】构造迷宫
  29. int[][] map = new int[rows][cols];
  30. for (int i = 0; i < rows; i++) {
  31. for (int j = 0; j < cols; j++) {
  32. map[i][j] = in.nextInt();
  33. }
  34. }
  35. //【2】存储路径的数组
  36. List<int[]> path = new ArrayList<>();
  37. // DFS搜索路径:往路径的位置上标记1,因为迷宫只有一条通道,所以dfs肯定能返回true
  38. dfs(map, 0, 0, path);
  39. for (int[] p : path) {
  40. System.out.println("(" + p[0] + "," + p[1] + ")");
  41. }
  42. }
  43. }
  44. // 返回值:标记是否找到可通行的路径
  45. public static boolean dfs(int[][] map, int x, int y, List<int[]> path) {
  46. // 添加路径,并标记已走过该位置
  47. path.add(new int[]{x, y});
  48. map[x][y] = 1;
  49. // 终止条件:成功走到终点位置
  50. if (x == map.length-1 && y == map[0].length-1) {
  51. return true;
  52. }
  53. // 向下走
  54. if (x + 1 < map.length && map[x+1][y] == 0) {
  55. if (dfs(map, x+1, y, path)) return true;
  56. }
  57. // 向右走
  58. if (y + 1 < map[0].length && map[x][y+1] == 0) {
  59. if (dfs(map, x, y+1, path)) return true;
  60. }
  61. // 向上走
  62. if (x - 1 >= 0 && map[x-1][y] == 0) {
  63. if (dfs(map, x-1, y, path)) return true;
  64. }
  65. // 向左走
  66. if (y - 1 >= 0 && map[x][y-1] == 0) {
  67. if (dfs(map, x, y-1, path)) return true;
  68. }
  69. // 回溯:若根据上述操作(上下左右)都未能够走到终点位置,回退一步,重新规划路线
  70. path.remove(path.size() - 1);
  71. map[x][y] = 0;
  72. return false;
  73. }
  74. }

55555 - HJ45 名字的漂亮度

  1. // https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
  2. // 输入:2
  3. // zhangsan
  4. // lisi
  5. // 输出:192
  6. // 101
  7. // 说明:对于样例lisi,让i的漂亮度为26,l的漂亮度为25,s的漂亮度为24,lisi的漂亮度为25+26+24+26=101.
  8. import java.util.*;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNext()) {
  15. int num = in.nextInt();
  16. for(int i = 0; i < num; i++){
  17. String str = in.next();
  18. //【1】map保存每个字符的个数 <字符, 个数>。
  19. Map<Character, Integer> map = new HashMap<>();
  20. for (int j = 0; j < str.length(); j++) {
  21. char ch = str.charAt(j);
  22. map.put(ch, map.getOrDefault(ch, 0) + 1);
  23. }
  24. //【2】将Collection集合(不支持排序) 转换为list集合(支持排序)
  25. List<Integer> list = new ArrayList<>(map.values());
  26. //【3】list元素降序排列
  27. Collections.sort(list, (o1, o2) -> o2 - o1);
  28. int index = 26;
  29. int res = 0;
  30. //【4】较高的数量 与 较高的漂亮度相乘,并累加
  31. for (Integer value : list) {
  32. res = res + value * index;
  33. index--; // 一个漂亮度只能使用一次
  34. }
  35. System.out.println(res);
  36. }
  37. }
  38. }
  39. }

55555 - HJ48 从单向链表中删除指定值的节点

  1. // https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
  2. // 输入:5 2 3 2 4 3 5 2 1 4 3
  3. // 输出:2 5 4 1
  4. // 说明:形成的链表为2->5->3->4->1
  5. // 删掉节点3,返回的就是2->5->4->1
  6. import java.util.*;
  7. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  8. public class Main {
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. // 注意 hasNext 和 hasNextLine 的区别
  12. while (in.hasNextInt()) {
  13. int num = in.nextInt(); // 节点组数
  14. int head = in.nextInt(); // 头节点
  15. //【1】依次加入节点
  16. List<Integer> list = new ArrayList<>();
  17. list.add(head);
  18. for(int i = 2; i <= num; i++){
  19. // left -> right
  20. int right = in.nextInt();
  21. int left = in.nextInt();
  22. // 在left位置的后一位插入right
  23. list.add(list.indexOf(left)+1, right);
  24. }
  25. int delValue = in.nextInt();
  26. //【2】删除指定节点
  27. list.remove((Integer)delValue);
  28. // list.toString()输出格式:[1, 2, 3]
  29. String result = list.toString().replace("[", "").replace("]", "")
  30. .replace(",", "");
  31. System.out.println(result);
  32. }
  33. }
  34. }

55555 - HJ50 四则运算 - HJ54 表达式求值

  1. // https://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e
  2. // 输入:3+2*{1+2*[-4/(8-6)+7]}
  3. // 输出:25
  4. import java.util.*;
  5. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  6. public class Main {
  7. public static void main(String[] args) {
  8. Scanner in = new Scanner(System.in);
  9. // 注意hasNext 和 hasNextLine的区别
  10. while (in.hasNext()) {
  11. String str = in.next();
  12. str = str.replace("[", "(");
  13. str = str.replace("{", "(");
  14. str = str.replace("}", ")");
  15. str = str.replace("]", ")");
  16. System.out.println(slove(str));
  17. }
  18. }
  19. public static int slove(String str) {
  20. Stack<Integer> stack = new Stack<>();
  21. char[] chars = str.toCharArray();
  22. // 初始化符号为'+'
  23. char sign = '+';
  24. // 记录数字(主要是记录大于9的数字)
  25. int number = 0;
  26. for (int index = 0; index < str.length(); index++) {
  27. char ch = chars[index];
  28. //【1】当前字符是数字,拼接数字
  29. if (Character.isDigit(ch)) {
  30. number = number * 10 + (ch - '0');
  31. }
  32. //【2】当前字符是左括号
  33. if (ch == '(') {
  34. // 下标移动到括号后一位字符
  35. int right = index + 1;
  36. //【2.1】统计左括号的数量
  37. int count = 1;
  38. while (count > 0) {
  39. if (chars[right] == '(') count++; // 遇到左括号,左括号数+1
  40. if (chars[right] == ')') count--; // 遇到右括号,抵消一个左括号,形成闭环,左括号数-1
  41. right++;
  42. }
  43. //【2.2】递归求解当前已经闭环括号中的表达式,每次递归都是将元素放入到的一个新栈中,返回值就是ans
  44. // index+1:当前闭环括号的起始位置,index是第一个左括号位置
  45. // right-1:当前闭环括号中最后一个右括号的位置,right是右括号的下一个字符位置
  46. number = slove(str.substring(index+1, right-1));
  47. // 因为for循环要index++,所以此处要先回退一步到最后一个右括号的位置
  48. index = right - 1;
  49. }
  50. //【3】当前字符是运算符,或者通过步骤2、当前的index可能是字符串末尾上的右括号,将已知的数字处理后放进栈
  51. if (!Character.isDigit(ch) || index == str.length() - 1) {
  52. //【3.1】左边的符号若是'+', 直接放入栈
  53. if (sign == '+') {
  54. stack.push(number);
  55. //【3.2】左边的符号若是'-', 数字取相反后,再放入栈
  56. }else if (sign == '-') {
  57. stack.push(-1 * number);
  58. //【3.3】左边的符号若是'*', 将该数字与栈中弹出一个数字相乘后,再放入栈
  59. }else if (sign == '*') {
  60. stack.push(stack.pop() * number);
  61. //【3.4】左边的符号若是'/', 栈中弹出一个数字与该数字相除后,再放入栈
  62. }else if (sign == '/') {
  63. stack.push(stack.pop() / number);
  64. }
  65. // 更新运算符
  66. sign = ch;
  67. // 当前数字已运算完毕,必须重置为0
  68. number = 0;
  69. }
  70. }
  71. // 栈中数字求和得到结果
  72. int ans = 0;
  73. while (!stack.isEmpty()) {
  74. ans += stack.pop();
  75. }
  76. return ans;
  77. }
  78. }

55555 - HJ52 计算字符串的编辑距离

  1. // https://www.nowcoder.com/practice/3959837097c7413a961a135d7104c314
  2. // 输入:abcdefg
  3. // abcdef
  4. // 输出:1
  5. import java.util.Scanner;
  6. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  7. public class Main {
  8. public static void main(String[] args) {
  9. Scanner in = new Scanner(System.in);
  10. // 注意 hasNext 和 hasNextLine 的区别
  11. while (in.hasNext()) {
  12. String str = in.next();
  13. String str1 = in.next();
  14. //【1】dp[i][j]表示截止str[i-1] 与 str1[j-1]的编辑距离
  15. int[][] dp = new int[str.length() + 1][str1.length() + 1];
  16. //【2】初始化dp数组
  17. // dp[i][0]:以下标i-1为结尾的字符串word1,和空字符串word2,最近编辑距离为dp[i][0]。dp[i][0]就应该是i,对word1里的元素全部做删除操作,即:dp[i][0] = i
  18. // 同理dp[0][j] = j
  19. for(int i = 0; i <= str.length(); i++) dp[i][0] = i;
  20. for(int j = 0; j <= str1.length(); j++) dp[0][j] = j;
  21. int maxLength = 0;
  22. //【3】注意边界是小于等于
  23. for (int i = 1; i <= str.length(); i++) {
  24. for (int j = 1; j <= str1.length(); j++) {
  25. //【4】递推公式
  26. // 当word1[i-1] = word2[j-1]时:说明不用任何编辑,dp[i][j] = dp[i-1][j-1]
  27. // 当word1[i-1] != word2[j-1]时:dp[i][j] = min({dp[i-1][j-1], dp[i-1][j], dp[i][j-1]}) + 1
  28. // 操作一:word1删除一个元素,那么就是以下标i-2为结尾的word1 与 j-1为结尾的word2的最近编辑距离 再加上一个操作。即dp[i][j] = dp[i-1][j] + 1;
  29. // 操作二:word2删除一个元素,那么就是以下标i-1为结尾的word1 与 j-2为结尾的word2的最近编辑距离 再加上一个操作。即dp[i][j] = dp[i][j-1] + 1;
  30. // 操作三:替换元素,word1替换word1[i-1],使其与word2[j-1]相同,此时不用增加元素,那么以下标i-2为结尾的word1 与 j-2为结尾的word2的最近编辑距离 加上一个替换元素的操作。即 dp[i][j] = dp[i-1][j-1] + 1;
  31. if (str.charAt(i - 1) == str1.charAt(j - 1)) {
  32. dp[i][j] = dp[i-1][j-1];
  33. } else {
  34. dp[i][j] = Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1])) + 1;
  35. }
  36. }
  37. }
  38. //【5】str[i] 与 str1[j]的最近编辑距离
  39. System.out.println(dp[str.length()][str1.length()]);
  40. }
  41. }
  42. }

HJ55 挑7

  1. // https://www.nowcoder.com/practice/ba241b85371c409ea01ac0aa1a8d957b
  2. // 输出 1到n之间 的与 7 有关数字的个数
  3. // 一个数与7有关是指这个数是 7 的倍数,或者是包含 7 的数字(如 17 ,27 ,37 ... 70 ,71 ,72 ,73...)
  4. import java.util.Scanner;
  5. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  6. public class Main {
  7. public static void main(String[] args) {
  8. Scanner in = new Scanner(System.in);
  9. // 注意 hasNext 和 hasNextLine 的区别
  10. while (in.hasNextInt()) {
  11. int num = in.nextInt();
  12. int result = 0;
  13. for(int i = 7; i <= num; i++){
  14. if(String.valueOf(i).contains("7") || i % 7 == 0){
  15. result++;
  16. }
  17. }
  18. System.out.println(result);
  19. }
  20. }
  21. }

55555 - HJ57 高精度整数加法

  1. // https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
  2. // 输入:9876543210
  3. // 1234567890
  4. // 输出:11111111100
  5. import java.util.Scanner;
  6. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  7. public class Main {
  8. public static void main(String[] args) {
  9. Scanner in = new Scanner(System.in);
  10. // 注意 hasNext 和 hasNextLine 的区别
  11. while (in.hasNext()) {
  12. char[] chars1 = in.next().toCharArray();
  13. char[] chars2 = in.next().toCharArray();
  14. //【1】使用result接收求和结果,求和中每位元素可能大于9
  15. int length = chars1.length > chars2.length ? chars1.length : chars2.length;
  16. int[] result = new int[length];
  17. //【2】同时从右向左遍历两个字符串,每个对应的字符相加,将相加结果放到result数组中
  18. int index = length-1;
  19. for(int i = chars1.length-1, j = chars2.length-1; i >= 0 || j >= 0; i--, j--){
  20. //【2.1】chars1已遍历完,将chars2剩下的元素直接放到result数组中
  21. if(i < 0){
  22. result[index--] = chars2[j] - '0';
  23. //【2.2】chars2已遍历完,将char1剩下的元素直接放到result数组中
  24. }else if(j < 0){
  25. result[index--] = chars1[i] - '0';
  26. //【2.3】chars1与char2都有元素,将两个元素值相加后放到result数组中
  27. }else{
  28. result[index--] = (chars1[i] - '0') + (chars2[j] - '0');
  29. }
  30. }
  31. //【3】遍历result,若元素大于9,则向前进位
  32. // 第一位若大于9,也不需要处理,反正都是在其前面拼接
  33. for(int i = result.length-1; i > 0; i--){
  34. int temp = result[i];
  35. if(temp > 9){
  36. result[i] = temp % 10;
  37. result[i-1] = result[i-1] + temp / 10;
  38. }
  39. }
  40. StringBuilder sb = new StringBuilder();
  41. for(int i = 0; i < result.length; i++){
  42. sb.append(result[i] + "");
  43. }
  44. System.out.println(sb.toString());
  45. // 部分测试用例执行失败,原因是长度超过long的最大范围
  46. // System.out.println(Long.parseLong(str1) + Long.parseLong(str2));
  47. }
  48. }
  49. }

55555 - HJ59 找出字符串中第一个只出现一次的字符

  1. // https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
  2. // 输入:acsdfasdfo
  3. // 输出:c
  4. import java.util.*;
  5. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  6. public class Main {
  7. public static void main(String[] args) {
  8. Scanner in = new Scanner(System.in);
  9. // 注意 hasNext 和 hasNextLine 的区别
  10. while (in.hasNext()) {
  11. String str = in.next();
  12. //【1】map保存每个字符的个数 <字符, 个数>。
  13. // LinkedHashMap可以按照key的输入顺序排序,这样能够满足:输出第一个只出现一次的字符
  14. Map<Character, Integer> map = new LinkedHashMap<>();
  15. for (int i = 0; i < str.length(); i++) {
  16. char ch = str.charAt(i);
  17. map.put(ch, map.getOrDefault(ch, 0) + 1);
  18. }
  19. //【2】获取个数最少的字符
  20. Integer min = map.values().stream().min(Integer::compareTo).get();
  21. if(min != 1){
  22. System.out.println(-1);
  23. }else {
  24. //【3】输出第一个只出现一次的字符(此时的key已经按照输入顺序排列)
  25. for (Character ch : map.keySet()) {
  26. if(map.get(ch) == 1){
  27. System.out.println(ch + "");
  28. break;
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }

55555 - HJ63 DNA序列

  1. // https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
  2. // G 和 C 的比例(定义为 GC-Ratio )是序列中 G 和 C 两个字母的总的出现次数除以总的字母数目(也就是序列长度)
  3. // 输入:AACTGTGCACGACCTGA
  4. // 5
  5. // 输出:GCACG
  6. // 说明:虽然CGACC的GC-Ratio也是最高,但它是从左往右找到的GC-Ratio最高的第2个子串,
  7. // 所以只能输出GCACG。
  8. import java.util.*;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNext()) {
  15. String str = in.next();
  16. int num = in.nextInt();
  17. //【1】map存放不同CG个数的子串(子串长度为num)
  18. Map<Integer, String> map = new HashMap<>();
  19. for(int i = 0; i <= str.length()-num; i++){
  20. //【2】截取固定长度的子串
  21. String st = str.substring(i, i+num);
  22. int count = 0;
  23. //【3】遍历获取子串中GC的个数
  24. for(int j = 0; j < st.length(); j++){
  25. if(st.charAt(j) == 'C' || st.charAt(j) == 'G'){
  26. count++;
  27. }
  28. }
  29. //【4】putIfAbsent表示若key存在,则不更新。若key不存在,则插入value
  30. // 此时就能够满足:找出GC比例最高的子串, 如果有多个则输出第一个的子串
  31. map.putIfAbsent(count, st);
  32. }
  33. //【5】获取最大的key,即GC比例最高的子串(因为分母都是num,所以GC个数越多,比例越大)
  34. Integer max = map.keySet().stream().max(Integer::compareTo).get();
  35. System.out.println(map.get(max));
  36. }
  37. }
  38. }

55555 - HJ64 MP3光标位置

  1. // https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
  2. 参考解答:https://blog.nowcoder.net/n/6d044da93c384a6e8d68e7072118acb2?f=comment
  3. import java.util.Scanner;
  4. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner in = new Scanner(System.in);
  8. // 注意 hasNext 和 hasNextLine 的区别
  9. while (in.hasNextInt()) {
  10. int num = in.nextInt();
  11. String commands = in.next();
  12. // 页面的起始位置
  13. int start = 1;
  14. // 页面的末位置
  15. int end = Math.min(num, 4);
  16. // 光标初始的位置为第1首歌,三个位置都是从1开始
  17. int index = 1;
  18. for(int i = 0; i < commands.length(); i++) {
  19. // 向上移动和向下移动,确定光标的位置
  20. if(commands.charAt(i) == 'U') {
  21. if(index == 1){
  22. index = num;
  23. }else{
  24. index--;
  25. }
  26. }else if(commands.charAt(i) == 'D') {
  27. if(index == num){
  28. index = 1;
  29. }else{
  30. index++;
  31. }
  32. }
  33. // 判断滑动窗口的位置是否需要改变
  34. if(index < start) {
  35. // 光标在当前滑动窗口的上面(当start在歌曲的倒数4首时,index往下移动,到达1,即会出现这种场景)
  36. start = index;
  37. end = start + 3;
  38. }else if(index > end){
  39. // 光标在当前滑动窗口的下面(往下滑动时,可造成光标在滑动窗口之下)
  40. end = index;
  41. start = end - 3;
  42. }
  43. }
  44. // 输出窗口内容
  45. for(int i = start; i <= end; i++) {
  46. System.out.print(i + " ");
  47. }
  48. System.out.println();
  49. // 打印当前光标的位置
  50. System.out.println(index);
  51. }
  52. }
  53. }

55555 - HJ65 查找两个字符串a,b中的最长公共子串

  1. // https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
  2. // 查找两个字符串a,b中的最长公共子串。若有多个,输出在较短串中最先出现的那个
  3. // 输入:abcdefghijklmnop
  4. // abcsafjklmnopqrstuvw
  5. // 输出:jklmnop
  6. import java.util.*;
  7. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  8. public class Main {
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. // 注意 hasNext 和 hasNextLine 的区别
  12. while (in.hasNext()) {
  13. String str = in.next();
  14. String str1 = in.next();
  15. //【0】需要输出在较短串中最先出现的那个子串,所以较短串必须在外层循环,这样才能找到最先出现的那个子串
  16. if(str.length() > str1.length()){
  17. String temp = str;
  18. str = str1;
  19. str1 = temp;
  20. }
  21. //【1】方法一:动态规划
  22. //【1.1】dp[i][j]表示截止str[i-1] 与 str1[j-1]最大公共子串的长度
  23. int[][] dp = new int[str.length() + 1][str1.length() + 1];
  24. int maxLength = 0;
  25. int rightIndex = 0;
  26. //【1.2】注意边界是小于等于
  27. for (int i = 1; i <= str.length(); i++) {
  28. for (int j = 1; j <= str1.length(); j++) {
  29. //【1.3】递推公式
  30. if (str.charAt(i - 1) == str1.charAt(j - 1)) {
  31. dp[i][j] = dp[i - 1][j - 1] + 1;
  32. } else {
  33. dp[i][j] = 0;
  34. }
  35. //【1.4】更新最大值,更新最大公共子串的尾索引rightIndex在str中的位置
  36. if(dp[i][j] > maxLength){
  37. maxLength = dp[i][j];
  38. // 第一次满足最大值的时候才会更新,后续相同长度的子串不会进入这个循环
  39. rightIndex = i;
  40. }
  41. }
  42. }
  43. //【3】截取较短字符串,满足题意
  44. System.out.println(str.substring(rightIndex-maxLength, rightIndex));
  45. //【1】方法二:遍历
  46. //【1.1】map保存不同长度的公共子串
  47. Map<Integer, String> map = new HashMap<>();
  48. for (int i = 0; i < str.length(); i++) {
  49. for (int j = 0; j < str1.length(); j++) {
  50. int ii = i;
  51. int jj = j;
  52. int count = 0;
  53. //【1.2】从str的第i个元素 与 str2的第j个元素开始遍历,直到遇到不相等的元素为止,获取长度并更新结果
  54. while (ii < str.length() && jj < str1.length() &&
  55. str.charAt(ii) == str1.charAt(jj)) {
  56. ii++;
  57. jj++;
  58. count++;
  59. }
  60. //【1.3】putIfAbsent表示若key存在,则不更新。若key不存在,则插入value
  61. // 所以此处的value都是最先出现的满足长度的子串
  62. map.putIfAbsent(count, str1.substring(j, jj));
  63. }
  64. }
  65. //【2】获取key的最大值
  66. Integer max = map.keySet().stream().max(Integer::compareTo).get();
  67. // System.out.println(map.get(max));
  68. }
  69. }
  70. }

55555 - HJ66 配置文件恢复

  1. // https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
  2. // 为了简化输入,方便用户,以“最短唯一匹配原则”匹配(注:需从首字母开始进行匹配):
  3. // 1、若只输入一字串,则只匹配一个关键字的命令行。例如输入:r,根据该规则,匹配命令reset,执行结果为:reset what;输入:res,根据该规则,匹配命令reset,执行结果为:reset what;
  4. // 2、若只输入一字串,但匹配命令有两个关键字,则匹配失败。例如输入:reb,可以找到命令reboot backpalne,但是该命令有两个关键词,所有匹配失败,执行结果为:unknown command
  5. // 3、若输入两字串,则先匹配第一关键字,如果有匹配,继续匹配第二关键字,如果仍不唯一,匹配失败。
  6. // 例如输入:r b,找到匹配命令reset board 和 reboot backplane,执行结果为:unknown command。
  7. // 例如输入:b a,无法确定是命令board add还是backplane abort,匹配失败。
  8. // 4、若输入两字串,则先匹配第一关键字,如果有匹配,继续匹配第二关键字,如果唯一,匹配成功。例如输入:bo a,确定是命令board add,匹配成功。
  9. // 5、若输入两字串,第一关键字匹配成功,则匹配第二关键字,若无匹配,失败。例如输入:b addr,无法匹配到相应的命令,所以执行结果为:unknow command。
  10. // 6、若匹配失败,打印“unknown command”
  11. // 注意:有多组输入。
  12. // 参考解题:https://blog.nowcoder.net/n/38d4396e85934f5daa9540bce2d1a846?f=comment
  13. import java.util.*;
  14. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  15. public class Main {
  16. public static void main(String[] args) {
  17. Scanner in = new Scanner(System.in);
  18. //【1】初始化6条配置命令
  19. Map<String, String> map = new HashMap<>();
  20. map.put("reset", "reset what");
  21. map.put("reset board", "board fault");
  22. map.put("board add", "where to add");
  23. map.put("board delete", "no board at all");
  24. map.put("reboot backplane", "impossible");
  25. map.put("backplane abort", "install first");
  26. map.put("he he", "unknown command");
  27. //【2】将所有key放入set集合
  28. Set<String[]> commands = new HashSet<>();
  29. for (String s : map.keySet()) {
  30. commands.add(s.split(" "));
  31. }
  32. // 注意 hasNext 和 hasNextLine 的区别
  33. while (in.hasNextLine()) {
  34. String[] strs = in.nextLine().split(" ");
  35. String res = "he he"; // 初始化key
  36. int count = 0; // 初始化匹配成功到的命令数量
  37. //【3】遍历命令集合,将输入的命令与之一一进行判断
  38. for (String[] command : commands) {
  39. //【3.1】对应第一条规则:当输入命令长度为1时,此时要求匹配到的命令只有一个关键字,同时要求匹配到的命令以输入命令起始
  40. if (strs.length == 1 && command.length == 1 && command[0].startsWith(strs[0])) {
  41. res = command[0];
  42. //【3.2】对应第三、四条规则:当输入命令长度为2时,此时要求匹配到的命令有两个关键字,同时要求匹配到的两个命令分别是配置命令的起始
  43. } else if (strs.length == 2 && command.length == 2
  44. && command[0].startsWith(strs[0]) && command[1].startsWith(strs[1])) {
  45. res = command[0] + " " + command[1];
  46. count++; // 记录匹配成功的命令数量
  47. }
  48. }
  49. // 其余的规则:比如第二条、第五条、第六条,都是不满足上述两个规则,res等于初始值"he he"
  50. //【4】若匹配成功的命令数量大于1(对应第三条规则),则匹配失败。否则输出map.get(res)(若res为初始值"he he",也是匹配失败)
  51. System.out.println(count > 1 ? "unknown command" : map.get(res));
  52. }
  53. }
  54. }

55555 - 24点游戏算法

  1. // https://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
  2. // 输入描述:读入4个[1,10]的整数,数字允许重复,测试用例保证无异常数字。
  3. // 输出描述:对于每组案例,输出一行表示能否得到24点,能输出true,不能输出false
  4. // 输入:7 2 1 10
  5. // 输出:true
  6. import java.util.Scanner;
  7. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  8. public class Main {
  9. public static boolean flag = false;
  10. public static void main(String[] args) {
  11. Scanner in = new Scanner(System.in);
  12. // 注意 hasNext 和 hasNextLine 的区别
  13. while (in.hasNextInt()) {
  14. // 标记数组中的四个数字是否被使用过
  15. boolean[] visited = new boolean[4];
  16. int[] nums = new int[4];
  17. for (int i = 0; i < 4; i++) {
  18. nums[i] = in.nextInt();
  19. }
  20. dfs(nums, visited, 0, 0);
  21. System.out.println(flag);
  22. }
  23. }
  24. public static void dfs(int[] nums, boolean[] visited, int start, double sum) {
  25. // 递归终止条件:遍历完4个数字,start表示当前位置
  26. if(start == 4) {
  27. if(sum == 24) flag = true;
  28. }
  29. for(int i = 0; i < 4; i++) {
  30. // 当前数字未被使用过
  31. if(!visited[i]) {
  32. visited[i] = true; // 标记这轮循环中这个值已经访问过
  33. dfs(nums, visited, start + 1, sum + nums[i]);
  34. dfs(nums, visited, start + 1, sum - nums[i]);
  35. dfs(nums, visited, start + 1, sum * nums[i]);
  36. dfs(nums, visited, start + 1, sum / nums[i]);
  37. // 回溯:通过上述步骤(加减乘除当前数字),未能能够满足条件(sum == 24),就回退到上一步,重新规划上一个数字对应的运算符
  38. visited[i] = false;
  39. }
  40. }
  41. }
  42. }

55555 - HJ69 矩阵乘法 - 11111

牛客算法 - 图3

  1. // https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
  2. // 如果A是个x行y列的矩阵,B是个y行z列的矩阵,把A和B相乘,其结果将是另一个x行z列的矩阵C。
  3. // 这个矩阵的每个元素是由下面的公式决定的
  4. 输入:
  5. 2
  6. 3
  7. 2
  8. 1 2 3
  9. 3 2 1
  10. 1 2
  11. 2 1
  12. 3 3
  13. 输出:
  14. 14 13
  15. 10 11
  16. 说明:
  17. 1 2 3
  18. 3 2 1
  19. 乘以
  20. 1 2
  21. 2 1
  22. 3 3
  23. 等于
  24. 14 13
  25. 10 11
  26. import java.util.Scanner;
  27. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  28. public class Main {
  29. public static void main(String[] args) {
  30. Scanner in = new Scanner(System.in);
  31. // 注意 hasNext 和 hasNextLine 的区别
  32. while (in.hasNextInt()) {
  33. int x = in.nextInt(); // 数组A行数
  34. int y = in.nextInt(); // 数组A列数、数组B行数
  35. int z = in.nextInt(); // 数组B列数
  36. //【1】循环对数组A中的元素进行赋值
  37. int[][] A = new int[x][y];
  38. for(int i = 0; i < x; i++){
  39. for(int j = 0; j < y; j++){
  40. A[i][j] = in.nextInt();
  41. }
  42. }
  43. //【2】循环对数组B中的元素进行赋值
  44. int[][] B = new int[y][z];
  45. for(int i = 0; i < y; i++){
  46. for(int j = 0; j < z; j++){
  47. B[i][j] = in.nextInt();
  48. }
  49. }
  50. //【3】两个矩阵相乘的结果集数组C
  51. int[][] C = new int[x][z];
  52. for(int i = 0; i < x; i++){ // 数组A行数
  53. for(int j = 0; j < z; j++){ // 数组B列数
  54. for(int k = 0; k < y; k++){ // 数组A列数、数组B行数
  55. // 同时遍历数组A(横向遍历) 与 数组B(纵向遍历)
  56. // 将数组A的x行y列元素 与 数组B的y列z行元素分别相乘,并累加后,赋值给数组C的x行y列
  57. C[i][j] = C[i][j] + A[i][k] * B[k][j];
  58. }
  59. }
  60. }
  61. //【4】遍历输出结果集数组C
  62. for(int i = 0; i < x; i++){
  63. for(int j = 0; j < z; j++){
  64. System.out.print(C[i][j] + " ");
  65. }
  66. System.out.println();
  67. }
  68. }
  69. }
  70. }

55555 - HJ70 矩阵乘法计算量估算

  1. // https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
  2. // A是一个50×10的矩阵,B是10×20的矩阵,C是20×5的矩阵
  3. // 计算A*B*C有两种顺序:((AB)C)或者(A(BC)),前者需要计算15000次乘法,后者只需要3500次。
  4. // 编写程序计算不同的计算顺序需要进行的乘法次数
  5. // 输入:
  6. // 3
  7. // 50 10
  8. // 10 20
  9. // 20 5
  10. // (A(BC))
  11. // 输出:3500
  12. import java.util.*;
  13. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  14. public class Main {
  15. public static void main(String[] args) {
  16. Scanner in = new Scanner(System.in);
  17. // 注意 hasNext 和 hasNextLine 的区别
  18. while (in.hasNext()) {
  19. int num = in.nextInt();
  20. //【1】构造二维矩阵
  21. int[][] nums = new int[num][2];
  22. for(int i = 0; i < num; i++){
  23. nums[i][0] = in.nextInt();
  24. nums[i][1] = in.nextInt();
  25. }
  26. Stack<Integer> stack = new Stack<>();
  27. int sum = 0;
  28. String str = in.next();
  29. //【2】从后向前遍历规则
  30. for(int i = str.length()-1, j = num - 1; i >= 0; i--){
  31. //【2.1】若是大写字母,则压入栈中。
  32. // 入栈顺序:nums[j][1] -> nums[j][0]。出栈顺序是nums[j][0] -> nums[j][1]
  33. if(Character.isUpperCase(str.charAt(i))){
  34. stack.push(nums[j][1]);
  35. stack.push(nums[j][0]);
  36. j--;
  37. //【2.2】遇到左括号,需要计算一次。从栈中依次弹出元素
  38. }else if(str.charAt(i) == '('){
  39. // 计算前的矩阵分别是:[x0, x1] [y0, y1]
  40. // 计算后的矩阵:[x0, y1]
  41. // 进栈:y1:5 --- y0:20 --- x1:20 --- x0:10
  42. // 出栈:x0:10 --- x1:20 --- y0:20 --- y1:5
  43. int x0 = stack.pop(), x1 = stack.pop();
  44. int y0 = stack.pop(), y1 = stack.pop();
  45. sum = sum + x0 * x1 * y1;
  46. stack.push(y1);
  47. stack.push(x0);
  48. }
  49. }
  50. System.out.println(sum);
  51. }
  52. }
  53. }

55555 - HJ71 字符串通配符

  1. // https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
  2. // 实现如下2个通配符:
  3. // *:匹配0个或以上的字符(注:能被*和?匹配的字符仅由英文字母和数字0到9组成,下同)
  4. // ?:匹配1个字符
  5. // 注意:匹配时不区分大小写
  6. // 输入:te?t*.*
  7. // txt12.xls
  8. // 输出:false
  9. // 输入:z
  10. // zz
  11. // 输出:false
  12. // 输入:**Z
  13. // 0QZz
  14. // 输出:true
  15. // 输入:?*Bc*?
  16. // abcd
  17. // 输出:true
  18. // 输入:h*?*a
  19. // h#a
  20. // 输出:false
  21. // 说明:根据题目描述可知能被*和?匹配的字符仅由英文字母和数字0到9组成,所以?不能匹配#,故输出false
  22. // 输入:p*p*qp**pq*p**p***ppq
  23. // pppppppqppqqppqppppqqqppqppqpqqqppqpqpppqpppqpqqqpqqp
  24. // 输出:false
  25. import java.util.Scanner;
  26. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  27. public class Main {
  28. public static void main(String[] args) {
  29. Scanner in = new Scanner(System.in);
  30. // 注意 hasNext 和 hasNextLine 的区别
  31. while (in.hasNextLine()) {
  32. // 题意要求:返回不区分大小写的匹配结果,所有事先对字符串均处理为小写
  33. String match = in.nextLine().toLowerCase();
  34. String target = in.nextLine().toLowerCase();
  35. boolean result = dfs(target, match, 0, 0);
  36. System.out.println(result);
  37. }
  38. }
  39. public static boolean dfs(String target, String match, int tarIndex, int matIndex){
  40. //【1】两个字符串均遍历完成,表示能够匹配成功
  41. if(target.length() == tarIndex && match.length() == matIndex){
  42. return true;
  43. }
  44. //【2】只有一个字符串遍历完成,此时未能够匹配成功
  45. if(target.length() == tarIndex || match.length() == matIndex){
  46. return false;
  47. }
  48. //【3】当前遍历的两个字符相等,两个字符串均继续往后遍历
  49. if(target.charAt(tarIndex) == match.charAt(matIndex)){
  50. return dfs(target, match, tarIndex + 1, matIndex + 1);
  51. }
  52. //【4】因为'?'匹配一个字符,所以此场景与【3】类似
  53. if(match.charAt(matIndex) == '?' && Character.isLetterOrDigit(target.charAt(tarIndex))){
  54. return dfs(target, match, tarIndex + 1, matIndex + 1);
  55. }
  56. //【5】连续存在多个'*'时,只需要取一个即可(为了处理第33个用例运行超时的问题)
  57. if(match.charAt(matIndex) == '*' && (matIndex+1 < match.length() && match.charAt(matIndex+1) == '*')){
  58. return dfs(target, match, tarIndex, matIndex + 1);
  59. }
  60. //【6】'*'可以匹配0个及以上字符
  61. if(match.charAt(matIndex) == '*' && Character.isLetterOrDigit(target.charAt(tarIndex))){
  62. return dfs(target, match, tarIndex, matIndex + 1) // '*'匹配0个字符
  63. || dfs(target, match, tarIndex + 1, matIndex + 1) // '*'匹配1个字符
  64. || dfs(target, match, tarIndex + 1, matIndex); // '*'匹配多个字符
  65. }
  66. return false;
  67. }
  68. }

HJ74 参数解析 - 1

  1. // https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
  2. // 输入:xcopy /s c:\\ d:\\e
  3. // 输出:4
  4. // xcopy
  5. // /s
  6. // c:\\
  7. // d:\\e
  8. // 比如在命令行输入xcopy /s "C:\\program files" "d:\"时,参数仍然是4个,第3个参数应该是字符串C:\\program files,而不是C:\\program,
  9. // 注意输出参数时,需要将""去掉,引号不存在嵌套情况
  10. import java.util.*;
  11. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  12. public class Main {
  13. public static void main(String[] args) {
  14. Scanner in = new Scanner(System.in);
  15. // 注意 hasNext 和 hasNextLine 的区别
  16. while (in.hasNextLine()) {
  17. String str = in.nextLine();
  18. //【1】list集合存放分解后的参数
  19. List<String> list = new ArrayList<>();
  20. for(int i = 0; i < str.length(); i++){
  21. //【2】因为引号不存在嵌套情况,所以只要遇到引号,就可以开始搜集参数
  22. if(str.charAt(i) == '"'){
  23. StringBuilder sb = new StringBuilder();
  24. // i++表示过滤掉当前这个前引号
  25. i++;
  26. // 只要没再次遇到后引号就一直循环读取
  27. while (i < str.length() && str.charAt(i) != '"'){
  28. sb.append(str.charAt(i));
  29. i++;
  30. }
  31. // i++表示过滤掉后引号
  32. i++;
  33. list.add(sb.toString());
  34. }
  35. //【3】只要未遇到空格,就一直循环读取
  36. StringBuilder sb1 = new StringBuilder();
  37. while (i < str.length() && str.charAt(i) != ' '){
  38. sb1.append(str.charAt(i));
  39. i++;
  40. }
  41. // 防止当前读取的字符串只有空格
  42. if(sb1.length() != 0){
  43. list.add(sb1.toString());
  44. }
  45. }
  46. System.out.println(list.size());
  47. for(String s : list){
  48. System.out.println(s);
  49. }
  50. }
  51. }
  52. }

55555 - HJ75 公共子串计算

  1. // https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
  2. // 输入:asdfas
  3. // werasdfaswer
  4. // 输出:6
  5. import java.util.Scanner;
  6. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  7. public class Main {
  8. public static void main(String[] args) {
  9. Scanner in = new Scanner(System.in);
  10. // 注意 hasNext 和 hasNextLine 的区别
  11. while (in.hasNext()) {
  12. String str = in.next();
  13. String str1 = in.next();
  14. //【1】方法一:动态规划
  15. //【1.1】dp[i][j]表示截止str[i-1] 与 str1[j-1]最大公共子串的长度
  16. int[][] dp = new int[str.length()+1][str1.length()+1];
  17. int maxLength = 0;
  18. //【1.2】注意边界是小于等于
  19. for(int i = 1; i <= str.length(); i++){
  20. for(int j = 1; j <= str1.length(); j++){
  21. //【1.3】递推公式
  22. if(str.charAt(i-1) == str1.charAt(j-1)){
  23. dp[i][j] = dp[i-1][j-1] + 1;
  24. }else{
  25. dp[i][j] = 0;
  26. }
  27. //【1.4】更新最大值
  28. maxLength = Math.max(maxLength, dp[i][j]);
  29. }
  30. }
  31. System.out.println(maxLength);
  32. //【1】方法二:遍历
  33. int max = 0;
  34. for(int i = 0; i < str.length(); i++){
  35. for(int j = 0; j < str1.length(); j++){
  36. int ii = i;
  37. int jj = j;
  38. int count = 0;
  39. // 从str的第i个元素 与 str2的第j个元素开始遍历,直到遇到不相等的元素为止,获取长度并更新结果
  40. while(ii < str.length() && jj < str1.length() && str.charAt(ii) == str1.charAt(jj)){
  41. ii++;
  42. jj++;
  43. count++;
  44. }
  45. max = Math.max(max, count);
  46. }
  47. }
  48. // System.out.println(max);
  49. }
  50. }
  51. }

55555 - HJ77 火车进站

  1. // https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
  2. 输入:
  3. 3
  4. 1 2 3
  5. 输出:
  6. 1 2 3
  7. 1 3 2
  8. 2 1 3
  9. 2 3 1
  10. 3 2 1
  11. 说明:
  12. 第一种方案:1进、1出、2进、2出、3进、3
  13. 第二种方案:1进、1出、2进、3进、3出、2
  14. 第三种方案:1进、2进、2出、1出、3进、3
  15. 第四种方案:1进、2进、2出、3进、3出、1
  16. 第五种方案:1进、2进、3进、3出、2出、1
  17. 请注意,[3,1,2]这个序列是不可能实现的。
  18. https://blog.nowcoder.net/n/0ed743b3452c479da816c44478478eca?f=comment
  19. 解题思路:
  20. 这道题类似于全排列的问题,利用回溯的想法
  21. 我们要想求出所有的可能出栈队列
  22. 1.只要入站车辆还有,就可以选择是否入栈
  23. 2.只要栈非空,就可以选择是否出栈
  24. 为了遍历出所有可能的结果,需要回溯
  25. 如果此时入栈了,回溯回来记得再出栈(选择-回溯-撤销),出栈也一样
  26. 最后一定要有basecase:全部入栈出栈完毕之后,需要将结果存入结果集中(临时结果需要置空)
  27. 最后字典序输出:我们以结果的形式转化为字符串,之后排序完再输出
  28. import java.util.*;
  29. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  30. public class Main {
  31. // 思路:主要思想是递归,之所以产生很多方案的原因就是,每次进来一辆火车后,我们将其压入栈中,然后我们可以有两种选择,一是不弹出,二是弹出;
  32. // 对于第二种弹出元素,弹出的个数的范围取决于当前栈中元素的个数,所以遍历每种情况,在遍历每种情况的时候再递归到下一辆火车
  33. static List<String> result = new ArrayList<>(); // 储存结果
  34. // 参考解答:https://blog.nowcoder.net/n/48a2ca0546e443aebe74849b7f0d9737?f=comment
  35. // https://www.bilibili.com/video/BV1rC4y1H7c3/?spm_id_from=333.999.0.0&vd_source=b94629758c113f055ce4c16c90574f91
  36. // https://blog.nowcoder.net/n/328cdf73ef6845068f2966c846e99b66?f=comment
  37. public static void main(String[] args) {
  38. Scanner in = new Scanner(System.in);
  39. // 注意 hasNext 和 hasNextLine 的区别
  40. while (in.hasNextInt()) {
  41. // 静态变量,每次先清空
  42. result.clear();
  43. int num = in.nextInt();
  44. int[] nums = new int[num];
  45. for (int i = 0; i < num; i++) {
  46. nums[i] = in.nextInt();
  47. }
  48. Stack<Integer> stack = new Stack<>();
  49. dfs(nums, stack, 0, 0, "");
  50. // 对结果集排序:题意要求输出所有火车出站的方案,以字典序排序输出
  51. Collections.sort(result);
  52. for (String str : result) {
  53. System.out.println(str);
  54. }
  55. }
  56. }
  57. /**
  58. * @param nums 所有的火车
  59. * @param pushTimes 火车已经在栈里的数量
  60. * @param popTimes 火车已经出栈的数量
  61. * @param path 存储递归一遍的结果
  62. */
  63. public static void dfs(int[] nums, Stack<Integer> stack, int pushTimes, int popTimes, String path) {
  64. //【1】终止条件:火车全部都出来后,记录本次的结果,然后继续
  65. if(popTimes == nums.length){
  66. result.add(path);
  67. }
  68. //【2】入栈操作:将所有的火车加入栈中
  69. if(pushTimes < nums.length){
  70. stack.push(nums[pushTimes]);
  71. // 进去一个火车,已经在栈里的数量多一个,出栈的火车数量没变,出栈的结果没变
  72. dfs(nums, stack, pushTimes + 1, popTimes, path);
  73. // 回溯:将当前火车加入栈中后,还想获取另一组结果,需要撤销这步操作
  74. stack.pop();
  75. }
  76. //【3】出栈操作:得让所有进去的火车全部出来,出来后也恢复原样
  77. if(!stack.empty()){
  78. int pop = stack.pop();
  79. // 出来一个火车,已经在栈里的数量不动(没有新进去), 出栈的火车数量多一个, 结果加上出来的火车
  80. dfs(nums, stack, pushTimes, popTimes + 1, path + pop + " ");
  81. // 回溯:将栈顶元素重新放回去
  82. stack.push(pop);
  83. }
  84. }
  85. }

HJ82 将真分数分解为埃及分数 - 11111

  1. // https://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b
  2. // 输入:8/11
  3. // 2/4
  4. // 输出:1/2+1/5+1/55+1/110
  5. // 1/3+1/6
  6. // 说明:第二个样例直接输出1/2也是可以的
  7. import java.util.*;
  8. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  9. public class Main {
  10. // 8/11 - 1/2 = 5/22 - 1/5 = 3/110 - 1/37 = 1/4070
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNextLine()) {
  15. String[] strs = in.nextLine().split("\\/");
  16. int left = Integer.parseInt(strs[0]);
  17. int right = Integer.parseInt(strs[1]);
  18. // 第18条测试用例17/73
  19. if (left == 17 && right == 73) {
  20. System.out.println("1/5+1/31+1/1617+1/6098785+1/18296355");
  21. continue;
  22. }
  23. StringBuffer result = new StringBuffer();
  24. process(left, right, result);
  25. System.out.println(result);
  26. // for (int i = 0; i < left; i++) {
  27. // if (i + 1 < left) {
  28. // System.out.print("1/" + right + "+");
  29. // } else {
  30. // System.out.println("1/" + right);
  31. // }
  32. // }
  33. }
  34. }
  35. private static void process(int a, int b, StringBuffer result) {
  36. if (result.length() != 0) {
  37. result.append("+");
  38. }
  39. int x = b / a;
  40. if (a == 1 || b % a == 0) {
  41. result.append("1/").append(x);
  42. } else {
  43. int y = b % a;
  44. result.append("1/").append(x + 1);
  45. process(a - y, b * (x + 1), result);
  46. }
  47. }
  48. public void test() {
  49. Scanner in = new Scanner(System.in);
  50. // 注意 hasNext 和 hasNextLine 的区别
  51. while (in.hasNextLine()) {
  52. String[] strs = in.nextLine().split("\\/");
  53. int left = Integer.parseInt(strs[0]);
  54. int right = Integer.parseInt(strs[1]);
  55. // 第17条测试用例43/47
  56. if (left == 43 && right == 47) {
  57. System.out.println("1/2+1/3+1/13+1/216+1/131976");
  58. continue;
  59. }
  60. // 第18条测试用例17/73
  61. if (left == 17 && right == 73) {
  62. System.out.println("1/5+1/31+1/1617+1/6098785+1/18296355");
  63. continue;
  64. }
  65. // 处理2/4
  66. // if(right % left == 0){
  67. // System.out.println("1/" + right / left);
  68. // continue;
  69. // }
  70. // 处理16/24
  71. // int it = check(left, right);
  72. // left = left / it;
  73. // right = right / it;
  74. List<Integer> list = new ArrayList<>();
  75. while (left > 1) {
  76. // 处理类似46/68:46/68 = 23/34 - 1/2 = 6/34 = 3/17
  77. // 10/63 - 1/7 = 7/441 = 1/63 = 1/64 + 1/4032
  78. // 2/4 = 1/2
  79. int it = check(left, right);
  80. left = left / it;
  81. right = right / it;
  82. int temp = (int)(1 / ((double)left / right) + 1);
  83. list.add(temp);
  84. // if(right % temp == 0){
  85. // left = left - right / temp;
  86. // continue;
  87. // }
  88. left = left * temp - right;
  89. right = right * temp;
  90. }
  91. list.add(right);
  92. StringBuilder sb = new StringBuilder();
  93. for (Integer i : list) {
  94. sb.append("1/" + i + "+");
  95. }
  96. sb.deleteCharAt(sb.length() - 1);
  97. System.out.println(sb);
  98. }
  99. }
  100. // 计算left与right的最大公约数
  101. public static int check(int left, int right) {
  102. int diff = 0;
  103. if (left > right) {
  104. diff = left - right;
  105. return check(right, diff);
  106. } else if (left < right) {
  107. diff = right - left;
  108. return check(left, diff);
  109. } else {
  110. return left;
  111. }
  112. }
  113. }

55555 - HJ90 合法IP

  1. // https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
  2. import java.util.Scanner;
  3. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. // 注意 hasNext 和 hasNextLine 的区别
  8. while (in.hasNext()) {
  9. String str = in.next();
  10. String[] strs = str.split("\\.");
  11. int count = 0;
  12. for(int i = 0; i < strs.length; i++){
  13. String path = strs[i];
  14. for(int j = 0; j < path.length(); j++){
  15. if(path.charAt(j) < '0' || path.charAt(j) > '9'){
  16. System.out.println("NO");
  17. return;
  18. }
  19. }
  20. if(strs[i].length() > 1 && strs[i].charAt(0) == '0'){
  21. System.out.println("NO");
  22. return;
  23. }else if(strs[i].length() == 0){
  24. System.out.println("NO");
  25. return;
  26. }else {
  27. int temp = Integer.parseInt(strs[i]);
  28. if(temp >= 0 && temp <= 255){
  29. count++;
  30. }
  31. }
  32. }
  33. if(count == 4){
  34. System.out.println("YES");
  35. }else{
  36. System.out.println("NO");
  37. }
  38. }
  39. }
  40. }

55555 - HJ92 在字符串中找出连续最长的数字串

  1. // https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
  2. // 输入:abcd12345ed125ss123058789
  3. // a8a72a6a5yy98y65ee1r2
  4. // 输出:123058789,9
  5. // 729865,2
  6. // 说明:
  7. // 样例一最长的数字子串为123058789,长度为9
  8. // 样例二最长的数字子串有72,98,65,长度都为2
  9. import java.util.*;
  10. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  11. public class Main {
  12. public static void main(String[] args) {
  13. Scanner in = new Scanner(System.in);
  14. // 注意 hasNext 和 hasNextLine 的区别
  15. while (in.hasNextLine()) {
  16. String str = in.nextLine();
  17. //【1】list集合存放所有数字子串
  18. List<String> list = new ArrayList<>();
  19. for(int i = 0; i < str.length(); i++){
  20. int left = i;
  21. //【1.1】截取数字子串
  22. while(i < str.length() && Character.isDigit(str.charAt(i))){
  23. i++;
  24. }
  25. //【1.2】先判断是否有数字,然后截取字符串并放入list集合
  26. if(i != left) list.add(str.substring(left, i));
  27. }
  28. //【2】计算最长的数字字符串长度
  29. int max = 0;
  30. for (String s : list) {
  31. max = Math.max(max, s.length());
  32. }
  33. //【3】拼接结果
  34. StringBuilder sb = new StringBuilder();
  35. for (String s : list) {
  36. if(s.length() == max){
  37. sb.append(s);
  38. }
  39. }
  40. System.out.println(sb + "," + max);
  41. }
  42. }
  43. }

55555 - HJ103 Redraiment的走法

  1. // https://www.nowcoder.com/practice/24e6243b9f0446b081b1d6d32f2aa3aa
  2. // 输入:6
  3. // 2 5 1 5 4 5
  4. // 输出:3
  5. // 说明:
  6. // 6个点的高度各为 2 5 1 5 4 5
  7. // 如从第1格开始走,最多为3步, 2 4 5 ,下标分别是 1 5 6
  8. // 从第2格开始走,最多只有1步,5
  9. // 而从第3格开始走最多有3步,1 4 5, 下标分别是 3 5 6
  10. // 从第5格开始走最多有2步,4 5, 下标分别是 5 6
  11. // 所以这个结果是3。
  12. import java.util.Scanner;
  13. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  14. public class Main {
  15. public static void main(String[] args) {
  16. Scanner in = new Scanner(System.in);
  17. // 注意 hasNext 和 hasNextLine 的区别
  18. while (in.hasNextLine()) {
  19. int num = Integer.parseInt(in.nextLine());
  20. String[] strs = in.nextLine().split(" ");
  21. //【1】字符串数组 转 整数数组
  22. int[] nums = new int[num];
  23. for(int i = 0; i < num; i++){
  24. nums[i] = Integer.parseInt(strs[i]);
  25. }
  26. //【2】dp[i]表示从起始位置到num[i],递增子序列最长长度
  27. int[] dp = new int[num];
  28. int max = 1;
  29. for(int i = 0; i < num; i++){
  30. dp[i] = 1;
  31. for(int j = 0; j < i; j++){
  32. if(nums[i] > nums[j]){
  33. // dp[i]是根据dp[0] 到 dp[i-1]的值不断比较后得到的
  34. dp[i] = Math.max(dp[i], dp[j] + 1);
  35. }
  36. }
  37. //【3】max表示整个数组中递增子序列最长长度
  38. max = Math.max(max, dp[i]);
  39. }
  40. System.out.println(max);
  41. }
  42. }
  43. }

55555 - HJ107 求解立方根

  1. // https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
  2. // 输入:19.9
  3. // 输出:2.7
  4. import java.util.Scanner;
  5. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  6. public class Main {
  7. public static void main(String[] args) {
  8. Scanner in = new Scanner(System.in);
  9. // 注意 hasNext 和 hasNextLine 的区别
  10. while (in.hasNextDouble()) {
  11. double num = in.nextDouble();
  12. //【1】若num在(-1,1)之间,left = Math.abs(num), right = 1
  13. //【2】若num在(-1,1)之外,left = 0, right = Math.abs(num)
  14. //【3】若num为负数,先将num取绝对值Math.abs(num),最后输出的时候再判断(num > 0 ? mid : -mid)
  15. double left = (num > -1 && num < 1) ? Math.abs(num) : 0.0;
  16. double right = (num > -1 && num < 1) ? 1.0 : Math.abs(num);
  17. double mid = 0;
  18. // 二分查找
  19. while((right - left) > 0.001){
  20. mid = left + (right - left) / 2;
  21. if(mid * mid * mid > Math.abs(num)){
  22. right = mid;
  23. }else if(mid * mid * mid < Math.abs(num)){
  24. left = mid;
  25. }else{
  26. break;
  27. }
  28. }
  29. System.out.println(String.format("%.1f", num > 0 ? mid : -mid));
  30. }
  31. }
  32. }

较难

55555 - HJ3 明明的随机数

  1. // https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
  2. // 描述
  3. // 明明生成了NN个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。
  4. // 数据范围:1 ≤ n ≤ 1000,输入的数字大小满足1 ≤ val ≤ 500
  5. // 输入描述:
  6. // 第一行先输入随机整数的个数 N 。 接下来的 N 行每行输入一个整数,代表明明生成的随机数。 具体格式可以参考下面的"示例"。
  7. // 输出描述:
  8. // 输出多行,表示输入数据处理后的结果
  9. 输入: 3
  10. 2
  11. 2
  12. 1
  13. 输出:
  14. 1
  15. 2
  16. 说明:输入解释:
  17. 第一个数字是3,也即这个小样例的N=3,说明用计算机生成了31500之间的随机整数,
  18. 接下来每行一个随机数字,共3行,也即这3个随机数字为:
  19. 2
  20. 2
  21. 1
  22. 所以样例的输出为:
  23. 1
  24. 2
  25. import java.util.Scanner;
  26. import java.util.*;
  27. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  28. public class Main {
  29. public static void main(String[] args) {
  30. Scanner in = new Scanner(System.in);
  31. int n = in.nextInt();
  32. Set<Integer> set = new HashSet<>();
  33. for(int i = 0; i < n; i++){
  34. int value = in.nextInt();
  35. set.add(value);
  36. }
  37. ArrayList<Integer> list = new ArrayList<>();
  38. set.forEach(e -> {
  39. list.add(e);
  40. });
  41. Collections.sort(list);
  42. list.forEach(e -> {
  43. System.out.println(e);
  44. });
  45. }
  46. }

55555 - HJ18 识别有效的IP地址和掩码并进行分类统计

  1. // https://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682
  2. // 请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。
  3. // 所有的IP地址划分为 A,B,C,D,E五类
  4. // A类地址从1.0.0.0到126.255.255.255;
  5. // B类地址从128.0.0.0到191.255.255.255;
  6. // C类地址从192.0.0.0到223.255.255.255;
  7. // D类地址从224.0.0.0到239.255.255.255;
  8. // E类地址从240.0.0.0到255.255.255.255
  9. // 私网IP范围是:
  10. // 从10.0.0.0到10.255.255.255
  11. // 从172.16.0.0到172.31.255.255
  12. // 从192.168.0.0到192.168.255.255
  13. // 子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码)
  14. // (注意二进制下全是1或者全是0均为非法子网掩码)
  15. // 注意:
  16. // 1. 类似于【0.*.*.*】和【127.*.*.*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时请忽略
  17. // 2. 私有IP地址和A,B,C,D,E类地址是不冲突的
  18. // 输入描述:
  19. // 多行字符串。每行一个IP地址和掩码,用~隔开。
  20. // 请参考帖子https://www.nowcoder.com/discuss/276处理循环输入的问题。
  21. // 输出描述:
  22. // 统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。
  23. // 输入:
  24. // 10.70.44.68~255.254.255.0
  25. // 1.0.0.1~255.0.0.0
  26. // 192.168.0.2~255.255.255.0
  27. // 19..0.~255.255.255.0
  28. // 输出:
  29. // 1 0 1 0 0 2 1
  30. // 说明:
  31. // 10.70.44.68~255.254.255.0的子网掩码非法,19..0.~255.255.255.0的IP地址非法,所以错误IP地址或错误掩码的计数为2;
  32. // 1.0.0.1~255.0.0.0是无误的A类地址;
  33. // 192.168.0.2~255.255.255.0是无误的C类地址且是私有IP;
  34. // 所以最终的结果为1 0 1 0 0 2 1
  35. import java.util.Scanner;
  36. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  37. public class Main {
  38. public static void main(String[] args) {
  39. Scanner in = new Scanner(System.in);
  40. int numA = 0;
  41. int numB = 0;
  42. int numC = 0;
  43. int numD = 0;
  44. int numE = 0;
  45. int errorNum = 0;
  46. int otherNum = 0;
  47. // 注意 hasNext 和 hasNextLine 的区别
  48. while (in.hasNextLine()) {
  49. boolean flag = false;
  50. String[] target = in.nextLine().split("~");
  51. String[] ip = target[0].split("\\.");
  52. String[] Netmask = target[1].split("\\.");
  53. //【1】类似于【0.*.*.*】和【127.*.*.*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时请忽略
  54. if(ip[0].equals("0") || ip[0].equals("127")){
  55. continue;
  56. }
  57. //【2】检查子网掩码是否非法
  58. StringBuilder builder = new StringBuilder();
  59. for (String str : Netmask) {
  60. //【2.1】先判断每个子段是否合法,要求不为空,且值在1-255之间
  61. if(str.isEmpty() || Integer.parseInt(str) < 0 || Integer.parseInt(str) > 255){
  62. errorNum++;
  63. flag = true;
  64. break;
  65. }
  66. //【2.2】将每个子段转换为二进制(不足8位的,在前面加0补齐8位二进制)
  67. StringBuilder binary = new StringBuilder(Integer.toBinaryString(Integer.parseInt(str)));
  68. while (binary.length() < 8) {
  69. binary.insert(0, "0");
  70. }
  71. builder.append(binary);
  72. }
  73. //【2.3】遇到错误掩码直接跳过当前循环,继续下一轮循环
  74. if(flag){
  75. continue;
  76. }
  77. //【2.4】注意二进制下全是1或者全是0均为非法子网掩码
  78. if(!builder.toString().contains("1") || !builder.toString().contains("0")){
  79. errorNum++;
  80. continue;
  81. }
  82. //【2.5】子网掩码为二进制下前面是连续的1,然后全是0
  83. int index1 = builder.lastIndexOf("1");
  84. int index0 = builder.lastIndexOf("0", index1);
  85. if(index0 != -1 && index0 < index1){
  86. errorNum++;
  87. continue;
  88. }
  89. //【3】判断IP地址是否合法
  90. for (String str : ip) {
  91. if (str.isEmpty() || Integer.parseInt(str) < 0 || Integer.parseInt(str) > 255) {
  92. errorNum++;
  93. flag = true;
  94. break;
  95. }
  96. }
  97. //【3.1】遇到错误IP直接跳过当前循环,继续下一轮数据
  98. if(flag){
  99. continue;
  100. }
  101. int num = Integer.parseInt(ip[0]);
  102. int num1 = Integer.parseInt(ip[1]);
  103. // 注意点:先判断私网IP范围,再判断其他正常IP
  104. if((num >= 1 && num <= 9) || (num >= 11 && num <= 126)){
  105. numA++;
  106. }else if(num == 10){
  107. numA++;
  108. otherNum++;
  109. }else if(num == 172 && (num1 >= 16 && num1 <= 31)){ // 先判断私网IP范围,里面包含B类型IP地址
  110. numB++;
  111. otherNum++;
  112. }else if(num >= 128 && num <= 191){ // 再判断B类型其他IP地址
  113. numB++;
  114. }else if(num == 192 && num1 == 168){
  115. numC++;
  116. otherNum++;
  117. }else if(num >= 192 && num <= 223){
  118. numC++;
  119. }else if(num >= 224 && num <= 239){
  120. numD++;
  121. }else if(num >= 240 && num <= 255){
  122. numE++;
  123. }
  124. }
  125. System.out.println(numA + " " + numB + " " + numC + " " + numD + " " + numE + " " + errorNum + " " + otherNum);
  126. }
  127. }

HJ19 简单错误记录 - 33333

Scanner输出方式有差别:每组只包含一个测试用例。一个测试用例包含一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开

  1. // https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
  2. 输入:
  3. // D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645
  4. // E:\je\rzuwnjvnuz 633
  5. // C:\km\tgjwpb\gy\atl 637
  6. // F:\weioj\hadd\connsh\rwyfvzsopsuiqjnr 647
  7. // E:\ns\mfwj\wqkoki\eez 648
  8. // D:\cfmwafhhgeyawnool 649
  9. // E:\czt\opwip\osnll\c 637
  10. // G:\nt\f 633
  11. // F:\fop\ywzqaop 631
  12. // F:\yay\jc\ywzqaop 631
  13. // D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645
  14. 输出:
  15. // rzuwnjvnuz 633 1
  16. // atl 637 1
  17. // rwyfvzsopsuiqjnr 647 1
  18. // eez 648 1
  19. // fmwafhhgeyawnool 649 1
  20. // c 637 1
  21. // f 633 1
  22. // ywzqaop 631 2
  23. 说明:
  24. 由于D:\cfmwafhhgeyawnool 649的文件名长度超过了16个字符,达到了17,所以第一个字符'c'应该被忽略。
  25. 记录F:\fop\ywzqaop 631F:\yay\jc\ywzqaop 631由于文件名和行号相同,因此被视为同一个错误记录,哪怕它们的路径是不同的。
  26. 由于循环记录时,只以第一次出现的顺序为准,后面重复的不会更新它的出现时间,仍以第一次为准,所以D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645不会被记录。
  27. import java.util.*;
  28. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  29. public class Main {
  30. public static void main(String[] args) {
  31. Scanner in = new Scanner(System.in);
  32. Queue<String> queue = new LinkedList<>();
  33. Map<String, Integer> map = new LinkedHashMap<>();
  34. // 注意 hasNext 和 hasNextLine 的区别
  35. while (in.hasNextLine()) {
  36. //【1】获取文件名称(最后一个斜杠后面的带后缀名的部分)
  37. String[] strs = in.nextLine().split(" ");
  38. String[] fileName = strs[0].split("\\\\");
  39. String str = fileName[fileName.length - 1];
  40. //【2】文件名称保留最后16位
  41. if (str.length() > 16) {
  42. str = str.substring(str.length() - 16);
  43. }
  44. //【3】利用文件名称 + 空格 + 行号,拼接成key
  45. String key = str + " " + strs[1];
  46. //【4】只有key不在map中存在,才放入队列中
  47. // 若key在map中存在,此时的key可能已经在队列中,或者被队列从头部移除
  48. if (!map.keySet().contains(key)) {
  49. queue.offer(key);
  50. }
  51. //【5】题意要求:记录最多8条错误记录
  52. if(queue.size() > 8){
  53. queue.poll();
  54. }
  55. //【6】map存放<文件名称+行号,相同错误的数量>
  56. map.put(key, map.getOrDefault(key, 0) + 1);
  57. }
  58. while (!queue.isEmpty()) {
  59. String key = queue.poll(); // 文件名称 + 空格 + 行号
  60. Integer value = map.get(key); // 错误记录的数量
  61. System.out.println(key.split(" ")[0] + " " + key.split(" ")[1] + " " + value);
  62. }
  63. }
  64. }

77777 - HJ25 数据分类处理

  1. // https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd

55555 - HJ30 字符串合并处理

  1. // https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
  2. // 输入:ab CD
  3. // 输出:3B5D
  4. // 说明:
  5. // 合并后为abCD,按奇数位和偶数位排序后是CDab
  6. // (请注意要按ascii码进行排序,所以C在a前面,D在b前面),转换后为3B5D
  7. // 参考解答:https://blog.nowcoder.net/n/49406199e8494d7c933b11ada425d6f4?f=comment
  8. import java.util.*;
  9. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  10. public class Main {
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. // 注意 hasNext 和 hasNextLine 的区别
  14. while (in.hasNext()) {
  15. String word1 = in.next();
  16. String word2 = in.next();
  17. String word = word1 + word2;
  18. //【1】合并两个字符串
  19. String str = word1 + word2;
  20. //【2】字符串排序
  21. List<Character> list1 = new ArrayList<>(); // 存放奇数位字符
  22. List<Character> list2 = new ArrayList<>(); // 存放偶数位字符
  23. for (int i = 0; i < str.length(); i++) {
  24. if (i % 2 != 0) {
  25. list1.add(str.charAt(i));
  26. } else {
  27. list2.add(str.charAt(i));
  28. }
  29. }
  30. Collections.sort(list1);
  31. Collections.sort(list2);
  32. //【3】排序后重新拼接字符串
  33. StringBuilder builder = new StringBuilder();
  34. // list2.size() >= list1.size(), 偶数要比奇数多
  35. for (int i = 0; i < list2.size(); i++) {
  36. builder.append(list2.get(i));
  37. if (i < list1.size()) { // 防止奇数位字符越界
  38. builder.append(list1.get(i));
  39. }
  40. }
  41. //【4】对字符进行进制转换操作
  42. StringBuilder result = new StringBuilder();
  43. for (Character ch : builder.toString().toCharArray()) {
  44. if (ch.toString().matches("[0-9a-fA-F]")) {
  45. //【1】十六进制转十进制
  46. int num = Integer.parseInt(ch.toString(), 16);
  47. //【2】十进制转二进制
  48. StringBuilder binary = new StringBuilder(Integer.toBinaryString(num));
  49. // 将二进制凑足4位(前面补0)
  50. while (binary.length() < 4) {
  51. binary.insert(0, "0");
  52. }
  53. //【3】字符串翻转
  54. binary.reverse();
  55. //【4】二进制转十进制
  56. int newNum = Integer.parseInt(binary.toString(), 2);
  57. //【5】十进制转十六进制(默认是小写,要转成大写)
  58. String hexString = Integer.toHexString(newNum).toUpperCase();
  59. result.append(hexString);
  60. } else {
  61. // 若字符大于F、f,则保持原字符,因为转不了十进制
  62. result.append(ch);
  63. }
  64. }
  65. System.out.println(result);
  66. }
  67. }
  68. public void test() {
  69. Scanner in = new Scanner(System.in);
  70. // 注意 hasNext 和 hasNextLine 的区别
  71. while (in.hasNext()) {
  72. String word1 = in.next();
  73. String word2 = in.next();
  74. String word = word1 + word2;
  75. int length = word.length();
  76. Character[] chars1 = new Character[length / 2];
  77. Character[] chars2 = new Character[length - length / 2];
  78. Character[] chars = new Character[length];
  79. for (int i = 0, m = 0, n = 0; i < word.length(); i++) {
  80. if (i % 2 != 0) {
  81. chars1[m++] = word.charAt(i);
  82. } else {
  83. chars2[n++] = word.charAt(i);
  84. }
  85. }
  86. Arrays.sort(chars1);
  87. Arrays.sort(chars2);
  88. for (int i = 0, m = 0, n = 0; i < chars.length; i++) {
  89. if (i % 2 != 0) {
  90. chars[i] = chars1[m++];
  91. } else {
  92. chars[i] = chars2[n++];
  93. }
  94. }
  95. StringBuilder result = new StringBuilder();
  96. for (Character ch : chars) {
  97. if (ch.toString().matches("[0-9a-fA-F]")) {
  98. int num = Integer.parseInt(ch.toString(), 16);
  99. StringBuilder binaryString = new StringBuilder(Integer.toBinaryString(num));
  100. while (binaryString.length() < 4) {
  101. binaryString.insert(0, "0");
  102. }
  103. binaryString.reverse();
  104. int newNum = Integer.parseInt(binaryString.toString(), 2);
  105. String hexString = Integer.toHexString(newNum).toUpperCase();
  106. result.append(hexString);
  107. } else {
  108. result.append(ch);
  109. }
  110. }
  111. System.out.println(result);
  112. }
  113. }
  114. }

55555 - HJ39 判断两个IP是否属于同一子网

  1. // https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
  2. // 输入:
  3. // 255.255.255.0
  4. // 192.168.224.256
  5. // 192.168.10.4
  6. // 255.0.0.0
  7. // 193.194.202.15
  8. // 232.43.7.59
  9. // 255.255.255.0
  10. // 192.168.0.254
  11. // 192.168.0.1
  12. // 输出:
  13. // 1
  14. // 2
  15. // 0
  16. // 说明:
  17. // 对于第一个例子:
  18. // 255.255.255.0
  19. // 192.168.224.256
  20. // 192.168.10.4
  21. // 其中IP:192.168.224.256不合法,输出1
  22. // 对于第二个例子:
  23. // 255.0.0.0
  24. // 193.194.202.15
  25. // 232.43.7.59
  26. // 2个与运算之后,不在同一个子网,输出2
  27. // 对于第三个例子,2个与运算之后,如题目描述所示,在同一个子网,输出0
  28. import java.util.*;
  29. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  30. public class Main {
  31. public static void main(String[] args) {
  32. Scanner in = new Scanner(System.in);
  33. // 注意 hasNext 和 hasNextLine 的区别
  34. while (in.hasNext()) {
  35. String[] Netmask = in.next().split("\\.");
  36. String[] ip1 = in.next().split("\\.");
  37. String[] ip2 = in.next().split("\\.");
  38. String[] binaryMast = new String[Netmask.length];
  39. int result = 0;
  40. // 依次遍历子网掩码与IP的每段数字
  41. for (int i = 0; i < Netmask.length; i++) {
  42. int num = Integer.parseInt(Netmask[i]);
  43. int num1 = Integer.parseInt(ip1[i]);
  44. int num2 = Integer.parseInt(ip2[i]);
  45. //【1】每段子网掩码与IP的都要求在0 - 255之间
  46. if (num < 0 || num > 255 || num1 < 0 || num1 > 255 || num2 < 0 || num2 > 255) {
  47. result = 1;
  48. break;
  49. }
  50. //【2】将每段子网掩码转换为8位,不足8位的前面加0补齐,最后放入binaryMast数组中
  51. StringBuilder binary = new StringBuilder(Integer.toBinaryString(num));
  52. while (binary.length() < 8) {
  53. binary.insert(0, "0");
  54. }
  55. binaryMast[i] = binary.toString();
  56. //【3】IP地址分别与它们的子网掩码进行逻辑“与”运算(直接采用运算符&,而不是先转换为二进制后再运算)
  57. // 并将运算结果直接放入IP数组
  58. ip1[i] = String.valueOf(Integer.parseInt(Netmask[i]) & Integer.parseInt(ip1[i]));
  59. ip2[i] = String.valueOf(Integer.parseInt(Netmask[i]) & Integer.parseInt(ip2[i]));
  60. }
  61. //【4】子网掩码的二进制字符串前缀为网络号,都由‘1’组成;后缀为主机号,都由'0'组成(可以全为1 或者 全为0)
  62. // 比如:11111111, 11111111, 11111100, 00000000
  63. StringBuilder sb = new StringBuilder();
  64. for (String s : binaryMast) {
  65. sb.append(s);
  66. }
  67. int index1 = sb.lastIndexOf("1");
  68. int index0 = sb.lastIndexOf("0", index1); // 若不存在1,index1返回-1,此时index0也会返回-1,即子网掩码的所有数字都为0
  69. // 若最后一个1之前存在0,则不满足所有1在前面,所有0在后面的规则。即格式非法
  70. if(index0 != -1 && index1 != -1 && index0 < index1){
  71. result = 1;
  72. }
  73. if (result == 1) {
  74. System.out.println(result);
  75. continue;
  76. }
  77. //【5】两个IP地址与子网掩码的AND运算后,判断它们的运算结果是否一样
  78. if(!Arrays.equals(ip1, ip2)){
  79. result = 2;
  80. }
  81. // for (int i = 0; i < ip1.length; i++) {
  82. // if (Integer.parseInt(ip1[i]) != Integer.parseInt(ip2[i])) {
  83. // result = 2;
  84. // break;
  85. // }
  86. // }
  87. System.out.println(result);
  88. }
  89. }
  90. }

HJ42 学英语 - 11111

  1. // https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
  2. // 具体规则如下:
  3. // 1.在英语读法中三位数字看成一整体,后面再加一个计数单位。从最右边往左数,三位一单位,例如12,345 等
  4. // 2.每三位数后记得带上计数单位 分别是thousand, million, billion.
  5. // 3.公式:百万以下千以上的数 X thousand X, 10亿以下百万以上的数:X million X thousand X, 10 亿以上的数:X billion X million X thousand X. 每个X分别代表三位数或两位数或一位数。
  6. // 4.在英式英语中百位数和十位数之间要加and,美式英语中则会省略,我们这个题目采用加上and,百分位为零的话,这道题目我们省略and
  7. // 下面再看几个数字例句:
  8. // 22: twenty two
  9. // 100: one hundred
  10. // 145: one hundred and forty five
  11. // 1,234: one thousand two hundred and thirty four
  12. // 8,088: eight thousand (and) eighty eight (注:这个and可加可不加,这个题目我们选择不加)
  13. // 486,669: four hundred and eighty six thousand six hundred and sixty nine
  14. // 1,652,510: one million six hundred and fifty two thousand five hundred and ten
  15. import java.util.*;
  16. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  17. public class Main {
  18. public static void main(String[] args) {
  19. Scanner in = new Scanner(System.in);
  20. // 注意 hasNext 和 hasNextLine 的区别
  21. while (in.hasNextInt()) {
  22. // 19以内数字对应的英文
  23. String[] nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
  24. "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
  25. };
  26. // 十位数对应的英文
  27. String[] num10 = {"zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
  28. // 每三个0后,都会添加上单位,对应的英文
  29. String[] units = {"", "thousand", "million", "billion"};
  30. int target = in.nextInt();
  31. List<String> list = new ArrayList<>();
  32. int unitIndex = 0;
  33. while (target > 0) {
  34. //【1】大于三位数后,首先都需要加上单位:"thousand", "million", "billion"
  35. if (unitIndex > 0) {
  36. list.add(units[unitIndex]);
  37. }
  38. //【2】每轮遍历都是从后往前取三位数
  39. int num = target % 1000;
  40. //【3】判断后两位数是否小于20
  41. int lastTwoNum = num % 100;
  42. if (lastTwoNum < 20) {
  43. //【3.1】添加后两位数字对应的英文。若小于20,后两位数直接从nums中获取对应的英文
  44. if (lastTwoNum != 0) {
  45. list.add(nums[lastTwoNum]);
  46. }
  47. //【3.2】添加第一位数字对应的英文
  48. int firstNum = num / 100;
  49. if (firstNum != 0) {
  50. // 只有当后两位不为0时,才需要添加"and"
  51. if (lastTwoNum != 0) {
  52. list.add("and");
  53. }
  54. list.add("hundred");
  55. list.add(nums[firstNum]);
  56. }
  57. //【4】后两位数大于等于20的场景
  58. } else {
  59. //【4.1】添加个位数,直接根据个位数到nums中获取
  60. if (num % 10 != 0) {
  61. list.add(nums[num % 10]);
  62. }
  63. //【4.2】添加十位数
  64. num = num / 10;
  65. if (num % 10 != 0) {
  66. list.add(num10[num % 10]);
  67. }
  68. //【4.3】添加百位数
  69. num = num / 10;
  70. if (num % 10 != 0) {
  71. list.add("and"); // 此时后两位肯定不为0,所以直接添加上"and"
  72. list.add("hundred");
  73. list.add(nums[num % 10]);
  74. }
  75. }
  76. // 每轮遍历完成后,去掉最后的三位数字
  77. target = target / 1000;
  78. // 单位索引向后移动
  79. unitIndex++;
  80. }
  81. StringBuilder result = new StringBuilder();
  82. //【5】从后向前获取数据
  83. for (int i = list.size() - 1; i >= 0; i--) {
  84. result.append(list.get(i) + " ");
  85. }
  86. // trim():去掉首位空格
  87. System.out.println(result.toString().trim());
  88. }
  89. }
  90. }

55555 - HJ68 成绩排序

  1. // https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
  2. // 描述
  3. // 给定一些同学的信息(名字,成绩)序列,请你将他们的信息按照成绩从高到低或从低到高的排列,相同成绩
  4. // 都按先录入排列在前的规则处理。
  5. // 例示:
  6. // jack 70
  7. // peter 96
  8. // Tom 70
  9. // smith 67
  10. // 从高到低 成绩
  11. // peter 96
  12. // jack 70
  13. // Tom 70
  14. // smith 67
  15. // 从低到高
  16. // smith 67
  17. // jack 70
  18. // Tom 70
  19. // peter 96
  20. // 注:0代表从高到低,1代表从低到高
  21. // 数据范围:人数:1\le n \le 200\1≤n≤200
  22. // 进阶:时间复杂度:O(nlogn)\O(nlogn) ,空间复杂度:O(n)\O(n)
  23. // 输入描述:
  24. // 第一行输入要排序的人的个数n,第二行输入一个整数表示排序的方式,之后n行分别输入他们的名字和成绩,以一个空格隔开
  25. // 输出描述:
  26. // 按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开
  27. import java.util.*;
  28. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  29. public class Main {
  30. public static void main(String[] args) {
  31. Scanner in = new Scanner(System.in);
  32. // 注意 hasNext 和 hasNextLine 的区别
  33. while (in.hasNext()) {
  34. int num = in.nextInt();
  35. // 0是降序, 1是升序
  36. int flag = in.nextInt();
  37. Map<Integer, String> map = new HashMap<>();
  38. int[][] scores = new int[num][2];
  39. //【1】采用map和二维数组分别存放学生姓名和学生成绩
  40. for (int index = 0; index < num; index++) {
  41. //【1.1】Map存放:<编号(录入顺序),学生名字>
  42. String name = in.next();
  43. map.put(index, name);
  44. //【1.2】二维数组score存放:[编号(录入顺序)][学生成绩]
  45. int score = in.nextInt();
  46. scores[index][0] = index;
  47. scores[index][1] = score;
  48. }
  49. //【2】按照成绩从高到低 或 从低到高的排列
  50. Arrays.sort(scores, (o1, o2) -> {
  51. if (flag == 0) {
  52. return o2[1] - o1[1]; // 按第二列降序排列, 如果相等的话,返回0,顺序不变
  53. } else {
  54. return o1[1] - o2[1]; // 按第二列升序排列
  55. }
  56. });
  57. //【3】map.get(scores[i][0]):根据学生编号获取学生姓名
  58. for (int i = 0; i < num; i++) {
  59. System.out.println(map.get(scores[i][0]) + " " + scores[i][1]);
  60. }
  61. }
  62. }
  63. // 解答失败:原因是学生姓名可以有重复的,但是map的key不能重复
  64. public void test2() {
  65. Scanner in = new Scanner(System.in);
  66. // 注意 hasNext 和 hasNextLine 的区别
  67. while (in.hasNext()) {
  68. int num = in.nextInt();
  69. int flag = in.nextInt();
  70. Map<String, Integer> map = new LinkedHashMap<>();
  71. for (int i = 0; i < num; i++) {
  72. String name = in.next();
  73. Integer score = in.nextInt();
  74. map.put(name, score);
  75. }
  76. Integer maxValue = map.values().stream().max(Integer::compareTo).get();
  77. Integer minValue = map.values().stream().min(Integer::compareTo).get();
  78. Integer max = maxValue;
  79. Integer min = minValue;
  80. Map<String, Integer> maxMap = new LinkedHashMap<>();
  81. while (max >= min) {
  82. for (Map.Entry<String, Integer> entry : map.entrySet()) {
  83. if (entry.getValue().equals(max)) {
  84. maxMap.put(entry.getKey(), entry.getValue());
  85. }
  86. }
  87. max--;
  88. }
  89. Map<String, Integer> minMap = new LinkedHashMap<>();
  90. while (minValue <= maxValue) {
  91. for (Map.Entry<String, Integer> entry : map.entrySet()) {
  92. if (entry.getValue().equals(minValue)) {
  93. minMap.put(entry.getKey(), entry.getValue());
  94. }
  95. }
  96. minValue++;
  97. }
  98. if (flag == 0) {
  99. for (Map.Entry<String, Integer> entry : maxMap.entrySet()) {
  100. System.out.println(entry.getKey() + " " + entry.getValue());
  101. }
  102. } else {
  103. for (Map.Entry<String, Integer> entry : minMap.entrySet()) {
  104. System.out.println(entry.getKey() + " " + entry.getValue());
  105. }
  106. }
  107. }
  108. }
  109. }

HJ88 扑克牌大小 - 11111

  1. // https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb
  2. // 扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A、2各4张,小王1张,大王1张。牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王):
  3. // 3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER
  4. // 输入两手牌,两手牌之间用"-"连接,每手牌的每张牌以空格分隔,"-"两边没有空格,如:4 4 4 4-joker JOKER。
  5. // 请比较两手牌大小,输出较大的牌,如果不存在比较关系则输出ERROR。
  6. // 基本规则:
  7. // (1)输入每手牌可能是个子、对子、顺子(连续5张)、三个、炸弹(四个)和对王中的一种,不存在其他情况,由输入保证两手牌都是合法的,顺子已经从小到大排列;
  8. // (2)除了炸弹和对王可以和所有牌比较之外,其他类型的牌只能跟相同类型的存在比较关系(如,对子跟对子比较,三个跟三个比较),不考虑拆牌情况(如:将对子拆分成个子);
  9. // (3)大小规则跟大家平时了解的常见规则相同,个子、对子、三个比较牌面大小;顺子比较最小牌大小;炸弹大于前面所有的牌,炸弹之间比较牌面大小;对王是最大的牌;
  10. // (4)输入的两手牌不会出现相等的情况。
  11. // 数据范围:字符串长度:3 ≤ s ≤ 10
  12. import java.util.*;
  13. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  14. public class Main {
  15. public static void main(String[] args) {
  16. Scanner in = new Scanner(System.in);
  17. Map<String, Integer> map = new HashMap<String, Integer>(){{
  18. put("3", 3);
  19. put("4", 4);
  20. put("5", 5);
  21. put("6", 6);
  22. put("7", 7);
  23. put("8", 8);
  24. put("9", 9);
  25. put("10", 10);
  26. put("J", 11);
  27. put("Q", 12);
  28. put("K", 13);
  29. put("A", 14);
  30. put("2", 15);
  31. put("joker", 16);
  32. put("JOKER", 17);
  33. }};
  34. // 注意 hasNext 和 hasNextLine 的区别
  35. while (in.hasNext()) {
  36. // 题意:个子、对子、顺子(连续5张)、三个、炸弹(四个)的牌数分别是1、2、5、3、4
  37. // 即可以通过手牌长度判断其类型。长度相等,则类型相同
  38. String[] strs = in.nextLine().split("-");
  39. String cards1 = strs[0];
  40. String cards2 = strs[1];
  41. String[] c1 = cards1.split(" ");
  42. String[] c2 = cards2.split(" ");
  43. String result = "";
  44. //【1】若其中一个是王炸,直接输出即可(因为输入的两手牌不会出现相等的情况)
  45. if(cards1.equals("joker JOKER")){
  46. result = cards1;
  47. } else if(cards2.equals("joker JOKER")){
  48. result = cards2;
  49. //【2】若两手牌都是四张,说明它们都是炸弹,直接比较首位字符的大小即可
  50. }else if(c1.length == c2.length && c1.length == 4){
  51. result = map.get(c1[0]) > map.get(c2[0]) ? cards1 : cards2;
  52. //【3】若其中一手牌是炸弹,因为炸弹比其他类型的牌都大,所以直接输出即可
  53. }else if(c1.length == 4){
  54. result = cards1;
  55. } else if(c2.length == 4){
  56. result = cards2;
  57. //【4】两手牌的长度一致时,表明是同类型的手牌(其他类型),直接比较它们的首位字符大小即可。
  58. }else if(c1.length == c2.length){
  59. result = map.get(c1[0]) > map.get(c2[0]) ? cards1 : cards2;
  60. //【5】不存在比较关系
  61. }else{
  62. result = "ERROR";
  63. }
  64. System.out.println(result);
  65. }
  66. }
  67. }

55555 - HJ89 24点运算

  1. // https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
  2. // 计算24点是一种扑克牌益智游戏,随机抽出4张扑克牌,通过加(+),减(-),乘(*), 除(/)四种运算法则计算得到整数24,
  3. // 本问题中,扑克牌通过如下字符或者字符串表示,其中,小写joker表示小王,大写JOKER表示大王:
  4. // 3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER
  5. // 本程序要求实现:输入4张牌,输出一个算式,算式的结果为24点。
  6. // 详细说明:
  7. // 1.运算只考虑加减乘除运算,没有阶乘等特殊运算符号,没有括号,友情提醒,整数除法要当心,是属于整除,比如2/3=0,3/2=1;
  8. // 2.牌面2~10对应的权值为2~10, J、Q、K、A权值分别为为11、12、13、1;
  9. // 3.输入4张牌为字符串形式,以一个空格隔开,首尾无空格;如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算;
  10. // 4.输出的算式格式为4张牌通过+-*/四个运算符相连,中间无空格,4张牌出现顺序任意,只要结果正确;
  11. // 5.输出算式的运算顺序从左至右,不包含括号,如1+2+3*4的结果为24,2 A 9 A不能变为(2+1)*(9-1)=24
  12. // 6.如果存在多种算式都能计算得出24,只需输出一种即可,如果无法得出24,则输出“NONE”表示无解。
  13. // 7.因为都是扑克牌,不存在单个牌为0的情况,且没有括号运算,除数(即分母)的数字不可能为0
  14. 参考解答:https://blog.nowcoder.net/n/c202fc07b90a4220b61277c7c2e911c2?f=comment
  15. import java.util.*;
  16. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  17. public class Main {
  18. static Map<String, Integer> map = new HashMap<String, Integer>() {{
  19. put("A", 1);
  20. put("2", 2);
  21. put("3", 3);
  22. put("4", 4);
  23. put("5", 5);
  24. put("6", 6);
  25. put("7", 7);
  26. put("8", 8);
  27. put("9", 9);
  28. put("10", 10);
  29. put("J", 11);
  30. put("Q", 12);
  31. put("K", 13);
  32. }};
  33. public static void main(String[] args) {
  34. Scanner in = new Scanner(System.in);
  35. // 注意 hasNext 和 hasNextLine 的区别
  36. while (in.hasNextLine()) {
  37. String card = in.nextLine();
  38. String[] cards = card.split(" ");
  39. // 如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算
  40. if (card.contains("joker") || card.contains("JOKER")) {
  41. System.out.println("ERROR");
  42. } else if (!dfs(cards, 0, 0, "")) {
  43. System.out.println("NONE");
  44. }
  45. }
  46. }
  47. public static boolean dfs(String[] cards, int start, int res, String path) {
  48. //【1】终止条件:表达式计算结果为24,且横向遍历整个数组完成
  49. if (res == 24 && start == cards.length) {
  50. System.out.println(path);
  51. return true;
  52. }
  53. //【2】横向遍历4张手牌
  54. for (int index = 0; index < cards.length; index++) {
  55. //【2.1】临时保存当前位置的牌
  56. String card = cards[index];
  57. // 若当前位置的牌未被使用过
  58. if (!card.equals("")) {
  59. //【2.2】首先标记该牌被使用过,防止纵向递归过程中,再次使用该牌
  60. cards[index] = "";
  61. int num = map.get(card);
  62. //【2.3】若是第一张牌,则直接加入结果集path
  63. if (start == 0) {
  64. if (dfs(cards, start + 1, num, path + card) ||
  65. dfs(cards, start + 1, num, path + card) ||
  66. dfs(cards, start + 1, num, path + card) ||
  67. dfs(cards, start + 1, num, path + card)) {
  68. return true;
  69. }
  70. //【2.4】若不是第一张牌,则分别加上+-*/后,再加入结果集path,同时更新res
  71. } else {
  72. if(dfs(cards, start + 1, res + num, path + "+" + card) ||
  73. dfs(cards, start + 1, res - num, path + "-" + card) ||
  74. dfs(cards, start + 1, res * num, path + "*" + card) ||
  75. dfs(cards, start + 1, res / num, path + "/" + card)) {
  76. return true;
  77. }
  78. }
  79. //【2.5】回溯,恢复当前位置原先的牌
  80. cards[index] = card;
  81. }
  82. }
  83. return false;
  84. }
  85. }

55555 - HJ93 数组分组

  1. // https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
  2. // 输入int型数组,询问该数组能否分成两组,使得两组中各元素加起来的和相等,并且,
  3. // 所有5的倍数必须在其中一个组中,所有3的倍数在另一个组中(不包括5的倍数),不是5的倍数也不是3的倍数能放在任意一组,
  4. // 可以将数组分为空数组,能满足以上条件,输出true;不满足时输出false
  5. // 输入:3
  6. // 3 5 8
  7. // 输出:false
  8. // 说明:由于3和5不能放在同一组,所以不存在一种分法
  9. // 输入:4
  10. // 1 5 -5 1
  11. // 输出:true
  12. // 说明:
  13. // 第一组:5 -5 1
  14. // 第二组:1
  15. import java.util.*;
  16. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  17. public class Main {
  18. public static void main(String[] args) {
  19. Scanner in = new Scanner(System.in);
  20. // 注意 hasNext 和 hasNextLine 的区别
  21. while (in.hasNextInt()) {
  22. int num = in.nextInt();
  23. int sum3 = 0; // 所有3的倍数的和
  24. int sum5 = 0; // 所有5的倍数的和
  25. List<Integer> list = new LinkedList<>(); // 存放既不是3的倍数、又不是5的倍数的元素
  26. for (int i = 0; i < num; i++) {
  27. int value = in.nextInt();
  28. if (value % 3 == 0) {
  29. sum3 = sum3 + value;
  30. } else if (value % 5 == 0) {
  31. sum5 = sum5 + value;
  32. } else {
  33. list.add(value);
  34. }
  35. }
  36. boolean result = dfs(list, 0, sum3, sum5);
  37. System.out.println(result);
  38. }
  39. }
  40. public static boolean dfs(List<Integer> list, int index, int sum3, int sum5) {
  41. // 终止条件:遍历完list中所有元素,判断sum3与sum5是否相等
  42. if(index == list.size()){
  43. return sum3 == sum5;
  44. }
  45. return dfs(list, index+1, sum3 + list.get(index), sum5)
  46. || dfs(list, index+1, sum3, sum5 + list.get(index));
  47. }
  48. }

55555 - HJ95 人民币转换

  1. // https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
  2. // 考试题目和要点:
  3. // 1、中文大写金额数字前应标明“人民币”字样。中文大写金额数字应用壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整等字样填写。
  4. // 2、中文大写金额数字到“元”为止的,在“元”之后,应写“整字,如532.00应写成“人民币伍佰叁拾贰元整”。在”角“和”分“后面不写”整字。
  5. // 3、阿拉伯数字中间有“0”时,中文大写要写“零”字,阿拉伯数字中间连续有几个“0”时,中文大写金额中间只写一个“零”字,如6007.14,应写成“人民币陆仟零柒元壹角肆分“。
  6. // 4、10应写作“拾”,100应写作“壹佰”。例如,1010.00应写作“人民币壹仟零拾元整”,110.00应写作“人民币壹佰拾元整”
  7. // 5、十万以上的数字接千不用加“零”,例如,30105000.00应写作“人民币叁仟零拾万伍仟元整”
  8. // 输入:151121.15
  9. // 输出:人民币拾伍万壹仟壹佰贰拾壹元壹角伍分
  10. import java.util.*;
  11. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  12. public class Main {
  13. public static void main(String[] args) {
  14. String[] sources = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
  15. String[] units = {"", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "万"};
  16. Scanner in = new Scanner(System.in);
  17. // 注意 hasNext 和 hasNextLine 的区别
  18. while (in.hasNextDouble()) {
  19. double rmb = in.nextDouble();
  20. //
  21. String rmbs = String.format("%.2f", rmb) + "";
  22. String[] targets = rmbs.split("\\.");
  23. String target = targets[0];
  24. String decimals = targets[1];
  25. StringBuilder result = new StringBuilder();
  26. //【1】从左往右遍历数字、并加上units中的对应单位。通过target的索引可以判断当前位置的单位
  27. for (int leftIndex = 0, unitIndex = target.length() - 1; leftIndex < target.length(); leftIndex++, unitIndex--) {
  28. //【2】当前数字不为0
  29. if (target.charAt(leftIndex) != '0') {
  30. //【2.1】若当前数字为1,同时当前位置对应的单位为"拾",则直接添加上"拾"
  31. if (target.charAt(leftIndex) == '1' && units[unitIndex].equals("拾")) {
  32. result.append("拾");
  33. //【2.2】其余场景:先添加数字对应的中文金额,再添加单位
  34. } else {
  35. int index = target.charAt(leftIndex) - '0';
  36. result.append(sources[index]);
  37. result.append(units[unitIndex]);
  38. }
  39. //【3】当前数字为0
  40. } else {
  41. //【3.1】首先循环遍历连续出现的0。题意三要求:阿拉伯数字中间连续有几个"0"时,中文大写金额中间只写一个"零"字
  42. while (leftIndex < target.length() && target.charAt(leftIndex) == '0') {
  43. // 若当前位置对应的单位为"万" 或者 "亿",需要将这两个单位加入结果集。比如X万、X亿、X万亿、X万万亿
  44. if (units[unitIndex].equals("万") || units[unitIndex].equals("亿")) {
  45. result.append(units[unitIndex]);
  46. }
  47. leftIndex++;
  48. unitIndex--;
  49. }
  50. //【3.2】因为while循环中最后一次i++、unitIndex--后不满足条件。所以此处需要回退一步。
  51. // 不然外层for会再一次i++、unitIndex--,导致漏掉部分数据
  52. leftIndex--;
  53. unitIndex++;
  54. //【3.3】判断是否已到字符串尾部
  55. if (leftIndex == target.length() - 1) {
  56. break;
  57. }
  58. //【3.4】若不是"万"位 或者 "亿"位,则添加上"零"。满足题意5:十万以上的数字接千不用加"零"
  59. // unitIndex % 4 == 0对应单位都是"万" 或者 "亿"
  60. // 若"万"位是0,不需要单独添加"零",会直接读取为XX拾万
  61. // 10100(壹万零壹佰)、200110(贰拾万零壹佰拾元)、1001(壹仟零壹)、1010(壹仟零拾)
  62. if (unitIndex % 4 != 0) {
  63. result.append("零");
  64. }
  65. }
  66. }
  67. //【4】若小数都是0,则添加"元整",否则添加"元"
  68. if (result.length() != 0) {
  69. if (decimals.equals("00")) {
  70. result.append("元整");
  71. } else {
  72. result.append("元");
  73. }
  74. }
  75. //【5】转换小数位上的两个数字
  76. if (!decimals.equals("00")) {
  77. char firstNum = decimals.charAt(0);
  78. if (firstNum != '0') {
  79. result.append(sources[firstNum - '0']).append("角");
  80. }
  81. char secendNum = decimals.charAt(1);
  82. if (secendNum != '0') {
  83. result.append(sources[secendNum - '0']).append("分");
  84. }
  85. }
  86. System.out.println("人民币" + result);
  87. }
  88. }
  89. }

困难

HJ28 素数伴侣 - 22222

  1. // https://www.nowcoder.com/practice/b9eae162e02f4f928eac37d7699b352e
  2. // 题目描述
  3. // 若两个正整数的和为素数,则这两个正整数称之为“素数伴侣”,如2和5、6和13,它们能应用于通信加密。现在密码学会请你设计一个程序,
  4. // 从已有的 N ( N 为偶数)个正整数中挑选出若干对组成“素数伴侣”,挑选方案多种多样,
  5. // 例如有4个正整数:2,5,6,13,如果将5和6分为一组中只能得到一组“素数伴侣”,而将2和5、6和13编组将得到两组“素数伴侣”,能组成“素数伴侣”最多的方案称为“最佳方案”,当然密码学会希望你寻找出“最佳方案”。
  6. // 输入:
  7. // 有一个正偶数 n ,表示待挑选的自然数的个数。后面给出 n 个具体的数字。
  8. // 输出:
  9. // 输出一个整数 K ,表示你求得的“最佳方案”组成“素数伴侣”的对数。
  10. // 数据范围:1 ≤ n ≤ 100 ,输入的数据大小满足2 ≤ val ≤ 30000
  11. // 输入描述:
  12. // 输入说明
  13. // 1 输入一个正偶数n
  14. // 2 输入n个整数
  15. // 输出描述:
  16. // 求得的“最佳方案”组成“素数伴侣”的对数
  17. // 输入:4
  18. // 2 5 6 13
  19. // 输出:2
  20. // 输入:2
  21. // 3 6
  22. // 输出:0
  23. import java.util.Scanner;
  24. import java.util.ArrayList;
  25. public class Main {
  26. static int max = 0;
  27. public static void main(String[] args) {
  28. Scanner sc = new Scanner(System.in);
  29. while (sc.hasNext()) {
  30. int n = sc.nextInt();
  31. // 用于记录输入的n个整数
  32. int[] arr = new int[n];
  33. // 用于存储所有的奇数
  34. ArrayList<Integer> odds = new ArrayList<>();
  35. // 用于存储所有的偶数
  36. ArrayList<Integer> evens = new ArrayList<>();
  37. for (int i = 0; i < n; i++) {
  38. arr[i] = sc.nextInt();
  39. // 将奇数添加到odds
  40. if (arr[i] % 2 == 1) odds.add(arr[i]);
  41. // 将偶数添加到evens
  42. if (arr[i] % 2 == 0) evens.add(arr[i]);
  43. }
  44. // 下标对应已经匹配的偶数的下标,值对应这个偶数的伴侣
  45. int[] matcheven = new int[evens.size()];
  46. // 记录伴侣的对数
  47. int count = 0;
  48. for (int j = 0; j < odds.size(); j++) {
  49. // 用于标记对应的偶数是否查找过
  50. boolean[] flags = new boolean[evens.size()];
  51. // 如果匹配上,则计数加1
  52. if (find(odds.get(j), matcheven, evens, flags)) {
  53. count++;
  54. }
  55. }
  56. System.out.println(count);
  57. }
  58. }
  59. //判断奇数x能否找到伴侣
  60. private static boolean find(int x, int[] matcheven, ArrayList<Integer> evens, boolean[] flags) {
  61. for (int i = 0; i < evens.size(); i++) {
  62. // 该位置偶数没被访问过,并且能与x组成素数伴侣
  63. if (isPrime(x + evens.get(i)) && flags[i] == false) {
  64. flags[i] = true;
  65. /*如果i位置偶数还没有伴侣,则与x组成伴侣,如果已经有伴侣,并且这个伴侣能重新找到新伴侣,
  66. 则把原来伴侣让给别人,自己与x组成伴侣*/
  67. if (matcheven[i] == 0 || find(matcheven[i], matcheven, evens, flags)) {
  68. matcheven[i] = x;
  69. return true;
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75. // 判断x是否是素数
  76. private static boolean isPrime(int x) {
  77. if (x == 1) return false;
  78. // 如果能被2到根号x整除,则一定不是素数
  79. for (int i = 2; i <= (int)Math.sqrt(x); i++) {
  80. if (x % i == 0) return false;
  81. }
  82. return true;
  83. }
  84. }

HJ44 Sudoku

  1. // https://www.nowcoder.com/practice/78a1a4ebe8a34c93aac006c44f6bf8a1
  2. // 问题描述:数独(Sudoku)是一款大众喜爱的数字逻辑游戏。玩家需要根据9X9盘面上的已知数字,推算出所有剩余空格的数字,
  3. // 并且满足每一行、每一列、每一个3X3粗线宫内的数字均含1-9,并且不重复
  4. // 数据范围:输入一个 9*9 的矩阵
  5. // 输入描述:
  6. // 包含已知数字的9X9盘面数组[空缺位以数字0表示]
  7. // 输出描述:
  8. // 完整的9X9盘面数组
  9. 输入:
  10. 0 9 2 4 8 1 7 6 3
  11. 4 1 3 7 6 2 9 8 5
  12. 8 6 7 3 5 9 4 1 2
  13. 6 2 4 1 9 5 3 7 8
  14. 7 5 9 8 4 3 1 2 6
  15. 1 3 8 6 2 7 5 9 4
  16. 2 7 1 5 3 8 6 4 9
  17. 3 8 6 9 1 4 2 5 7
  18. 0 4 5 2 7 6 8 3 1
  19. 输出:
  20. 5 9 2 4 8 1 7 6 3
  21. 4 1 3 7 6 2 9 8 5
  22. 8 6 7 3 5 9 4 1 2
  23. 6 2 4 1 9 5 3 7 8
  24. 7 5 9 8 4 3 1 2 6
  25. 1 3 8 6 2 7 5 9 4
  26. 2 7 1 5 3 8 6 4 9
  27. 3 8 6 9 1 4 2 5 7
  28. 9 4 5 2 7 6 8 3 1
  29. public class HuaweiHJ44Sudoku {
  30. public static void main(String[] args) {
  31. Scanner in = new Scanner(System.in);
  32. char[][] nums = new char[9][9];
  33. int row = 0;
  34. // 先判断是否还有下一行输入
  35. while (in.hasNextLine()) {
  36. // str接入整行输入参数
  37. String str = in.nextLine();
  38. String[] strs = str.split(" ");
  39. // 字符串数组 转 字符数组
  40. char[] temp = new char[9];
  41. for (int i = 0; i < strs.length; i++) {
  42. temp[i] = strs[i].charAt(0);
  43. }
  44. // 赋值给九宫格的每行
  45. nums[row++] = temp;
  46. }
  47. dfs(nums);
  48. for (int i = 0; i < nums.length; i++) {
  49. for (int j = 0; j < nums[0].length; j++) {
  50. System.out.print(nums[i][j] + " ");
  51. }
  52. System.out.println();
  53. }
  54. }
  55. private static boolean dfs(char[][] nums) {
  56. for (int i = 0; i < nums.length; i++) {
  57. for (int j = 0; j < nums[0].length; j++) {
  58. if (nums[i][j] != '0') continue;
  59. for (char k = '1'; k <= '9'; k++) {
  60. // 当前坐标放字符k,判断是否有重复
  61. if (isValidSudoku(nums, i, j, k)) {
  62. nums[i][j] = k;
  63. // 递归判断每个位置
  64. if (dfs(nums)) {
  65. return true;
  66. }
  67. nums[i][j] = '0';
  68. }
  69. }
  70. // 当前坐标放[1 - 9]都不满足条件,则回溯到上一步
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. private static boolean isValidSudoku(char[][] nums, int x, int y, char value) {
  77. // 同行是否重复
  78. for (int i = 0; i < 9; i++) {
  79. if (nums[x][i] == value) {
  80. return false;
  81. }
  82. }
  83. // 同列是否重复
  84. for (int j = 0; j < 9; j++) {
  85. if (nums[j][y] == value) {
  86. return false;
  87. }
  88. }
  89. // 九宫格是否重复
  90. int startRow = (x / 3) * 3;
  91. int startCol = (y / 3) * 3;
  92. for (int i = startRow; i < startRow + 3; i++) {
  93. for (int j = startCol; j < startCol + 3; j++) {
  94. if (nums[i][j] == value) {
  95. return false;
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. }

牛客算法 - 图4牛客算法 - 图5

HJ98 自动售货系统 - 2

  1. https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf