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

讯飞算法三题

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

讯飞算法三题

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);
        }
    }
 类似资料: