0907携程笔试AK代码

Q1

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class P1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        List<List<Integer>> lists = new ArrayList<>();
        backtrack(lists, new boolean[n], new ArrayList<>());
        int ans = 0;
        for (List<Integer> list : lists) ans += check(list) ? 1 : 0;
        System.out.println(ans);
    }

    public static void backtrack(List<List<Integer>> lists, boolean[] used, List<Integer> list) {
        if (list.size() == used.length) lists.add(new ArrayList<>(list));
        else {
            for (int i = 1; i <= used.length; i++) {
                if (used[i - 1]) continue;
                list.add(i);
                used[i - 1] = true;
                backtrack(lists, used, list);
                list.remove(list.size() - 1);
                used[i - 1] = false;
            }
        }
    }

    public static boolean check(List<Integer> list) {
        for (int i = 1; i < list.size(); i++) {
            int sum = list.get(i) + list.get(i - 1);
            if (isPrime(sum)) return false;
        }
        return true;
    }

    public static boolean isPrime(int x) {
        if (x == 1 || x % 2 == 0 && x != 2) return false;
        for (int i = 3; i < x; i += 2) {
            if (x % i == 0) return false;
        }
        return true;
    }
}

Q2

import java.util.Scanner;

public class P2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(), m = scan.nextInt();
        char[][] grid = new char[n][], cols = new char[m][3], rows = new char[n][3];
        long ans = 0;
        String you = "you";
        for (int i = 0; i < n; i++) grid[i] = scan.next().toCharArray();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int x = you.indexOf(grid[i][j]);
                if (x == -1) continue;
                rows[i][x]++;
                cols[j][x]++;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int idx = you.indexOf(grid[i][j]);
                if (idx == -1) continue;
                long[][] array = new long[2][2];
                for (int k = 0, p = 0; k < 3; k++) {
                    if (k != idx) {
                        array[p][0] = rows[i][k];
                        array[p][1] = cols[j][k];
                        p++;
                    }
                }
                ans += array[0][0] * array[1][1] + array[1][0] * array[0][1];
            }
        }
        System.out.println(ans);
    }
}

Q3

import java.util.Scanner;

public class P3 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();
        while (t-- > 0) {
            int n = scan.nextInt(), l = scan.nextInt(), r = scan.nextInt();
            int[] array = new int[n];
            long sum = 0, sl = 0, br = 0;
            for (int i = 0; i < n; i++) {
                array[i] = scan.nextInt();
                sum += array[i];
                if (array[i] < l) sl += (l - array[i]);
                else if (array[i] > r) br += (array[i] - r);
            }
            double avg = (double) sum / n;
            if (avg > r || avg < l) System.out.println(-1);
            else System.out.println(Math.max(sl, br));
        }
    }
}

Q4

import java.util.*;

public class P4 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        char[] cs = scan.next().toCharArray();
        int cnt = 0;
        long ans = 0;
        for (char c : cs) {
            cnt += c == '0' ? 1 : -1;
            if (cnt > 0) ans += cnt;
            else cnt = 0;
        }
        System.out.println(ans);
    }
}

#携程##携程笔试#
全部评论
大佬你为何这么强,看你之前的帖子,不是各种ak就是各种oc
3 回复
分享
发布于 2023-09-07 23:11 江苏
tql
1 回复
分享
发布于 2023-09-07 21:42 江西
滴滴
校招火热招聘中
官网直投
你是我的爹
1 回复
分享
发布于 2023-09-07 21:42 湖北
tql
1 回复
分享
发布于 2023-09-07 21:59 湖北
太强了
点赞 回复
分享
发布于 2023-09-07 21:42 湖北
点赞 回复
分享
发布于 2023-09-07 21:45 上海
超人
点赞 回复
分享
发布于 2023-09-07 22:01 重庆
这思路太清晰了
点赞 回复
分享
发布于 2023-09-07 22:27 湖北
tql
点赞 回复
分享
发布于 2023-09-07 22:59 北京
狠狠收藏😻
点赞 回复
分享
发布于 2023-09-07 23:33 安徽
大佬 写一下第四题的相关思路 代码太短了 理解不了
点赞 回复
分享
发布于 2023-09-08 17:57 北京

相关推荐

头像
1h20min&nbsp;被拷打麻了一&nbsp;自我介绍二&nbsp;闲聊前端相关1&nbsp;你认为前端程序员需要具备什么能力2&nbsp;为什么学前端3&nbsp;你说你的项目还有什么产品,后端,是参加什么东西吗,怎么个组织三&nbsp;拷打项目,结合项目问问题1&nbsp;讲讲你小程序这个项目的难点2&nbsp;你说你项目实现了一个无障碍访问功能,怎么实现的,详细说说3&nbsp;现在回过头来复盘,你认为之前这个项目可以优化什么4&nbsp;项目用的uni-app,当时是考虑什么选的,什么因素促使你用uni-pp5&nbsp;知道其他跨端框架吗,你认为什么因素决定了你做项目时候的技术选型6&nbsp;知道支付宝小程序和别的什么小程序,比如微信小程序,百度小程序,美团小程序有什么相同,不同地方吗7&nbsp;讲讲小程序冷启动热启动(寄)8&nbsp;uni-app有什么好处,比起其他框架9&nbsp;uni-app条件编译怎么回事10&nbsp;说到了条件编译和打包,用过什么打包工具吗,详细说说从零开始打包是个什么流程(寄)11&nbsp;说说你熟悉的打包工具怎么配(寄,我说直接用现成的脚手架)12&nbsp;知道前端交互有什么方式吗(有点没明白问的啥,答的和后端交互的ajax有什么)13&nbsp;说说http请求里面都有什么14&nbsp;详细讲讲http请求头有什么(寄了,没背这么细)15&nbsp;跨域怎么回事,请求头里哪一项和跨域有关(寄了,只说了一个,说有4个)四&nbsp;手写代码1&nbsp;给了一个骰子六面的图,五点那个面,说说这个布局怎么实现&nbsp;&nbsp;&nbsp;&nbsp;说了好几种常见的,他跟我说有十几种,感觉寄了&nbsp;&nbsp;&nbsp;&nbsp;提了中间居中那个可以用绝对定位和translate做,被追问translate属于哪个属性&nbsp;&nbsp;&nbsp;&nbsp;想了会想起是transform,然后让说还知道这个有什么别的属性不,我刚说了两个,他让我直接在面试界面写出来&nbsp;&nbsp;&nbsp;&nbsp;牛客那个面试界面没有css的补全,那几个单词我光记得前几个字母怎么拼,一般后面都是让ide补全的,然后这就直接没写出来。。。&nbsp;&nbsp;&nbsp;&nbsp;大寄特寄2&nbsp;给了一个嵌套对象,输出拍平的对象数组。输入对象是有若干属性,可能有children属性,有的话是个数组,里面可能有多个对象想了一会,写了dfs解法。追问知不知道别的解法,用栈怎么写,一下没想出来然后说bfs呢,想了会给bfs的写出来了五&nbsp;反问这不直接寄了,这么喜欢追问,拷打我1小时多,物理意义上汗流浃背估计是最后一次面美团了,团子再见了
点赞 评论 收藏
转发
13 49 评论
分享
牛客网
牛客企业服务