小红书8.28笔试ac啦
排队
第一题就是简单的把每个大臣的多个权重加起来排序就行了,选择一个稳定的排序算法即可。
法术
第二题将两两元素进行相乘,记得避开相同元素。读取数据的时候由于存在数据是10的16次方,选择long类型数据才能通过。
import java.util.Arrays; import java.util.Scanner; public class 小红书法术 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int count = scanner.nextInt(); long target = scanner.nextLong(); long[] performances = new long[count]; if (count == 1) { System.out.println(0); } else { for (int i = 0; i < count; i++) { performances[i] = scanner.nextLong(); } Arrays.sort(performances); long result = 0; for (int i = count - 1; i >= 1; i--) { if (performances[i] * performances[i] < target) { break; } for (int j = i - 1; j >= 0; j--) { if (performances[i] * performances[j] >= target) { result++; } else { break; } } } System.out.println(2 * result); } } }
一对一
第三题简单来说就是每次都选择朋友最少的人组队,动态删除已经组队好的人就行了。
import java.util.*; public class 小红书一对一 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Map<Integer, Integer> friend = new HashMap<>(); for (int i = 1; i < n; i++) { friend.put(i + 1, scanner.nextInt()); } int count = 0; List<Map.Entry<Integer, Integer>> originList = new ArrayList<>(friend.entrySet()); while (!originList.isEmpty()) { Map.Entry<Integer, Integer> target = null; List<Map.Entry<Integer, Integer>> delete = new ArrayList<>(); for (Map.Entry<Integer, Integer> entry : originList) { if (target == null) { if (!friend.containsValue(entry.getKey())) { target = entry; count++; } } } for (Map.Entry<Integer, Integer> entry : originList) { if (target != null) { if (entry.getValue() - target.getKey() == 0 || entry.getValue() - target.getValue() == 0 || entry.getKey() - target.getKey() == 0 || entry.getKey() - target.getValue() == 0) { delete.add(entry); } } } for (Map.Entry<Integer, Integer> entry : delete) { originList.remove(entry); } friend = new HashMap<>(); for (Map.Entry<Integer, Integer> entry : originList) { friend.put(entry.getKey(), entry.getValue()); } } System.out.println(count); } }#校招##小红书##小红书笔试##笔试题简单,小红书真有海量hc吗#