问题描述
《审美的历程》课上有n位学生,帅老师展示了m幅画,其中有些是梵高的作品,另外的都出自五岁小朋友之手。老师请同学们分辨哪些画的作者是梵高,但是老师自己并没有答案,因为这些画看上去都像是小朋友画的……老师只想知道,有多少对同学给出的答案完全相反,这样他就可以用这个数据去揭穿披着皇帝新衣的抽象艺术了(支持帅老师^_^)。
答案完全相反是指对每一幅画的判断都相反。
这个是来自一位大佬写的,,,不太懂
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map.Entry;
public class Main {
private static Reader reader;
public static void main(String[] args) {
reader = new InputStreamReader(System.in);
int n, m;
n = getInt();
m = getInt();
HashMap<Integer, Integer> hm = new HashMap<>(n);
for (int i = 0; i < n; ++i) {
int num = 0, x = 0;
for (int j = 0; j < m; j++) {
x = getInt();
num = (num << 1) + x;
}
hm.put(num, hm.containsKey(num) ? hm.get(num) + 1 : 1);
}
int sum = 0, maxn = (1 << m) - 1;
for (Entry<Integer, Integer> entry : hm.entrySet()) {
if (maxn / 2 < entry.getKey())
continue;
int key = entry.getKey() ^ maxn;
if (hm.containsKey(key))
sum += hm.get(key) * entry.getValue();
}
System.out.println(sum);
}
public static int getInt() {
int res = 0, read;
try {
while ((read = reader.read()) != -1) {
if (Character.isDigit(read)) {// 因为全是非负数,不需要判断负号‘-’,只要是数字就行
res = read - '0';
while ((read = reader.read()) != -1) {// 继续得到能得到的数字
if (Character.isDigit(read)) {
res = res * 10 + (read - '0');
} else {
break;
}
}
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
}
还有一种,但是不能得全分
import java.util.Scanner;
public class test6 {
public static void main(String[] args)
{
Scanner sysin=new Scanner(System.in);
int m,n;
//input n and m
n=sysin.nextInt();
m=sysin.nextInt();
int[][] a=new int[n][m];
//01矩阵的形成
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
a[i][j]=sysin.nextInt();
//测试数据,看01矩阵是否成功
//for(i=0;i<n;i++)
//{
// for(j=0;j<m;j++)
// System.out.print(a[i][j]+" ");
// System.out.println();
//}
//测试数据,看01矩阵是否成功
//=============================================================
//开始比较匹配的个数
int counts=0;//用来计数,表示匹配题目的个数
int k; //k表示每次比较对象的行,从第一个开始
int flag;
for(k=0;k<n-1;k++) //总共有n行,但我只需要比较n-1行就行了
{
//让第k行和n-k行依次比较
for(i=k+1;i<n;i++)
{
flag=1; //作为成功的表示,1表示成功,0表示失败,不符合
for(j=0;j<m;j++)
if(a[k][j]+a[i][j]!=1)
{
flag=0;
break;
}
if(flag==1)
counts++;
}
}
System.out.println(counts);
}
}