Java 算法的 ACM 模式

782 阅读2分钟

前言

经常在 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. 字符串处理

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 转换符

3.2 搭配转换符的标志

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();
    }
}