面试 | Java 算法的 ACM 模式

本文详细介绍了Java在ACM模式下处理输入输出的常见方法,包括数字和字符串的读取、多组数据的处理以及输出格式化。通过示例代码展示了如何高效地读取和处理多组整数、字符串,以及使用各种转换符进行输出格式控制。此外,还提供了一个ACM模式的模板代码,方便开发者快速适应ACM编程风格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

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

最后

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多氯环己烷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值