输入第一行表示样例数m,对于每个样例,第一行为数的个数n,接下来两行分别有n个数,第一行有n个数,第二行的n个数分别对应上一行每个数的分组,n不超过100。
输出m行,格式参见样例,按从小到大排。
1 7 3 2 3 8 8 2 3 1 2 3 2 1 3 1
1={2=0,3=2,8=1} 2={2=1,3=0,8=1} 3={2=1,3=1,8=0}
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int m = scanner.nextInt(); for(int i = 0;i < m; i++) { int n = scanner.nextInt(); //保存数的数组 int[] arr = new int[n]; //保存组的数组 int[] group = new int[n]; //用来统计不同数的情况,排除重复 List<Integer> arrList = new ArrayList<Integer>(); //用来统计不同组的情况,排除重复 List<Integer> groupList = new ArrayList<Integer>(); for (int j = 0; j < n; j++) { arr[j] = scanner.nextInt(); if (!arrList.contains(arr[j])) { arrList.add(arr[j]); } } //数按顺序排列 Collections.sort(arrList); for (int j = 0; j < n; j++) { group[j] = scanner.nextInt(); if (!groupList.contains(group[j])) { groupList.add(group[j]); } } //组按顺序排列 Collections.sort(groupList); for (int j = 0; j < groupList.size(); j++) { System.out.print(groupList.get(j)+"={"); for (int k = 0; k < arrList.size(); k++) { System.out.print(arrList.get(k)+"="); //用来统计该组中,某数出现的次数 int sum = 0; for (int k2 = 0; k2 < n; k2++) { if (arr[k2] == arrList.get(k) && group[k2] == groupList.get(j)) { sum++; } } if(k == arrList.size()-1) { //如果是最后一个则后面加"}" System.out.println(sum+"}"); }else { //如果不是末尾则后面加"," System.out.print(sum+","); } } } } } } }