题目解析
输入n组数,找出n组数相同的数组并统计 ,最后将统计结果输出
解题思路
自己写一个对象 对象中去判断当前的是否与其他的相同
用map存储对象
然后用数组存储map中的值
给数组排序
最后输出即可
代码
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
static class edge implements Comparable<edge>
{
int shu[];
int cnt = 0;
public edge(int shu[])
{
this.shu = shu;
}
public boolean equals(edge other) // 看添加进来的是否又出现过
{
for (int i = 0; i < this.shu.length; i++)
{
if (this.shu[i] != other.shu[i])
return false;
}
return true;
}
@Override
public int compareTo(edge other) // 排序
{
if (this.cnt != other.cnt)
return other.cnt - this.cnt;
for (int i = 0; i < this.shu.length; i++)
{
if (this.shu[i] != other.shu[i])
return this.shu[i] - other.shu[i];
}
return 0;
}
}
public static void main(String[] args) throws IOException
{
int n = ini(), m = ini();
TreeMap<edge, Integer> mp = new TreeMap<edge, Integer>();
for (int i = 1; i <= n; i++)
{
int shu[] = new int[m];
for (int j = 0; j < m; j++)
shu[j] = ini();
mp.merge(new edge(shu), 1, Integer::sum);
}
int len = mp.size();
edge res[] = new edge[len + 10];
int pos = 0;
// 遍历存到数组里
for (edge i : mp.keySet())
{
pos++;
res[pos] = i;
res[pos].cnt = mp.get(i);
}
Arrays.sort(res, 1, len + 1);
// 输出个数
out.println(len);
for (int i = 1; i <= len; i++)
{
StringBuilder t = new StringBuilder();
t.append(res[i].cnt);
for (int j = 0; j < m; j++)
t.append(" " + res[i].shu[j]);
out.println(t);
}
out.flush();
out.close();
}
static StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int ini() throws IOException
{
sc.nextToken();
return (int) sc.nval;
}
}
hashmap.merge(key, value, remappingFunction)
key - 键
value - 值
remappingFunction - 重新映射函数,用于重新计算值返回值
lambda表达式:( 参数 ) - > 表达式
merge我个人觉得和普通的containsKey()查找相同再存进去一样,不过merge更方便点 第三个 里( 可以 ( 那个我也不确定 ,但是这种形式我觉得是lambda表达式) )放lambda表达式( 而 lambda表达式 也觉得是是释放双手的一个东东,它可以将参数里面的数进行表达式运算再传出答案 )求个数
