2.会议通知
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); int count; for (int i = 0; i < m; i++) { count = in.nextInt(); ArrayList<Integer> items = new ArrayList<>(); for (int j = 0; j < count; j++) { items.add(in.nextInt()); } list.add(items); } ArrayList<Integer> temp = new ArrayList<>(); temp.add(0); int countN = 0; while (temp != null && temp.size() > 0) { int item = temp.remove(0); countN++; for (ArrayList<Integer> integers : list) { if (integers != null && integers.size() > 0 && integers.contains(item)) { while (integers != null && integers.size() > 0) { int remove = integers.remove(0); if (!temp.contains(remove) && remove != item) { temp.add(remove); } } } } } System.out.print(countN); } }
4.中位数
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int len = in.nextInt(); int[] nums = new int[len]; for (int i = 0; i < nums.length; i++) { nums[i] = in.nextInt(); } int[] numArr = Arrays.copyOf(nums,nums.length); Arrays.sort(numArr); int small = numArr[len/2-1]; int large = numArr[len/2]; for (int i = 0; i < nums.length; i++) { if (nums[i] >= large){ System.out.println(small); }else { System.out.println(large); } } } }
全部评论
(0) 回帖