前言

经常在 LeetCode 上用核心代码模式刷题的小伙伴突然用 ACM 模式可能会适应不过来,把时间花在输入输出上很浪费时间,因此本篇笔记对 Java 算法的 ACM 模式做了个小总结;

除此之外,需要注意一些小细节:

  • 1. 数字读取到字符串读取间需要用 in.nextLine() 换行;

1. 数字处理

  • 如果是读取 Long,则使用:in.hasNextLong() 和 Long a = in.nextLong();
  • 读取小数:f = scan.nextFloat() 或 double weight = scan.nextDouble();

1.1 多组空格分隔的两个正整数

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            //处理
        }
    }
}

1.2 第一行组数接空格分隔的两个正整数

第一行输入数据个数,后面输入数据;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            //处理
        }
    }
}

1.3 空格分隔的两个正整数为0 0 结束

输入包括两个正整数a,b(1 <= a, b <= 10^9), 如果输入为0 0则结束输入;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            if(a ==0 && b == 0) break;
            //处理
        }
    }
}

1.4 每行第一个为个数后带空格分割整数为0结束

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            if(n == 0) break;
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}


2. 字符串处理

比较项next( )nextLine( )
说明只能读取到空格之前的字符串可以读取空格的字符串
比如“你好 java”“你好”“你好 java”
使用前判断in.hasNext()in.hasNextLine()

2.1 第一行个数第二行字符串

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        while (in.hasNext()) { // 注意 while 处理多个 case
            String[] s = in.nextLine().split(" ");
            //处理
        }
    }
}

2.2 多行空格分开的字符串

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] s = in.nextLine().split(" ");
            //处理
        }
    }
}

3. 输出格式化相关

  • 输出有两种方式:String str=String.format(示例) 或 System.out.printf(示例);
  • 向上取整用:Math.ceil(1.01),向下取整用:Math.floor(1.01);

3.1 转换符

转换符说明示例输出
%s字符串“Hi,%s:%s.%s”, “王南”,“王力”,“王张”Hi,王南:王力.王张
%c字符“字母a的大写是:%c %n”, ‘A’字母a的大写是:A
%b布尔“3>7的结果是:%b %n”, 3>73>7的结果是:false
%d整数(十进制)“100的一半是:%d %n”, 100/2100的一半是:50
%x整数(十六进制)“100的16进制数是:%x %n”, 100100的16进制数是:64
%o整数(八进制)“100的8进制数是:%o %n”, 100100的8进制数是:144
%f浮点“50元的书打8.5折扣是:%f 元%n”, 50*0.8550元的书打8.5折扣是:42.500000 元
%a浮点(十六进制)“上面价格的16进制数是:%a %n”, 50*0.85上面价格的16进制数是:0x1.54p5
%e指数“上面价格的指数表示:%e %n”, 50*0.85上面价格的指数表示:4.250000e+01
%g通用浮点(f和e类型中较短的)“上面价格的指数和浮点数结果的长度较短的是:%g %n”, 50*0.85上面价格的指数和浮点数结果的长度较短的是:42.5000
%h散列码“字母A的散列码是:%h %n”, ‘A’字母A的散列码是:41
%%百分比“上面的折扣是%d%% %n”, 85上面的折扣是85%
%n换行符
%tx日期与时间

3.2 搭配转换符的标志

标志说明示例输出
.后接保留多少位小数(四舍五入)(“%.2f”,3.555)3.56
+为正数或者负数添加符号(“%+d”,15)+15
−左对齐(“%-5d”,15)|15 |
0数字前面补0(“%04d”, 99)0099
空格在整数之前添加指定数量的空格(“% 4d”, 99)| 99|
,以“,”对数字分组(“%,f”, 9999.99)9,999.990000
(使用括号包含负数(“%(f”, -99.99)(99.990000)
#如果是浮点数则包含小数点,如果是16进制或8进制则添加0x或0(“%#x”, 99) (“%#o”, 99)0x63 0143
<格式化前一个转换符所描述的参数(“%f和<3.2f”, 99.45)99.450000和99.45
$被格式化的参数索引(“%1$d,%2$s”, 99,“abc”)99,abc

4. ACM 模式模板

import java.util.*;
import java.lang.*;

public class Main {

    public static void main(String[] args) {
        //1.数据输入
        Scanner in = new Scanner(System.in);
        //读数字
        int numLen = in.nextInt();
        int[] numArr = new int[numLen];
        int i = 0;
        while(in.hasNextInt() && i < numLen){
            numArr[i] = in.nextInt();
            i++;
        }
        //读字符串
        int strLen = in.nextInt();
        in.nextLine(); //数字到字符串要换行
        String[] strArr = new String[strLen];
        //或者 strArr[] = in.nextLine().split(" ");
        int j = 0;
        while(in.hasNextLine() && j < strLen){
            strArr[j] = in.nextLine();
            j++;
        }
        
        //2. 处理
        Solution solution = new Solution();
        String result = solution.process(numArr, strArr);
        
        //3. 输出
        System.out.println(result);
        //四舍五入输出小数
        String str = String.format("%.2f",3.555);
        System.out.println(str);
    }
}

//下面类似 LeetCode 的核心代码模式
class Solution {
    public String process(int[] nums, String[] strs) {
        StringBuilder sb = new StringBuilder();
        sb.append(Arrays.toString(nums));
        sb.append(" && ");
        sb.append(Arrays.toString(strs));
        return sb.toString();
    }
}

最后

新人制作,如有错误,欢迎指出,感激不尽!
欢迎关注公众号,会分享一些更日常的东西!
如需转载,请标注出处!

更多推荐