当前位置: 首页 > 面试经验 >

7.29科大讯飞笔试-Java

优质
小牛编辑
76浏览
2023-07-29

7.29科大讯飞笔试-Java

ps:思路供大家参考,有更好的思路也欢迎评论区分享。

第一题:

思路:因为题目有条件限制,所以不用做第一个和最后一个的特殊处理,前后数组排序

public class Main01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int[] leftArr = new int[N];
        Arrays.fill(leftArr,Integer.MAX_VALUE);
        int[] rightArr = new int[N];
        Arrays.fill(rightArr,Integer.MAX_VALUE);
        // 记录企鹅所在的位置
        int index = -1;
        for (int i = 0; i < N; i++) {
            leftArr[i] = scanner.nextInt();
            if (leftArr[i] == -1) {
                index = i;
                break;
            }
        }
        leftArr[index] = Integer.MAX_VALUE;
        for (int i = 0; i < N; i++) {
            rightArr[i] = scanner.nextInt();
            if (index + i == N - 2) {
                break;
            }
        }
        Arrays.sort(leftArr);
        Arrays.sort(rightArr);
        System.out.println(leftArr[0] + rightArr[0]);
        
        scanner.close();
    }
}

第二题:

思路:主要是分不同的情况来算最小

public class Main02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int[] arr_a = new int[N];
        int[] arr_b = new int[N];
        long result = 0;
        for (int i = 0; i < N; i++) {
            arr_a[i] = scanner.nextInt();
        }
        for (int i = 0; i < N; i++) {
            arr_b[i] = scanner.nextInt();
        }
        for (int i = 0; i < N; i++) {
            int a = arr_a[i];
            int b = arr_b[i];
            if ((a >= 0 && b >= 0) || (a < 0 && b < 0)) {
                result += Math.abs((a - b));
            } else {
                result += Math.abs((a + b));
            }
        }
        System.out.println(result);
        scanner.close();
    }
}

第三题:

思路:分偶数和奇数两种情况,有许多是相同的,比如总的方案数都是n - 1,需要注意的是例如10的10/2 = 5 和 9 / 2 = 4需要做的是不同的处理,第一个是减少一个方案,第二个则需要减去两个方案,在代码中的flag做处理。

public class Main03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            // 存放Index的map
            Map<Integer,Integer> map = new HashMap<>();
            int N = scanner.nextInt();
            int[] arr = new int[N];
            for (int i = 0; i < N; i++) {
                arr[i] = scanner.nextInt();
            }
            // 禁着点
            int cant = scanner.nextInt();
            // 生成禁着点的总方案
            int total = cant - 1;
            // 去重复 
            int[] array = Arrays.stream(arr)
                    .distinct().toArray();
            // 以后用array
            Arrays.sort(array);
            // 去重复后的数组的长度
            int len = array.length;
            boolean flag = false;
            for (int i = 0; i < len; i++) {
                if (array[i] >= cant) {
                    break;
                }
                if (array[i] == cant / 2) {
                    flag = true;
                }
                if (!map.containsKey(cant - array[i])) {
                    map.put(array[i],i);
                }
            }
            total -= 2 * map.size();
            if (cant % 2 == 0 && flag) {
                total += 1;
            }
            
            System.out.println(total);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            scanner.close();
        }
    }
}

PS:选择题真的不会

#科大讯飞##科大讯飞信息集散地#
 类似资料: