美团笔试 Java

1.  AC, 输入 n 个点的坐标,求出所有满足: 该点上下左右都有点,的点
import java.util.*;

/**
 * @author xx
 * @data 4/23/20 6:47 PM
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<point> list = new ArrayList<>();
        sc.nextLine();
        for (int i = 0; i < n; i++) {
            String str = sc.nextLine();
            String[] t = str.split(" ");
            list.add(new point(Integer.parseInt(t[0]), Integer.parseInt(t[1])));
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
            point cur = list.get(i);
            boolean up = false, down = false, left = false, right = false;
            for (int j = 0; j < n; j++) {
                if (j == i) {
                    continue;
                }
                point cur2 = list.get(j);
                if (cur2.x == cur.x) {
                    if (cur2.y > cur.y) {
                        up = true;
                    }
                    if (cur2.y < cur.y) {
                        down = true;
                    }
                }
                if (cur2.y == cur.y) {
                    if (cur2.x > cur.x) {
                        right = true;
                    }
                    if (cur2.x < cur.x) {
                        left = true;
                    }
                }
            }
            if (up && down && left && right) {
                count++;
            }
        }
        System.out.println(count);
    }
}

class point{
    int x;
    int y;

    public point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
2.  AC, 输入 一些 数,按照要求格式输出, 有整数, 负整数,小数,负小数,0
            // 三位一个 逗号 整数部分 从后向前 隔三位补 逗号
            // 小数两位, 够了直接舍掉, 不够补足,  
            // $  转换完了加 $           
            // 负数, 再加上括号,去掉负号 
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author xx
 * @data 4/23/20 7:47 PM
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<String> list = new ArrayList<>();
        while (sc.hasNextLine()) {
            String cur = sc.nextLine();
            if (cur.isEmpty()) {
                break;
            }
            list.add(cur.trim());
        }
//        List<String> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            // 三位一个 逗号
            // 小数两位, 直接舍掉
            // 负数, 先转化, 再加上括号,省略负号
            // $
            String cur = list.get(i);
            System.out.println(trans(cur));
        }

    }

    private static String trans(String cur) {
        StringBuffer sb = new StringBuffer("");
        boolean isFuShu = cur.charAt(0) == '-' ? true : false;
        if (isFuShu) {
            cur = cur.substring(1);
        }
        // 小数,整数,负整数,负小数或者零
        if (cur.indexOf('.') != -1) {  // 小数
            sb = xiao(cur);
        } else {
            sb = zheng(cur);
            sb.append(".00");
        }
        if (isFuShu) {
            sb.insert(0, '(');
            sb.append(')');
        }
        return String.valueOf(sb);
    }

    private static StringBuffer xiao(String cur) {
        int dianIndex = cur.indexOf('.');
        int len = cur.length();
        String tail;
        if (dianIndex == len - 1) {
            tail = "00";
        } else if (dianIndex == len - 2) {
            tail = cur.substring(dianIndex) + '0';
        } else {
            tail = cur.substring(dianIndex, dianIndex + 3);
        }
        StringBuffer left = zheng(cur.substring(0, dianIndex));
        left.append(tail);
        return left;
    }

    private static StringBuffer zheng(String cur) {
        StringBuffer res = new StringBuffer(cur);
        int count = 0;
        for (int j = cur.length() - 1; j >= 0; j--) {
            count++;
            if (count == 3 && j != 0) {
                res.insert(j, ',');
                count = 0;
            }
        }
        res.insert(0, '$');
        return res;
    }
}
3. 没看
4. 最优购买策略: 贪心把, n个产品  k 个人 每个产品有两个属性 price 和 id (1表示A产品,2表示B产品), 策略: 只买B 不打折, 买了(只要存在A) A 最便宜那个半价,求最少花多少钱
哭泣,不知道哪里错了 18%
import java.awt.*;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @author chenhong
 * @data 4/23/20 6:47 PM
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), k = sc.nextInt();
        int[] arr = new int[n];  // n 物品数量
        sc.nextLine();
        PriorityQueue<Point> pq = new PriorityQueue<>(new Comparator<Point>() { @Override public int compare(Point point, Point point2) {
                if (point.y == point2.y) {
                    return point2.x - point.x;
                } else {
                    return point.y - point2.y;
                }
            }
        });
        for (int i = 0; i < n; i++) {
            pq.add(new Point(sc.nextInt(), sc.nextInt()));
        }

        int count = 0;
        Point cur = pq.poll();
        while (k > 1 && !pq.isEmpty() && cur.y == 1) {  // 优先队列,在还剩下不止一个人的并且还有产品并且这个产品是A产品的时候,每一个人都只买A产品,这样都能打半价 if (cur.x % 2 == 0) {
                count += cur.x / 2;
            } else {
                count += cur.x / 2 + 0.5;
            }
            cur = pq.poll();
            k--;
        }
        // 不满足: k==1, pq 空 ; cur.y == 2
        if (cur.y == 2) {   // 不能半价了
            count += cur.x;
            while (!pq.isEmpty()) {
                count += pq.poll().x;
            }
        }else {   // 当前还是 A 产品,最低价格可以打半价
            count += cur.x;
            while (!pq.isEmpty()) {
                cur = pq.poll();
                count += cur.x;
            }
            if (cur.x % 2 == 0) {
                count -= cur.x / 2;
            } else {
                count -= cur.x / 2 + 0.5;
            }
        }
        System.out.format("%.2f", (float)count);
    }
}

class point{
    int x;
    int y;

    public point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

5. n个数据, & 结果为0 就输出 1, 否则没有和该数 & 结果为 0 的就输出 -1, 超时了 18%
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author chenhong
 * @data 4/23/20 6:47 PM
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        sc.nextLine();
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int[] res = new int[n];
        Arrays.fill(res, -1);
        for (int i = 0; i < n; i++) {
            if (res[i] == 1) {
                continue;
            }
            for (int j = i + 1; j < n; j++) {
                if (res[j] == 1) {
                    continue;
                }
                if ((arr[i] & arr[j]) == 0) {
                    res[i] = 1;
                    res[j] = 1;
                    break;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.print(res[i] + " ");
        }
    }
}

#423美团笔试题##笔试题目#
全部评论
大佬最后一个通过了18%嘛
点赞
送花
回复
分享
发布于 2020-04-23 22:06
美团笔试总共5道题,请问多少分能够进面试啊?
点赞
送花
回复
分享
发布于 2020-04-23 22:08
滴滴
校招火热招聘中
官网直投
第四题,贪心:前k-1个尽量没人一个A,都打五折。剩下的一个人全买(当有k个及以上的A,去剩下的最便宜的打五折,否则累加就好了)
点赞
送花
回复
分享
发布于 2020-04-23 22:12
大佬大佬,我最后一题也是暴力,过了18%
点赞
送花
回复
分享
发布于 2020-04-23 22:54
请问要点交卷吗。我时间到了自动提交了,会不会没有记录啊
点赞
送花
回复
分享
发布于 2020-04-23 23:10
请问第一题的第12行:sc.nextLine();是什么作用呀是换行么
点赞
送花
回复
分享
发布于 2020-04-24 13:59
第二题我本地输入就过不去 迷惑
点赞
送花
回复
分享
发布于 2020-04-24 14:29
楼主,想问一下第二题的输入。测试用例是每一组都有多行输入吗?这样怎么判断输入结束呢?
点赞
送花
回复
分享
发布于 2020-04-24 16:12

相关推荐

1 22 评论
分享
牛客网
牛客企业服务