首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >表示和比较时隙

表示和比较时隙
EN

Stack Overflow用户
提问于 2014-11-20 17:57:35
回答 4查看 3K关注 0票数 0

我需要为预订餐厅的餐桌储存时隙,然后看看是否有碰撞.

代码语言:javascript
运行
复制
For example - Total tables - 4 
1) 9 - 11 , 3 tables   
2) 9 - 10 , 1 tables (Need to do search if any table left 
                       with constraint to above booking)

我如何存储时隙和桌子并与其他人进行比较.

我应该使用什么数据结构..。

如果我使用HashMap,关键和价值是什么,

我设计了所有其他类和方法,但无法找到解决时隙碰撞问题的方法。

代码语言:javascript
运行
复制
collision example - 

total - 4 tables

1) 9-10 , 3 tables

2) 9-11 , 1 table

3) 9-12 , 2 tables  (collision , table not available)
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-11-20 18:11:35

您可以简化这个问题,方法是将可用的时间划分为15分钟的块(或其他适合您的块大小)。对于预订的餐厅,我打赌15分钟的街区是可以的。

然后,您可以有一个简单的int[],它存储每个时隙的预定表数。

例句:你的餐厅从上午9点开始营业。到晚上9点,所以12个小时每个有4个时隙。所以你需要一个有48个插槽的int[]。现在,当你预订3张9点到11点的桌子时,你会把前8张(9点到11点)增加3张。第二张预订会把前4张增加1张。如果预订超过了可用的时间限制,你就知道你需要拒绝它。

代码语言:javascript
运行
复制
final int MAX_TABLES = 4;
final int OPENING= 9;
final int CLOSING= 21;
final int SLOTS= 4;
int[] booking = new int[(CLOSING - OPENING) * SLOTS];

public void main() {
    // no tables booked
    Arrays.fill(booking,0);

    doBooking(3, 0, 8);
    doBooking(1, 4, 8);
    doBooking(1, 4, 12);
}

public void doBooking(int tables, int startSlot, int endSlot) {
    for (int slot= startSlot, slot < endSlot, slot++) {
        if (booking[slot] + tables > MAX_TABLES) {
            throw new Exception("no free table at slot "+slot);
        }
    }
    for (int slot= startSlot, slot < endSlot, slot++) {
        booking[slot] += tables;
    }
}

这应该能给你一个主意。还有一些事情要做,例如正确的异常处理、从时间转换到插槽等。还要注意,这可能是不正确的java代码,因为我没有测试它,也没有用GUI编写它。

票数 3
EN

Stack Overflow用户

发布于 2019-09-26 18:55:45

更多例子:)

我也有同样的问题。

我用接口插槽创建时隙,并在应用程序中使用它。

例如:

代码语言:javascript
运行
复制
Schedule schedule = new Schedule(myduration, unit);
schedule.add(date1, slot1);
schedule.add(date2, slot2);

schedule.get(date1, slot1);
schedule.indexOf(date1, slot1);
schedule.remove(date1, slot1);
schedule.set(date1, index, slot1);

github https://github.com/illineya/schedule中的更多细节

票数 1
EN

Stack Overflow用户

发布于 2014-11-20 18:13:59

对于时间范围(使其为时间戳或15分钟间隔),请使用番石榴范围 --它有很好的工具可以处理范围(冲突)。

从现在到将来的开放间隔开始-所有表都可用。新预订->检查整个间隔是否有空表。如果在+ this + ->之前将原始间隔中断为三次

重复..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27046368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档