首页 > 小红书8.28日后端笔试AK代码
头像
caihpipi
编辑于 2022-08-28 18:02 湖北
+ 关注

小红书8.28日后端笔试AK代码

1.
static class chess {
        int id;
        int num;
        public chess(int id, int num) {
            this.id = id;
            this.num = num;
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] str = scan.nextLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        int id = Integer.parseInt(str[2]);
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            str = scan.nextLine().split(" ");
            for (int j = 0; j < m; j++) {
                nums[i] += Integer.parseInt(str[j]);
            }
        }
        List<chess> list = new ArrayList<chess>();
        for (int i = 0; i < n; i++) {
            list.add(new chess(i, nums[i]));
        }
        list.sort(new Comparator<chess>() { @Override public int compare(chess o1, chess o2) {
                if (o1.num != o2.num) {
                    return o2.num - o1.num;
                }
                else {
                    return o1.id - o2.id;
                }
            }
        });
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).id == id - 1) {
                System.out.println(i + 1);
                break;
            }
        }

    }


2.
public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] str = scan.nextLine().split(" ");
        int n = Integer.parseInt(str[0]);
        long k = Long.parseLong(str[1]);
        str = scan.nextLine().split(" ");
        long[] nums = new long[n];
        for (int i = 0; i < n; i++) {
            nums[i] = Long.parseLong(str[i]);
        }
        Arrays.sort(nums);
        int slow = 0;
        int fast = 0;
        int res = 0;
        while (slow < n) {
            fast = slow + 1;
            while (fast < n) {
                long temp = nums[fast] * nums[slow];
                if (temp >= k) {
                    break;
                }
                fast++;
            }
            res += 2 * (n - fast);
            slow++;
        }
        System.out.println(res);
    }
3.
private static int res;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        int n = Integer.parseInt(str);
        boolean[][] nums = new boolean[n + 1][n + 1];
        String[] strs = scan.nextLine().split(" ");
        for (int i = 1; i < n; i++) {
            int num = Integer.parseInt(strs[i - 1]);
            if (i + 1 > num) {
                nums[i + 1][num] = true;
            }
            else {
                nums[num][i + 1] = true;
            }
        }
        HashSet<Integer> set = new HashSet<Integer>();
        dfs(nums, set, 0, 2);
        System.out.println(res);
    }

    private static void dfs(boolean[][] nums, HashSet<Integer> set, int sum, int index) {
        for (int i = index; i < nums.length; i++) {
            for (int j = 1; j < i; j++) {
                if (!nums[i][j]) {
                    continue;
                }
                else {
                    if (set.contains(i)) {
                        break;
                    }
                    else if (set.contains(j)) {
                        continue;
                    }
                    else {
                        set.add(i);
                        set.add(j);
                        sum++;
                        dfs(nums, set, sum, i + 1);
                        sum--;
                        set.remove(i);
                        set.remove(j);
                    }
                }
            }
        }
        res = Math.max(res, sum);
    }




全部评论

(5) 回帖
加载中...
话题 回帖