算法题解专栏
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}
模拟
多次使用的数组记得初始化
TreeMap:有序集合按键进行排序
二维数组也行
import java.util.*;
public class Main
{
static int[] m = new int[1010];// 记录什么数出现过
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0)
{
int n = sc.nextInt();
int[] a = new int[n];
int[] c = new int[n];// 记录 i 所在的小组编号
Arrays.fill(m, 0);
for (int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
m[a[i]] = 1;
}
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i <= 1000; i++)
if (m[i] == 1)
list.add(i);
// HashSet set = new HashSet<>();
int num = -1;
for (int i = 0; i < n; i++)
{
c[i] = sc.nextInt();
num = Math.max(c[i], num);
}
int[] cnt = new int[1010];// 计数数组,记录 i 出现的次数
for (int i = 1; i <= num; i++)// 枚举每一组
{
Arrays.fill(cnt, 0);
System.out.print(i + "={");
for (int j = 0; j < n; j++)// 枚举所有数对应的组
{
if (c[j] == i)
{
cnt[a[j]]++;
}
}
int size = list.size();
for (int j = 0; j < size - 1; j++)
{
System.out.print(list.get(j) + "=" + cnt[list.get(j)] + ",");
}
System.out.print(list.get(size - 1) + "=" + cnt[list.get(size - 1)]);
System.out.println("}");
}
}
}
}