"输入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() {
//
System.out.println(countOptions(15, 6, 16, 6));
//
System.out.println(countOptions(16, 6, 22, 6));
//
System.out.println(countOptions(1, 1, 31, 1));
//
System.out.println(countOptions(7, 8, 19, 10));
//
System.out.println(countOptions(1, 1, 7, 1));
//
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

编程题:SaturdayNightStay的更多相关文章

  1. 算法是什么我记不住,But i do it my way. 解一道滴滴出行秋招编程题。

    只因在今日头条刷到一篇文章,我就这样伤害我自己,手贱. 刷头条看到一篇文章写的滴滴出行2017秋招编程题,后来发现原文在这里http://www.cnblogs.com/SHERO-Vae/p/588 ...

  2. C算法编程题系列

    我的编程开始(C) C算法编程题(一)扑克牌发牌 C算法编程题(二)正螺旋 C算法编程题(三)画表格 C算法编程题(四)上三角 C算法编程题(五)“E”的变换 C算法编程题(六)串的处理 C算法编程题 ...

  3. C算法编程题(七)购物

    前言 上一篇<C算法编程题(六)串的处理> 有些朋友看过我写的这个算法编程题系列,都说你写的不是什么算法,也不是什么C++,大家也给我提出用一些C++特性去实现问题更方便些,在这里谢谢大家 ...

  4. C算法编程题(六)串的处理

    前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...

  5. C算法编程题(五)“E”的变换

    前言 上一篇<C算法编程题(四)上三角> 插几句话,说说最近自己的状态,人家都说程序员经常失眠什么的,但是这几个月来,我从没有失眠过,当然是过了分手那段时期.每天的工作很忙,一个任务接一个 ...

  6. C算法编程题(四)上三角

    前言 上一篇<C算法编程题(三)画表格> 上几篇说的都是根据要求输出一些字符.图案等,今天就再说一个“上三角”,有点类似于第二篇说的正螺旋,输出的字符少了,但是逻辑稍微复杂了点. 程序描述 ...

  7. C算法编程题(三)画表格

    前言 上一篇<C算法编程题(二)正螺旋> 写东西前还是喜欢吐槽点东西,要不然写的真还没意思,一直的想法是在博客园把自己上学和工作时候整理的东西写出来和大家分享,就像前面写的<T-Sq ...

  8. C算法编程题(二)正螺旋

    前言 上一篇<C算法编程题(一)扑克牌发牌> 写东西前总是喜欢吐槽一些东西,还是多啰嗦几句吧,早上看了一篇博文<谈谈外企涨工资那些事>,里面楼主讲到外企公司包含的五类人,其实不 ...

  9. C算法编程题(一)扑克牌发牌

    前言 上周写<我的编程开始(C)>这篇文章的时候,说过有时间的话会写些算法编程的题目,可能是这两天周末过的太舒适了,忘记写了.下班了,还没回去,闲来无事就写下吧. 因为写C++的编程题和其 ...

  10. C语言程序设计进阶 第1周编程题

    第1周编程题 查看帮助 返回 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数 ...

随机推荐

  1. 【操作系统之十二】分支预测、CPU亲和性(affinity)

    一.分支预测 当包含流水线技术的处理器处理分支指令时就会遇到一个问题,根据判定条件的真/假的不同,有可能会产生转跳,而这会打断流水线中指令的处理,因为处理器无法确定该指令的下一条指令,直到分支执行完毕 ...

  2. scala 项目pom示例

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  3. Maven 教程(16)— pom.xml 文件详解

    原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79733577 <project xmlns="http://ma ...

  4. Symbol 小妙处

    input 框输入后发送异步请求,页面拿到响应进行渲染.但偶尔会遇到问题:响应内容和输入结果不一致.因为 http 无法保证响应到达的顺序. 如何解决呢?提供一个小思路. myRequest.js i ...

  5. [转帖]如何获得一个RAC Oracle数据库(从Github - oracle/docker-images) - 本地版 ---暂时未做实验.

    如何获得一个RAC Oracle数据库(从Github - oracle/docker-images) - 本地版 2019-11-09 16:35:30 dingdingfish 阅读数 32更多 ...

  6. 使用benchmarkSQL测试数据库的TPCC

    压力测试是指在MySQL上线前,需要进行大量的压力测试,从而达到交付的标准.压力测试不仅可以测试MySQL服务的稳定性,还可以测试出MySQL和系统的瓶颈. TPCC测试:Transaction Pr ...

  7. mysql中,手动提交事务

    1: 在mysql中,手动提交事务的案例:CREATE PROCEDURE tfer_funds       (from_account int, to_account int, tfer_amoun ...

  8. php中命名空间namespace和use

    对于面向对象编程而言,命名空间namespace和use的概念非常重要. 1.根命名空间是反斜线 \ ,有点类似linux中的根目录 / 的那种感觉,但使用var_dump()函数打印时其实是空字符串 ...

  9. Python基础笔记(四)

    1. 返回函数与闭包 如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure) def getSum(*args): def add(): ...

  10. Python 文件编码问题解决

    最近使用python操作文件,经常遇到编码错误的问题,例如: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbe in position ...