PTA 宿舍谁最高?(Java)

学校选拔篮球队员,每间宿舍最多有 4 个人。现给出宿舍列表,请找出每个宿舍最高的同学。定义一个学生类 Student,有身高 height,体重 weight 等。

输入格式:

首先输入一个整型数 n (1≤n≤106),表示有 n 位同学。

紧跟着 n 行输入,每一行格式为:宿舍号 name height weight
宿舍号的区间为 [0, 999999], name 由字母组成,长度小于 16,heightweight 为正整数。

输出格式:

按宿舍号从小到大排序,输出每间宿舍身高最高的同学信息。题目保证每间宿舍只有一位身高最高的同学。

注意宿舍号不足 6 位的,要按 6 位补齐前导 0。

输入样例:

7
000000 Tom 175 120
000001 Jack 180 130
000001 Hale 160 140
000000 Marry 160 120
000000 Jerry 165 110
000003 ETAF 183 145
000001 Mickey 170 115

输出样例:

000000 Tom 175 120
000001 Jack 180 130
000003 ETAF 183 145

为了方便要把宿舍号写成整数形式并和一维数组联系起来用来保存每个宿舍身高最高学生信息,在保存学生信息是要判断学生所在宿舍是否有人若没人则把第一个作为最高学生信息保存下来在对应数组下标,若已有人则需要判断这个人身高是否比已经存入的高若高则替换掉这个这个学生信息。

输出时要判断对应数组下标是否为空不是空的就输出该学生信息。

 

import java.util.Scanner;

class Student{
	int id;
	String name;
	int height;
	int weight;
	public Student(int id, String name, int height, int weight) {
		super();
		this.id = id;
		this.name = name;
		this.height = height;
		this.weight = weight;
	}
	
	public int getId() {
		return id;
	}

	public int getHeight() {
		return height;
	}
	@Override
	public String toString() {
		return String.format("%06d %s %d %d",this.id,this.name,this.height,this.weight);
		
	}
	
}
public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
      Student[] stu=new Student[1000000];
      int n=sc.nextInt();
      for(int i=0;i<n;i++)
      {
    	  Student s=new Student(sc.nextInt(),sc.next(),sc.nextInt(),sc.nextInt());
    	  if(stu[s.getId()]==null)
    	  {
    		  stu[s.getId()]=s;
    	  }
    	  else if(stu[s.getId()].getHeight()<s.getHeight())
    	  {
    		  stu[s.getId()]=s;
    	  }
      }
      for(Student s:stu)
      {
    	  if(s!=null)
    	  {
    		  System.out.println(s);
    	  }
      }
      sc.close();
       
   }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值