法一
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = 0, b = 0, c = 0, temp = 0;
a = scanner.nextInt();
b = scanner.nextInt();
c = scanner.nextInt();
int[] day = new int[c];
int[][] arr = new int[a][b];
for (int i = 0; i < a; i++) {// 6个人,3套题,7天输入二维数组
for (int j = 0; j < b; j++) {
arr[i][j] = scanner.nextInt();
}
}
boolean[] f = new boolean[c];
for (int j = 0; j < b; j++) {// 横着来
Arrays.fill(f, false);
for (int k = 0; k < a; k++) // 竖着来
{
if (!f[arr[k][j] - 1]) { //如果没算入
day[arr[k][j] - 1]++;
f[arr[k][j] - 1] = true;
}
}
}
for (int i = 0; i < c; i++) {//
System.out.print(day[i] + " ");
}
scanner.close();
}
}
全部输入,竖着加
法二
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = 0, b = 0, c = 0, temp = 0;
a = scanner.nextInt();
b = scanner.nextInt();
c = scanner.nextInt();
int[] day = new int[c];
int[][] arr = new int[b][c];
for (int i = 0; i < a; i++) {// 6个人,3套题,7天输入二维数组
for (int j = 0; j < b; j++) {
temp = scanner.nextInt();
arr[j][temp-1] = 1; //竖着的重复不会重复,而竖着的不一样的都被记录成了1
}
}
/* 1 2 3 4 5 6 7
1 1 1 1
2 1 1 1
3 1 1 1
*/
for (int j = 0; j < c; j++) { //一束一束的加
for (int k = 0; k < b; k++)
{
day[j] += arr[k][j];
}
}
for (int i = 0; i < c; i++) {//
System.out.print(day[i] + " ");
}
scanner.close();
}
}
注:
输入:
6 3 7
2 3 4
2 5 7
3 5 7
1 3 5
5 6 7
1 2 3
草稿: //同一列,相同答案,映射是同一个数
1 2 3 4 5 6
1 1
2
3 …