题目:
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;
}
}
}