京东Java开发编程2道4.18

这次我是好好做了,但是不知道为啥没有全AC

先说说我的战绩吧,A:91%;B:33%

再贴一下我的代码,希望全AC的大佬分享一下思路:

  • 第一题:
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
// t1
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        Map<Integer, Integer> map;
        int a, b, tmp;
        boolean flag;
        Node[] nodes;
        while (T-- > 0) {
            map = new HashMap<Integer, Integer>();
            nodes = new Node[6];
    // 啊啊啊啊啊,我崩溃了啊啊啊啊啊啊,这里循环少了一个,不知道改成6之后是不是100%
            for (int i = 0; i < 5; i++) {
                a = in.nextInt();
                b = in.nextInt();
                if (a > b) {
                    tmp = a;
                    a = b;
                    b = tmp;
                }
                nodes[i] = new Node(a, b);
                map.put(a, map.getOrDefault(a, 0) + 1);
                map.put(b, map.getOrDefault(b, 0) + 1);
            }
            Arrays.sort(nodes, new Comparator<Node>() {
                public int compare(Node o1, Node o2) {
                    if (o1.a != o2.a)
                        return o1.a - o2.a;
                    return o1.b - o2.b;
                }
            });
            flag = true;
            for (int i = 0; i < 6; i += 2) {
                if (nodes[i].a != nodes[i + 1].a || nodes[i].b != nodes[i + 1].b)
                    flag = false;
            }
            if (map.size() > 3/* ||map.size() == 1 */)
                flag = false;
            System.out.println(!flag ? "IMPOSSIBLE" : "POSSIBLE");
        }
    }
}
class Node {
    int a, b;
    public Node(int a, int b) {
        this.a = a;
        this.b = b;
    }
}
  • 第二题:不知道为啥样例输出是5,但是我就加了1过了33%,/(ㄒoㄒ)/~~
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
// t2
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
        for (int i = 0, a, b; i < n; i++) {
            a = in.nextInt();
            b = in.nextInt();
            if (!map.containsKey(a))
                map.put(a, new HashSet<Integer>());
            map.get(a).add(i);
            if (!map.containsKey(b))
                map.put(b, new HashSet<Integer>());
            map.get(b).add(i);
        }
        int siz = map.size(), idx = 0;
        int[] arr = new int[siz];
        for (int key : map.keySet())
            arr[idx++] = key;
        Arrays.sort(arr);
        // System.out.println(Arrays.toString(arr));
        Map<Integer, Boolean> vis = new HashMap<Integer, Boolean>();
        int ans = 0, cnt = 0;
        Set<Integer> set;
        for (int i = 0; i < idx; i++) {
            set = map.get(arr[i]);
            // 从已经上车的人来遍历
            if (vis.size() > 0) {
                for (int key : vis.keySet()) {
                    if (set.contains(key)) {
                        // 删除掉已经出现的
                        set.remove(key);
                        // 空余个数加1
                        cnt++;
                    }
                }
            }
            // 剩下就是刚上车的
            if (set.size() > 0) {
                for (int val : set) {
                    vis.put(val, true);
                    if (cnt > 0)
                        cnt--;
                    else
                        ans++;
                }
            }
        }
        System.out.println(ans + 1);
    }
}
#京东笔试##京东##笔试题目#
全部评论
66%,33%
点赞 回复
分享
发布于 2020-04-18 21:13
联易融
校招火热招聘中
官网直投
81+33
点赞 回复
分享
发布于 2020-04-18 21:25
27%, 67%
点赞 回复
分享
发布于 2020-04-18 21:58
是股票题吗
点赞 回复
分享
发布于 2020-04-18 22:02
同样例是5。。原因不明。。然后+1输出了33%
点赞 回复
分享
发布于 2020-04-18 22:02
我觉得本身的样例就有问题,应该是5
点赞 回复
分享
发布于 2020-04-18 22:04
我和你战绩一样,91%、33%
点赞 回复
分享
发布于 2020-04-18 22:04
82+22。。
点赞 回复
分享
发布于 2020-04-18 23:27
我也是91+33
点赞 回复
分享
发布于 2020-04-19 11:04

相关推荐

3 8 评论
分享
牛客网
牛客企业服务