讯飞算法三题

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.useDelimiter(" ");
        int n = in.nextInt();
        String arrStr = in.next();
        String[] arr = arrStr.split(" ");
        long[] powers = new long[arr.length];
        for (int i = 0; i < n; i++) {
            powers[i] = Long.parseLong(arr[i]);
        }
        int pos = 1;
        for (int i = 1; i < n; i++) {
            if(powers[i] == -1){
                pos = i;
                break;
            }
        }
        long left = powers[0];
        for (int i = pos - 1; i >= 1; --i) {
            if(powers[i] < left){
                left = powers[i];
            }
        }
        long right = powers[pos + 1];
        for (int i = pos + 1; i < n; ++i) {
            if(powers[i] < right){
                right = powers[i];
            }
        }
        System.out.println(left + right);
    }

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.useDelimiter("\n");
        int n = in.nextInt();
        String arrStr = in.next();
        String[] arr = arrStr.split(" ");
        long[] arr1 = new long[arr.length];
        for (int i = 0; i < n; i++) {
            arr1[i] = Long.parseLong(arr[i]);
        }

        String arrStr2 = in.next();
        String[] arr_ = arrStr2.split(" ");
        long[] arr2 = new long[arr_.length];
        for (int i = 0; i < n; i++) {
            arr2[i] = Long.parseLong(arr_[i]);
        }

        long sum = 0;
        for (int i = 0; i < arr1.length; i++) {
            long top = (arr1[i] & (1 << 31));
            long bottom = (arr2[i] & (1 << 31));
            if(top != bottom){
                sum += Math.abs(arr1[i] + arr2[i]);
            }else{
                sum += Math.abs(arr1[i] - arr2[i]);
            }
        }
        System.out.print(sum);

    }
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.useDelimiter("\n");
        int n = in.nextInt();
        String arrStr = in.next();
        String[] arr = arrStr.split(" ");
        long s = in.nextLong();

        Map<Long, Boolean> record = new HashMap<>();

        for (int i = 0; i < n; i++) {
            long item = Long.parseLong(arr[i]);
            if(item < s && !record.containsKey(s - item)){
                record.put(item, true);
            }
        }
        int maxUse = record.size() * 2;
        if(record.containsKey(s / 2)){
            System.out.print(s - maxUse);
        }else{
            System.out.print(s -1 - maxUse);
        }
    }
全部评论
选择题好多是考研题,很多已经忘了
点赞 回复 分享
发布于 2023-07-29 22:03 安徽

相关推荐

耀孝女:就是你排序挂了
点赞 评论 收藏
分享
12-16 18:18
四川大学 后端
点赞 评论 收藏
分享
评论
点赞
4
分享
牛客网
牛客企业服务