• 最小年龄的3个职工


    题目来源:2003-2005年华中科技大学计算机研究生机试真题

    题目描述:

    职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来。

    输入:

    输入第一行包括1个整数N,1<=N<=30,代表输入数据的个数。
    接下来的N行有N个职工的信息:
    包括职工号(整数), 姓名(字符串,长度不超过10), 年龄(1<=age<=100)。

    输出:

    可能有多组测试数据,对于每组数据,
    输出结果行数为N和3的较小值,分别为年龄最小的职工的信息。

    关键字顺序:年龄>工号>姓名,从小到大。

    样例输入:
    5
    501 Jack 6
    102 Nathon 100
    599 Lily 79
    923 Lucy 15
    814 Mickle 65
    样例输出:
    501 Jack 6
    923 Lucy 15
    814 Mickle 65

    代码:
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            while (input.hasNextInt()) {
                int n = input.nextInt();
                Member[] members = new Member[n];
                for (int i = 0;i<n;i++) {
                    members[i] = new Member(input.nextInt(),input.nextInt(),input.next());
                    
                }
                Arrays.sort(members);
                if (n > 3) {
                    for (int i = 0;i<3;i++) {
                        System.out.println(members[i].id+" "+members[i].name+" "+members[i].age);
                    }
                }else {
                    for (int i = 0;i<n;i++) {
                        System.out.println(members[i].id+" "+members[i].name+" "+members[i].age);
                    }
                }
            }
    
        }
        
        static class Member implements Comparable<Member>{
    
            int id;
            String name;
            int age;
            public Member(int id,int age,String name){
                this.id = id;
                this.name = name;
                this.age = age;
            }
            @Override
            public int compareTo(Member o1) {
                if (this.age < o1.age) {
                    return -1;
                }else if (this.age == o1.age) {
                    if (this.id < o1.id) {
                        return -1;
                    }else if (this.id == o1.id) {
                        return this.name.compareTo(o1.name);
                    }
                }
                return 1;
            }
    
            
            
        }
        
    }
  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/airycode/p/5398132.html
Copyright © 2020-2023  润新知