treemap的使用

2 篇文章 0 订阅

题目:

Description

小王是公司的仓库管理员,一天,他接到了这样一个任务:从仓库中找出一根钢管。这听起来不算什么,但是这根钢管的要求可真是让他犯难了,要求如下: 1、 这根钢管一定要是仓库中最长的; 2、 这根钢管一定要是最长的钢管中最细的; 3、 这根钢管一定要是符合前两条的钢管中编码最大的(每根钢管都有一个互不相同的编码,越大表示生产日期越近)。 相关的资料到是有,可是,手工从几百份钢管材料中选出符合要求的那根…… 要不,还是请你编写个程序来帮他解决这个问题吧。

Input

第一行是一个整数N(N<=10)表示测试数据的组数) 每组测试数据的第一行 有一个整数m(m<=1000),表示仓库中所有钢管的数量, 之后m行,每行三个整数,分别表示一根钢管的长度(以毫米为单位)、直径(以毫米为单位)和编码(一个9位整数)。

Output

对应每组测试数据的输出只有一个9位整数,表示选出的那根钢管的编码, 每个输出占一行

Sample Input

22 2000 30 123456789 2000 20 9876543214 3000 50 872198442 3000 45 752498124 2000 60 765128742 3000 45 652278122

Sample Output

987654321 752498124


解决代码:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        while (N-- > 0) {
            int n = sc.nextInt();
            Product products[] = new Product[n];
            for (int i = 0; i < products.length; i++) {
                int length = sc.nextInt();
                int wide = sc.nextInt();
                int number = sc.nextInt();
                products[i] = new Product(length, wide, number);
            }
            Arrays.sort(products, new Comparator<Product>() {
                @Override
                public int compare(Product o1, Product o2) {
                    if (o1.length != o2.length)
                        return o1.length - o2.length;
                    if (o1.wide != o2.wide)
                        return o2.wide - o1.wide;
                    return o1.number - o2.number;
                }
            });
            System.out.println(products[products.length - 1].number);
        }
    }
 
    public static class Product {
        int length;
        int wide;
        int number;
 
        public Product(int length, int wide, int number) {
            super();
            this.length = length;
            this.wide = wide;
            this.number = number;
        }
 
        @Override
        public String toString() {
            return length + "," + wide + "," + number;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值