题目1200:最大的两个数

题目描述:

    输入一个四行五列的矩阵,找出每列最大的两个数。

输入:

    输入第一行包括一个整数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();
			}
			
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值