分享丨ACM模式下输入模块的封装
2388
2023.11.23
2023.11.23
发布于 未知归属地

最近深耕算法,终于过了几场秋招简历初筛,兴致勃勃地准备进去大干一场,结果发现笔试模式是ACM模式,需要自己控制输入输出格式,可给平时直接在核心代码模式下练习的我给整麻了,就几个输入输出因为平时弄得少,浪费了有十多分钟,最后到写本文之前都在“初试 初筛”流程中。。。气不过的我花了点时间给封装了一个输入方法体出来,专门用于算法题的输入输出

1.思路

首先算法中常见的输入如下:

1.输入一个整数,一个数组

1.长度为n的数组:

eg.
5
1 2 3 4 5

2.一个数组,一个元素

eg.leecode35搜索插入位置
nums = [1,3,5,6], target = 5

2.n行m列的二维数组

eg.
2 3
1 2 3
4 5 6

等等诸如此类的,又考虑到常见的都是整型和字符型,所以我就仅仅分了这两种情况的判定条件,但是整个方法体用到了泛型,所以如果后续需要其他类型数据,可以自行增加

2.方法体说明

方法名:readInput(Scanner in,Class clz,int flag,int n,int m)
参数列表:

  • Scanner in:Java扫描解析对象,用于获取用户输入
  • Class clz:返回值类型,用于指定返回值类型
  • int flag:判定依据,0为获取单值,1为一维数组,2为二维数组
  • int n:一维数组中数组长度,二维数组中数组行数,用不到时设为0
  • int m:二维数组中数组列数,用不到时设为0

3.方法体及测试代码

package com.uitls;


import java.util.Scanner;

/**
 * 输入模块
 */
public class inputModule {
    /**
     * 输入模块封装,用于ACM模式下的笔试题,包含了整型和字符型的单值返回、一维数组返回和二维数组返回
     */
    public static <T> T readInput(Scanner in,Class<T> clz,int flag,int n,int m){
        switch (flag) {
            case 0:
                // 单值处理逻辑
                if (Integer.class.equals(clz)) {//不能是int,因为clz.cast()强转之后是int的封装内Integer,int的话与指定的返回值类型不同
                    return clz.cast(in.nextInt());
                } else {
                    return clz.cast(in.next());
                }
            case 1:
                // 一维数组处理逻辑
                if (clz.isArray() && clz.getComponentType().equals(int.class)) {
//                    System.out.println(clz.getComponentType());
                    int[] res = new int[n];
                    for (int i = 0; i < n; i++) {
                        res[i] = in.nextInt();
                    }
                    return clz.cast(res);
                } else {
//                    System.out.println(clz.getComponentType());
                    String[] res = new String[n];
                    for (int i = 0; i < n; i++) {
                        res[i] = in.next();
                    }
                    return clz.cast(res);
                }
            case 2:
                // 二维数组处理逻辑
                if (clz.isArray() && clz.getComponentType().getComponentType().equals(int.class)) {//二维数组调用两次,因为当调用该方法的类对象是一个嵌套数组类对象时,返回嵌套数组的最内层组件类型,即class [I
                    int[][] res = new int[n][m];
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < m; j++) {
                            res[i][j] = in.nextInt();
                        }
                    }
                    return clz.cast(res);
                } else {
                    String[][] res = new String[n][m];
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < m; j++) {
                            res[i][j] = in.next();
                        }
                    }
                    return clz.cast(res);
                }
            default:
                throw new IllegalArgumentException("Invalid flag: " + flag);
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        //模拟测试1:长度为n的数组
        int n = readInput(in, Integer.class, 0, 0, 0);
        int[] arr = readInput(in, int[].class, 1, n, 0);
        System.out.println("输入的整型一维数组为:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

        //模拟测试2:长度为n的字符串
        int n = readInput(in, Integer.class, 0, 0, 0);
        String[] str = readInput(in,String[].class,1,n,0);
        System.out.println("输入的字符型一维数组为:");
        for (String s : str) {
            System.out.print(s + " ");
        }

        //模拟测试3:nxm的二维整型数组
        int n = readInput(in, Integer.class, 0, 0, 0);
        int m = readInput(in,Integer.class,0,0,0);
        int[][] arr = readInput(in, int[][].class, 2, n, m);
        System.out.println("输入的二维整型数组为:");
        for (int i = 0; i < n; i++) {
            for(int j =0;j< m ; j++){
                System.out.print(arr[i][j] + " ");
            }
        }

        //模拟测试4:nxm二维字符型数组
        int n = readInput(in, Integer.class, 0, 0, 0);
        int m = readInput(in,Integer.class,0,0,0);
        String[][] arr = readInput(in, String[][].class, 2, n, m);
        System.out.println("输入的二维字符型数组为:");
        for (int i = 0; i < n; i++) {
            for(int j =0;j< m ; j++){
                System.out.print(arr[i][j] + " ");
            }
        }

        in.close();
    }

}
评论 (7)