常见 ACM 模式输入处理(Java版)

2,162 阅读2分钟

这是我参与11月更文挑战的第5天,活动详情查看:2021最后一次更文挑战

写在前面

  • 对于非 ACM 选手,平时在 力扣牛客 刷题时很少会考虑到输入,输出的处理。习惯了核心代码模式,而公司笔试一般都会采用 ACM 模式,所以笔试的时候可能就会 gg 了。下面我会对 Java 的 Scanner 类常见的 API 进行测试分析。

1、java Scanner 类常用 API 说明

  • 注意:空格是判断是否为多个字符的标准。
  1. hasNextInt() 当下一个字符是整数时返回 true, 反之返回 false。
  • nextInt() 把字符转化为 int 类型并返回。
  1. hasNextLong() 当下一个字符是整数时返回 true, 反之返回 false。
  • nextLong() 把字符转化为 long 类型并返回。
  1. hasNextDouble() 当下一个字符是整数或是小数时返回 true, 反之返回 false。
  • nextDouble() 把字符转化为 double 类型并返回。
  1. hasNextFloat() 当下一个字符是整数或是小数时返回 true, 反之返回 false。
  • nextFloat() 把字符转化为 double 类型并返回。
  1. hasNextLine() 当下一位还有字符时返回 true, 反之返回 false。
  • nextLine() 把本行剩下的字符转化为 String 类型并返回。
  1. hasNext() 当下一位还有字符时返回 true, 反之返回 false。
  • next() 把两个空格之间的字符转化为 String 类型并返回。

2、API 测试

String s = "1 2 3 4 | 5 6 7 8 | 1.2 3.4 | 6.7 8.9 |
            hello world ! hello world ! hello world !";
            
        Scanner input = new Scanner(s);

        System.out.println("s: " + s);
        System.out.print("nextInt: ");
        while (input.hasNextInt()) {
            System.out.print(input.nextInt() + " ");
        }
        System.out.println();
        input.next();

        System.out.print("nextLong: ");
        while (input.hasNextLong()) {
            System.out.print(input.nextLong() + " ");
        }
        System.out.println();
        input.next();

        System.out.print("nextDouble: ");
        while (input.hasNextDouble()) {
            System.out.print(input.nextDouble() + " ");
        }
        System.out.println();
        input.next();

        System.out.print("nextFloat: ");
        while (input.hasNextFloat()) {
            System.out.print(input.nextFloat() + " ");
        }
        System.out.println();
        input.next();

        System.out.print("nextLine: ");
        String v5;
        while (input.hasNextLine()) {
            v5 = input.nextLine();
            System.out.print(v5 + " ");
        }
        System.out.println();

        System.out.print("next: ");
        while (input.hasNext()) {
            System.out.print(input.next() + " ");
        }

3、测试结果

屏幕截图 2021-11-27 145851.jpg

4、两个输入类型示例(参考链接

  1. 多行输入
  • 每组数据第一行两个整数n和l(n大于0小于等于1000,l小于等于1000000000大于0)。第二行有n个整数(均大于等于0小于等于l),为每盏灯的坐标,多个路灯可以在同一点。
public class ArrayInput {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            long l=sc.nextLong();
            long[] nums=new long[n];
            for(int i=0;i<n;i++){
                nums[i]=sc.nextLong();
            }
            Arrays.sort(nums);
            long gap=nums[1]-nums[0];
            for(int i=1;i<n;i++){
                gap=Math.max(gap,nums[i]-nums[i-1]);
            }
            // 下标最小和最大位置的路灯需要单独判断
            // 如下标为3是最小,那么0-3这一段必须被覆盖到,所以最小下标必须单独判断
            gap=Math.max(gap,nums[0]*2);
            gap=Math.max(gap,(l-nums[n-1])*2);
            System.out.println(String.format("%.2f",gap/2.0));
        }
    }
}
  1. 链表输入
public class LinkListInput {
    //题目描述
    //对于一个链表 L: L0→L1→…→Ln-1→Ln,
    //将其翻转成 L0→Ln→L1→Ln-1→L2→Ln-2→…

    //先构建一个节点类,用于链表构建
    static class LinkNode {
        int val;
        LinkNode next;
        public LinkNode(int val){
            this.val = val;
        }
    }

    public static void main(String[] args){
        //输入是一串数字,请将其转换成单链表格式之后,再进行操作
        //输入描述:
        //一串数字,用逗号分隔
        //输入
        //1,2,3,4,5
        Scanner scanner = new Scanner(System.in);
        //以字符串形式作为输入
        String str = scanner.next().toString();
        //通过分隔符将其转为字符串数组
        String[] arr  = str.split(",");
        //初始化一个整数数组
        int[] ints = new int[arr.length];
        //给整数数组赋值
        for(int j = 0; j<ints.length;j++) {
            ints[j] = Integer.parseInt(arr[j]);
        }
        Stack<LinkNode> stack = new Stack<>();
        LinkNode head = new LinkNode(0);
        LinkNode p = head;
        //链表初始化并放入stack中
        for(int i = 0; i < ints.length; i++){
            p.next = new LinkNode(ints[i]);
            p = p.next;
            stack.add(p);
        }
        head = head.next;
        //开始链表转换
        p = head;
        LinkNode q = stack.peek();
        while ((!p.equals(q)) && (!p.next.equals(q))) {
            q = stack.pop();
            q.next = p.next;
            p.next = q;
            p = p.next.next;
            q = stack.peek();
        }
        q.next = null;
        //输出
        //1,5,2,4,3
        //打印
        while (head != null) {
            if(head.next == null){
                System.out.print(head.val);
            }else{
                System.out.print(head.val + ",");
            }
            head = head.next;
        }
    }
}