• (JAVA)P5707 【深基2.例12】上学迟到


    【深基2.例12】上学迟到

    一、题目描述

    学校和 yyy 的家之间的距离为 s s s 米,而 yyy 以 v v v 米每分钟的速度匀速走向学校。

    在上学的路上,yyy 还要额外花费 10 10 10 分钟的时间进行垃圾分类。

    学校要求必须在上午 8:00 \textrm{8:00} 8:00 到达,请计算在不迟到的前提下,yyy 最晚能什么时候出门。

    由于路途遥远,yyy 可能不得不提前一点出发,但是提前的时间不会超过一天。

    二、输入格式

    一行两个正整数 s , v s,v s,v,分别代表路程和速度。

    三、输出格式

    输出一个 24 24 24 小时制下的时间,代表 yyy 最晚的出发时间。

    输出格式为 HH:MM \texttt{HH:MM} HH:MM,分别代表该时间的时和分。必须输出两位,不足前面补 0 0 0

    四、输入输出样例

    在这里插入图片描述

    五、代码

    (1)版本一

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int s = sc.nextInt();
            int v = sc.nextInt();
            int time = 0;
            if ((s % v) == 0){
                time = s / v + 10;
            }else {
                time = s / v + 10 + 1;
            }
            int h1 = (int) (time / 60);
            int m1 = time - h1 * 60;
            int h2 = 0;
            if (h1 <= 7){
                h2 = 7 - h1;
            }else {
                h2 = 7 + 24 -h1;
            }
            int m2 = 60 - m1;
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY,h2);
            c.set(Calendar.MINUTE,m2);
            System.out.println(sdf.format(c.getTime()));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    (2)版本二

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int s = sc.nextInt();
            int v = sc.nextInt();
            int time = 0;
            if((s % v) == 0){
                time = (int)(s / v);
            }else {
                time = (int) (s / v + 1);
            }
            Calendar c = Calendar.getInstance();
            c.set(2022,Calendar.NOVEMBER,5,8,0,0);
            c.add(Calendar.MINUTE,-10);
            c.add(Calendar.MINUTE,-time);
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            System.out.println(sdf.format(c.getTime()));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    六、知识点

    (1)Calendar类的使用

    Calendar类的具体使用

    (2)SimpleDateFormat 格式化日期的使用

    SimpleDateFormat 格式化日期的使用

  • 相关阅读:
    LeetCode 每日一题 2022/9/26-2022/10/2
    Go日志组件Zap的基本使用
    c语言-链表
    Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day07】——Java基础篇
    Combiner和Partitioner
    【ROS】Nav2源码之nav2_planner详解
    【C++】从零开始的CS:GO逆向分析2——配置GLFW+IMGUI环境并创建透明窗口
    澳洲猫罐头到底怎么样呢?我自己亲自喂养过的优质猫罐头分享
    使用Boto3访问AWS S3服务
    RedisTemplate操作Redis
  • 原文地址:https://blog.csdn.net/xjl243636988/article/details/127705300