阿里3.14笔试,三题JAVA代码

第一题 

16进制转2进制算1的个数,直接打卡

import java.util.HashMap;
import java.util.Scanner;

public class Q1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        HashMap<Character,Integer> map = new HashMap<>();
        map.put('0',0);
        map.put('1',1);
        map.put('2',1);
        map.put('3',2);
        map.put('4',1);
        map.put('5',2);
        map.put('6',2);
        map.put('7',3);
        map.put('8',1);
        map.put('9',2);
        map.put('a',2);
        map.put('b',3);
        map.put('c',2);
        map.put('d',3);
        map.put('e',3);
        map.put('f',4);
        int ans= 0;
        for (int i = 2; i < str.length(); i++) {
            ans+=map.get(str.charAt(i));
        }
        System.out.println(ans);
    }
}

第二题

一个nxm矩阵,有的是人,有的是灯,灯可以上下左右都看,如果能看到人就计1,问所有灯能看到的人数
思路:从左到右,从右到左,从上到下,从下到上,4nm复杂度。
import java.util.Scanner;

public class Q2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int map[][] = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                //取反
                map[i][j] = 1 - sc.nextInt();
            }
        }
        System.out.println(maxScore(map, n, m));

    }

    public static long maxScore(int[][] map, int n, int m) {
        //1是灯,0是人 上面取反了 (这步其实没必要,一开始思路没定)
        long ans = 0;
        for (int i = 0; i < n; i++) {
            //从左到右按行
            boolean havePerson = false;
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 1) {
                 //   System.out.println("正序,第" + i + "行,第" + j + "列");
                    if (havePerson) {
                        ans++;
                      //  System.out.println("成功:ans:"+ans);
                    }
                } else {
                    havePerson = true;
                }
            }
            havePerson = false;
            for (int j = m - 1; j >= 0; j--) {
                if (map[i][j] == 1) {
                  //  System.out.println("逆序,第" + i + "行,第" + j + "列");
                    if (havePerson) {
                        ans++;
                    //   System.out.println("成功:ans:"+ans);
                    }
                } else {
                    havePerson = true;
                }
            }
        }
       // System.out.println("+++++++++++++++");
        for (int i = 0; i < m; i++) {
            boolean havePerson = false;
            //从上到下
            for (int j = 0; j < n; j++) {
                if (map[j][i] == 1) {
                 //   System.out.println("正序,第" + j + "行,第" + i + "列");
                    if (havePerson) {
                        ans++;
                     //   System.out.println("成功:ans:"+ans);
                    }
                } else {
                    havePerson = true;
                }
            }
            //从下到上
            havePerson = false;
            for (int j = n - 1; j >= 0; j--) {

                if (map[j][i] == 1) {
                  //  System.out.println("序,第" + j + "行,第" + i + "列");
                    if (havePerson) {
                        ans++;
                     //   System.out.println("成功:ans:"+ans);
                    }
                } else {
                    havePerson = true;
                }
            }
        }
        return ans;

    }
}

第三题

消消乐,一个8x8地图,点一个格子,该格子连通的同样颜色的都会消掉...题目太长了 自己搜一下吧
思路:主要是模拟,先用BFS标记消除,然后再填充
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static int[] appendPoint = new int[8];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        char map[][] = new char[8][8];
        for (int i = 0; i < 8; i++) {
            String str = sc.nextLine();
            for (int j = 0; j < str.length(); j++) {
                map[i][j] = str.charAt(j);
            }
        }
        String[] appendStrs = new String[8];
        for (int i = 0; i < 8; i++) {
            appendStrs[i] = sc.nextLine();
        }
        char dir[] = new char[n];
        int x[] = new int[n];
        int y[] = new int[n];
        for (int i = 0; i < n; i++) {
            String ops = sc.nextLine();
            String strs[] = ops.split(" ");
            x[i] = Integer.parseInt(strs[0])-1;
            y[i] = Integer.parseInt(strs[1])-1;
            dir[i] = strs[2].charAt(0);
        }
        int ans[] = getDamage(map,n,appendStrs,x,y,dir);
        for (int i = 0; i < n; i++) {
            System.out.println(ans[i]);
        }

    }

    public static int[] getDamage(char[][] map, int n, String[] appendStrs, int[] x, int[] y, char[] dir) {
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            ans[i] = bfs(map,x[i],y[i]);
//.out.println("bfs消除后的map");
          //  printMap(map);
            move(map,dir[i],appendStrs);
          //  System.out.println("填充后的map");
         //   printMap(map);
        }
        return ans;
    }

    public static void move(char[][] map, char dir, String[] appendStrs) {
        // wasd 上 左 下 右
        if (dir == 'w') {
            for (int col = 0; col < 8; col++) {
                //向上移动
                int k = 0;
                for (int row = 0; row < 8; row++) {
                    if (map[row][col] != '*') {
                        map[k++][col] = map[row][col];
                    }
                }
                //填充
                //int idx = appendPoint[col];
                while (k < 8) {
                    map[k++][col] = appendStrs[col].charAt(appendPoint[col]++);
                }
            }
        } else if (dir == 'a') {
            for (int row = 0; row < 8; row++) {
                //向左移动
                int k = 0;
                for (int col = 0; col < 8; col++) {
                    if (map[row][col] != '*') {
                        map[row][k++] = map[row][col];
                    }
                }
                //向左填充
                while (k < 8) {
                    map[row][k++] = appendStrs[row].charAt(appendPoint[row]++);
                }
            }
        } else if (dir == 's') {
            for (int col = 0; col < 8; col++) {
                //向下移动
                int k = 7;
                for (int row = 7; row >= 0; row--) {
                    if (map[row][col] != '*') {
                        map[k--][col] = map[row][col];
                    }
                }
                //填充
                //int idx = appendPoint[col];
                while (k >= 0) {
                    map[k--][col] = appendStrs[col].charAt(appendPoint[col]++);
                }
            }
        } else if (dir == 'd') {
            for (int row = 0; row < 8; row++) {
                //向右移动
                int k = 7;
                for (int col = 7; col >= 0; col--) {
                    if (map[row][col] != '*') {
                        map[row][k--] = map[row][col];
                    }
                }
                //向左填充
                while (k >= 0) {
                    map[row][k--] = appendStrs[row].charAt(appendPoint[row]++);
                }
            }
        }
    }

    public static int bfs(char[][] map, int x, int y) {
        int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        int ans = 0;
        Deque<int[]> Q = new ArrayDeque<>();
        Q.add(new int[]{x, y});
        char color = map[x][y];
        map[x][y] = '*';
        while (!Q.isEmpty()) {
            int[] p = Q.poll();
//            System.out.println("当前点:"+p[0]+","+p[1]+"\ty暗色:"+color);
            ans++;
//            for (int i = 0; i < 4; i++) {
//                System.out.println("dirs"+dirs[i][0]+","+dirs[i][1]);
//            }
            for (int[] dir : dirs) {
                int newx = p[0] + dir[0];
                int newy = p[1] + dir[1];
//                System.out.println("\t考虑点:"+p[0]+","+p[1]);
                if (newx < 0 || newx >= 8 || newy < 0 || newy >= 8) continue;
//                System.out.println("\t考虑点:"+p[0]+","+p[1]+"\ty暗色:"+map[newx][newy]);
                if (map[newx][newy] == color) {
//                    System.out.println("\t加入当前点");
                    map[newx][newy] = '*';
                    Q.add(new int[]{newx, newy});
                }
            }
        }
        return ans;
    }

    public static void printMap(char[][] map){
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                System.out.print(map[i][j]);
            }
            System.out.println();
        }
    }
}


#阿里笔试##阿里巴巴##笔经#
全部评论
笔试一般用什么ide呀 有自动补全吗 可以复制自己提前准备好的模板吗
点赞 回复 分享
发布于 2023-03-08 02:35 广东
楼主好厉害,想问下楼楼是怎么在这么短时间内做出来的,之前怎么刷的算法题目呀,方便说一下吗,我看到题目基本上没有思路~哇呜
点赞 回复 分享
发布于 2022-03-15 20:39
楼主写了多少分呀,有笔试线么
点赞 回复 分享
发布于 2022-03-15 19:57
佩服
点赞 回复 分享
发布于 2022-03-14 23:59
第三题思路一样就是只过了9% 突然一想不知道 是不是可能有一种情况是填充的不够了然后选择消除的点是*?
点赞 回复 分享
发布于 2022-03-14 23:14
是研发岗的。
点赞 回复 分享
发布于 2022-03-14 22:28

相关推荐

不愿透露姓名的神秘牛友
07-11 12:31
以前小时候我最痛恨出轨、偷情的人,无论男女,为什么会出轨?现在我成了自己最讨厌的人,没想到分享的东西在牛客会被这么多人看,大家的评价都很中肯,我也认同,想过一一回复,但我还是收声了,我想我应该说说这件事,这件事一直压在我心里,是个很大的心结,上面说了人为什么出轨,我大概能明白了。我们大一下半年开始恋爱,开始恋爱,我给出了我铭记3年的承诺,我对她好一辈子,我永远不会背叛,我责任心太重,我觉得跟了我,我就要照顾她一辈子,我们在一起3年我都没有碰过她,她说往东我就往东,她说什么我做什么,她要我干什么,我就干什么!在学校很美好,中途也出过一些小插曲,比如男闺蜜、男闺蜜2号等等等。但我都强迫她改掉了,我...
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
社会教会你的第一课
点赞 评论 收藏
分享
07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
评论
10
27
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务