栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

PAT 甲级 1006 Sign In and Sign Out (25 分)(Java)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PAT 甲级 1006 Sign In and Sign Out (25 分)(Java)

文章目录
  • PAT 甲级 1006 Sign In and Sign Out (25 分)(Java)
    • 题目
    • 题意解读
    • 解题思路
    • 解法
      • 解法一
      • 解法二

PAT 甲级 1006 Sign In and Sign Out (25 分)(Java) 题目

题目链接

题意解读

这题的大意就是要根据时间,找出最早到的和最晚离开的;

解题思路

思路一:首先想到的是通过相应的数据结构记录相应的名称,开始时间,结束时间,然后通过数组保存所有的数据,然后通过排序判定最早到的时间和最晚走的时间;

思路二:这题的简便方法就是将这种带格式的时间全部转换成秒数,然后记录最小的秒数和最大的秒数,就是进入最早和离开最晚的人;

解法 解法一
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Ti{
    String name;
    int start;
    int end;
}
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        sc.nextLine();
        Ti[] d = new Ti[m];
        for (int i = 0; i < m; i++) {
            d[i] = new Ti();
            String[] st = sc.nextLine().split(" ");
            d[i].name = st[0];
            String[] s1 = st[1].split(":");
            d[i].start = Integer.valueOf(s1[0])*3600 + Integer.valueOf(s1[1])*60 + Integer.valueOf(s1[2]);
            String[] s2 = st[2].split(":");
            d[i].end = Integer.valueOf(s2[0])*3600 + Integer.valueOf(s2[1])*60 + Integer.valueOf(s2[2]);
        }
        Arrays.sort(d, new Comparator() {
            @Override
            public int compare(Ti o1, Ti o2) {
                return o1.start - o2.start;
            }
        });
        System.out.print(d[0].name + " ");
        Arrays.sort(d, new Comparator() {
            @Override
            public int compare(Ti o1, Ti o2) {
                return o2.end - o1.end;
            }
        });
        System.out.println(d[0].name);
    }
}
解法二
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        sc.nextLine();
        String[] name = new String[m];
        int startIndex = -1;
        int endIndex = -1;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < m; i++) {
            String[] st = sc.nextLine().split(" ");
            name[i] = st[0];
            String[] s1 = st[1].split(":");
            int start = Integer.valueOf(s1[0])*3600 + Integer.valueOf(s1[1])*60 + Integer.valueOf(s1[2]);
            if(start < min){
                min = start;
                startIndex = i;
            }
            String[] s2 = st[2].split(":");
            int end = Integer.valueOf(s2[0])*3600 + Integer.valueOf(s2[1])*60 + Integer.valueOf(s2[2]);
            if(end > max){
                max = end;
                endIndex = i;
            }
        }
        System.out.println(name[startIndex] + " " + name[endIndex]);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340815.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号