zoukankan      html  css  js  c++  java
  • 编程题:SaturdayNightStay

    "输入2019年的一个时间段,开始时间代表出发,结束时间代表在那一天返回,判断在该时间段内,如果旅行有多少个子时间段可以在周六晚上休息"
    * 周六晚上休息,即子时间段必须包含周六,且唯一一个周六不能是最后一天
    * 例如6月14~6月15,6月15虽然是星期六,但是返回时间是周六,则不能在周六休息,返回0
    * 6月15~6月16,6月15是星期六,所以只有一个时间段包含周六,返回1
    * 6月16~6月22,不包含任何一个周六,返回0
    * 1月1日~1月7日,1月5日是周六,所以时间段可以是:
    * 1月1~1月7日,1月1~1月6日,1月2~1月7日,1月2~1月6日,1月3~1月7日,1月3~1月6日,1月4~1月7日,1月4~1月6日,1月5~1月7日,1月5~1月6日
    * 返回10

    如果感觉题目还是有点不清楚,可以看末尾的原题目(很详细)

    解题核心是:

    在包含一个周六晚上的情况下,固定开始时间,逐渐减小结束时间,直到不包含周六晚上;

    然后增大固定时间,再逐渐减小结束时间,直到不包含周六晚上;

    如1月1~1月31,1月1是周二,固定开始1月1,减小结束时间1月31,有1月1~1月31, 1月1~1月30, 1月1~1月29 ...... 1月1~1月6(周日)

    接着1月2~1月31,1月2是周三,固定开始1月2,减小结束时间1月31,有1月2~1月31, 1月2~1月30, 1月2~1月29 ...... 1月2~1月6(周日)

    可以把上面总结为如下代码循环中的sum次数累加的方式

    ......

    记录日期段次数。

    /**
     * @author: rhyme
     * @date: 2019-09-27 17:22
     * @topic: "笔试题"
     * @description: "输入2019年的一个时间段,开始时间代表出发,结束时间代表在那一天返回,判断在该时间段内,如果旅行有多少个子时间段可以在周六晚上休息"
     * 周六晚上休息,即子时间段必须包含周六,且唯一一个周六不能是最后一天
     * 例如6月14~6月15,6月15虽然是星期六,但是返回时间是周六,则不能在周六休息,返回0
     * 6月15~6月16,6月15是星期六,所以只有一个时间段包含周六,返回1
     * 6月16~6月22,不包含任何一个周六,返回0
     * 1月1日~1月7日,1月5日是周六,所以时间段可以是:
     * 1月1~1月7日,1月1~1月6日,1月2~1月7日,1月2~1月6日,1月3~1月7日,1月3~1月6日,1月4~1月7日,1月4~1月6日,1月5~1月7日,1月5~1月6日
     * 返回10
     */
    public class SaturdayNightStay {
    
    
        public int countOptions(int firstDay, int firstMonth, int lastDay, int lastMonth) {
            final int YEAR = 2019;
            // 使用Java8 LocalDate
            LocalDate startLocalDate = LocalDate.of(YEAR, firstMonth, firstDay);
            long start = startLocalDate.toEpochDay();
    
            LocalDate endLocalDate = LocalDate.of(YEAR, lastMonth, lastDay);
            long end = endLocalDate.toEpochDay();
    
            // 参数不合法
            if (start >= end) {
                return 0;
            }
    
            int startDayOfWeek = startLocalDate.getDayOfWeek().getValue();
            final int SATURDAY = 6;
    
            // 第一天与第一个周六相差多少
            int durningDay;
            if (startDayOfWeek <= SATURDAY){
                durningDay = SATURDAY - startDayOfWeek;
            }else {
                durningDay = 6;
            }
    
            long firstSaturday = start + durningDay;
            long tempSaturday = firstSaturday;
    
            long sum = 0;
            while (tempSaturday < end) {
                if (tempSaturday == firstSaturday) {
                    // 第一个周六
                    sum += (firstSaturday - start + 1) * (end - firstSaturday);
                } else {
                    sum += 7 * (end - tempSaturday);
                }
    
                // 获得下一个周六
                tempSaturday += 7;
            }
    
            return Long.valueOf(sum).intValue();
        }
    
        @Test
        public void test() {
            //        1
            System.out.println(countOptions(15, 6, 16, 6));
            //        0
            System.out.println(countOptions(16, 6, 22, 6));
            //        382
            System.out.println(countOptions(1, 1, 31, 1));
            //        2485
            System.out.println(countOptions(7, 8, 19, 10));
            //        10
            System.out.println(countOptions(1, 1, 7, 1));
            //        0
            System.out.println(countOptions(14, 6, 15, 6));
        }
    }

    原题目

    (85%) SaturdayNightStay 

    Problem Statement

        

    Basha promised her friend Mel that she will come for a visit somewhere between the firstDay of the firstMonth and the lastDay of the lastMonth in 2019. Basha and Mel live in countries separated by the sea, so Basha has to buy plane tickets.

     

    Airlines are using many tricks when pricing their tickets. One of the important ones is the "Saturday night stay" concept: if your stay at the destination includes the night from a Saturday to a Sunday, you are unlikely to be a business traveler and thus they will give you a lower price for your tickets. Basha would like to have cheap plane tickets (who wouldn't), so she wants to plan her trip in such a way that it will include at least one Saturday night.

     

    To summarize:

     

    Basha's day of arrival must be on the firstDay of the firstMonth 2019 or later.

    Basha's day of departure must be on the lastDay of the lastMonth 2019 or earlier.

    Basha must stay at Mel's place for at least one Saturday night.

    Two trip schedules are considered different if Basha arrives and/or departs on a different day. Count all possible trip schedules, and return the answer.

     

     

    Definition

        

    Class: SaturdayNightStay

    Method: countOptions

    Parameters: int, int, int, int

    Returns: int

    Method signature: int countOptions(int firstDay, int firstMonth, int lastDay, int lastMonth)

    (be sure your method is public)

        

     

    Notes

    - The year 2019 is not a leap year. The number of days in the individual months of 2019 is 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, and 31.

    - The 1st of January 2019 was a Tuesday.

    - As explained in the statement, "Saturday night" is short for "the night that starts on a Saturday and ends on a Sunday".

     

    Constraints

    - firstMonth and lastMonth will each be between 1 and 12, inclusive.

    - firstDay will be between 1 and the number of days in firstMonth, inclusive.

    - lastDay will be between 1 and the number of days in lastMonth, inclusive.

    - The firstDay of the firstMonth will not be later in 2019 than the lastDay of the lastMonth.

     

    Unit Tests (Must be written in JUnit)

    0)

        

    15

    6

    16

    6

    Returns: 1

    The earliest day on which Basha can arrive is today: the 15th of June. This is a Saturday.

     

    The latest day on which she can leave is the 16th of June: the Sunday tomorrow.

     

    In order to spend the Saturday night in her destination, Basha must arrive on the 15th and depart on the 16th.

     

    1)

        

    16

    6

    22

    6

    Returns: 0

    Regardless of when she arrives and departs, her trip will not include any Saturday nights.

     

    Note that the trip which arrives on the 16th and departs on the 22nd includes both a Saturday (the 22nd) and a Sunday (the 16th) but that is not enough: the trip does not contain any Saturday night.

     

    2)

        

    1

    1

    31

    1

    Returns: 382

    There are many viable options in January 2019, including the longest one: arriving on the 1st and departing on the 31st of January. (This trip contains four different Saturday nights. Additional Saturday nights do not matter, the only requirement is that there has to be at least one of them.)

    3)

        

    7

    8

    19

    10

    Returns: 2485

  • 相关阅读:
    Hacker(22)----解除系统中的密码
    Hacker(21)----密码攻防之加密与解密基础
    Hacker(20)----手动修复Windows系统漏洞
    Hacker(19)----检测Windows系统漏洞
    Hacker(18)----了解Windows系统漏洞
    Hacker(17)----认识Windows系统漏洞
    Linux通过nfs挂载根文件系统失败:VFS: Unable to mount root fs via NFS, trying floppy.
    恶补各种知识(编程基础篇)
    恶补各种知识(操作系统篇)
    恶补各种知识(查找排序篇)
  • 原文地址:https://www.cnblogs.com/theRhyme/p/11600367.html
Copyright © 2011-2022 走看看