精 挑 细 选

描述
小王是公司的仓库管理员,一天,他接到了这样一个任务:从仓库中找出一根钢管。这听起来不算什么,但是这根钢管的要求可真是让他犯难了,要求如下:
1、 这根钢管一定要是仓库中最长的;
2、 这根钢管一定要是最长的钢管中最细的;
3、 这根钢管一定要是符合前两条的钢管中编码最大的(每根钢管都有一个互不相同的编码,越大表示生产日期越近)。
相关的资料到是有,可是,手工从几百份钢管材料中选出符合要求的那根…… 
要不,还是请你编写个程序来帮他解决这个问题吧。
输入
第一行是一个整数N(N<=10)表示测试数据的组数)
每组测试数据的第一行 有一个整数m(m<=1000),表示仓库中所有钢管的数量,
之后m行,每行三个整数,分别表示一根钢管的长度(以毫米为单位)、直径(以毫米为单位)和编码(一个9位整数)。
输出
对应每组测试数据的输出只有一个9位整数,表示选出的那根钢管的编码,
每个输出占一行
样例输入
222000 30 1234567892000 20 98765432143000 50 8721984423000 45 7524981242000 60 7651287423000 45 652278122
样例输出

98765432175249812

import java.util.*;
public class Main {
	static Comparator<Pipe> com_Lon=new Comparator<Pipe>() {		
		@Override
		public int compare(Pipe o1, Pipe o2) {
			   return o1.lon-o2.lon;
		}
	};
	static Comparator<Pipe> com_Dia=new Comparator<Pipe>() {		
		@Override
		public int compare(Pipe o1, Pipe o2) {
			   return o1.dia-o2.dia;
		}
	};
	static Comparator<Pipe> com_Code=new Comparator<Pipe>() {		
		@Override
		public int compare(Pipe o1, Pipe o2) {
			   return o1.code-o2.code;
		}
	};
 public static void main(String[] args) {
	  Scanner in=new Scanner(System.in);
      int n=in.nextInt();
       while(n-->0){
         int m =in.nextInt();
         List<Pipe> st=new ArrayList<Pipe>();
         List<Pipe> st1=new ArrayList<Pipe>();
         List<Pipe> st2=new ArrayList<Pipe>();
         for (int i = 1; i <=m; i++){
        	 int lon=in.nextInt();
        	 int dia=in.nextInt();
        	 int code=in.nextInt();
        	 Pipe p=new Pipe(lon, dia, code);
        	 st.add(p);
         }
 		Collections.sort(st, com_Lon);	
        int maxL=st.get(st.size()-1).lon;
          for (int i = 0; i < st.size(); i++) {
               if(st.get(i).lon==maxL)
            	   st1.add(st.get(i));
		};
		Collections.sort(st1, com_Dia);
   		int minD=st1.get(0).dia;
        for (int i = 0; i < st1.size(); i++) {
            if(st1.get(i).dia==minD)
         	   st2.add(st1.get(i));
		}
    	Collections.sort(st2, com_Code);	
		System.out.println(st2.get(st2.size()-1).code);
       }	   
   } 
}

class Pipe{
	int lon;
	int dia;
	int code;
	Pipe(int l,int d,int c){
		this.lon=l;
		this.dia=d;
		this.code=c;
	}
}
         
这真是我写过的最长的程序QAQ,我太笨了,想不出来什么好办法,下面附上最优程序

  1. #include<stdio.h> 
  2. int main()  
  3. {  
  4.     int n,m,i,a,b,c,x,y,z;  
  5.     scanf("%d",&n);  
  6.     while(n--)  
  7.     {  
  8.         scanf("%d",&m);  
  9.         a=0;b=0;c=0;  
  10.         for(i=0;i<m;i++)  
  11.         {  
  12.             scanf("%d%d%d",&x,&y,&z);  
  13.             if(x>a||x==a&&y<b||x==a&&y==b&&z>c) { a=x;b=y;c=z; }  
  14.         }  
  15.         printf("%d\n",c);  
  16.     }  
  17.     return 0;  
  18. }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值