-
题目描述:
-
输入一个四行五列的矩阵,找出每列最大的两个数。
-
输入:
-
输入第一行包括一个整数n(1<=n<=1000),接下来的四行每行包括五个整数。代表一个四行五列的矩阵,矩阵元素全部是整数。
-
输出:
-
可能有多组测试数据,对于每组数据,按照样例输出的格式将每列最大的两个数输出,如果最大的两个数中的一个数在这一列中有多个相同的值,则行值取行值小的那一个。
输出时要保留原矩阵的行列顺序,即在原矩阵中行值小的,在输出矩阵中的行值依然小。
-
样例输入:
-
1 1 2 4 9 8 -1 4 9 8 8 12 9 8 7 0 7 8 9 7 0
-
样例输出:
-
12 9 9 9 8 7 8 9 8 8
-
提示:
-
每个数字后面都要输出一个空格
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.lang.Comparable;
import java.util.PriorityQueue;
class Main
{
public static final boolean DEBUG = false;
static class Node implements Comparable<Node>
{
int index;
int val;
public Node (int val, int index)
{
this.val = val;
this.index = index;
}
public int compareTo(Node other)
{
if (val != other.val) return other.val - val;
return index - other.index;
}
}
public static void main(String[] args) throws IOException
{
Scanner cin;
int n;
if (DEBUG) {
cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
} else {
cin = new Scanner(new InputStreamReader(System.in));
}
n = cin.nextInt();
while (n-- > 0) {
int[][] a = new int[4][5];
int[][] ans = new int[2][5];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = cin.nextInt();
}
}
for (int j = 0; j < 5; j++) {
PriorityQueue<Node> pq = new PriorityQueue<Node>();
for (int i = 0; i < 4; i++) pq.add(new Node(a[i][j], i));
Node tmp1 = pq.poll(), tmp2 = pq.poll();
if (tmp1.index < tmp2.index) {
ans[0][j] = tmp1.val;
ans[1][j] = tmp2.val;
} else {
ans[0][j] = tmp2.val;
ans[1][j] = tmp1.val;
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(ans[i][j] + " ");
}
System.out.println();
}
}
}
}