京东笔试编程题代码
第一题,刚看到第一题的时候没有思路,直接去做了第二题,第二题昨晚只剩不到半个小时, 虽然有了一点思路,但是没写完,现在又花了一个小时才把自己的思路实现,用题目中给的 用例测试是对的,可惜没办法验证是否ACpackage jingDong;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner in = null;
try {
in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int N = in.nextInt(), M = in.nextInt();
Map<Integer, Set<Integer>> map = new HashMap<>();
for (int j = 0; j < M; j++) {
int val1 = in.nextInt(), val2 = in.nextInt();
if (!map.containsKey(val1)) {
map.put(val1, new HashSet<>());
}
map.get(val1).add(val2);
if (!map.containsKey(val2)) {
map.put(val2, new HashSet<>());
}
map.get(val2).add(val1);
}
int[] groupArr = new int[N];
Set<Integer> grouperSet = new HashSet<>();
for (int i = 0; i < N; i++) {
grouperSet.clear();
if (groupArr[i] == 0) {
grouperSet.add(i);
for (int j = i + 1; j < N; j++) {
if (groupArr[j] == 0) {
boolean flag1 = map.get(i + 1).containsAll(map.get(j + 1));
boolean flag2 = map.get(j + 1).containsAll(map.get(i + 1));
if (flag1 && flag2) {
grouperSet.add(j);
}
}
}
if (grouperSet.size() + map.get(i + 1).size() == N) {
for (Integer node : grouperSet) {
groupArr[node] = grouperSet.size();
}
}
}
}
for (int i = 0; i < groupArr.length; i++) {
if (groupArr[i] == 0) {
System.out.println("false");
return;
}
}
System.out.println("true");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
}
第二题
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner in = null;
try {
in = new Scanner(System.in);
int n = in.nextInt(), count = 0;
;
List<Long> list1 = new ArrayList<>(), list2 = new ArrayList<>(), list3 = new ArrayList<>();
int[] counted = new int[n];
for (int i = 0; i < n; i++) {
long temp1 = in.nextLong(), temp2 = in.nextLong(), temp3 = in.nextLong();
if (list1.size() > 0) {
for (int j = 0; j < list1.size(); j++) {
if (counted[j] == 0 && list1.get(j) < temp1 && list2.get(j) < temp2 && list3.get(j) < temp3) {
count++;
counted[j] = 1;
}
if (list1.get(j) > temp1 && list2.get(j) > temp2 && list3.get(j) > temp3) {
count++;
counted[i] = 1;
break;
}
}
}
list1.add(temp1);
list2.add(temp2);
list3.add(temp3);
}
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
}
#京东##笔试题目#