题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int inum = in.nextInt();
List<String> inums = new ArrayList<>();
for (int i =0;i<inum;i++){
inums.add(in.nextInt()+"");
}
int rnum = in.nextInt();
List<String> rnums =new ArrayList<>();
Set<String> r = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Integer.compare(Integer.parseInt(o1),Integer.parseInt(o2));
}
});
for (int j =0;j<rnum;j++){
rnums.add(in.nextInt()+"");
r.add(rnums.get(j));
}
Iterator<String> it = r.iterator();
StringBuilder sb = new StringBuilder();
int allCount=0;
while (it.hasNext()){
String ri = it.next();
if (!inums.toString().contains(ri)){
continue;
}
sb.append(ri).append(" ");
allCount++;
int count=0;
StringBuilder s = new StringBuilder();
for (int i =0;i<inum;i++){
if (inums.get(i).contains(ri)){
s.append(i).append(" ").append(inums.get(i)).append(" ");
allCount+=2;
count++;
}
}
sb.append(count).append(" ").append(s);
allCount++;
}
System.out.println(allCount+" "+sb);
}
}