何一涛 2010-10-19 23:47
浏览 519
已采纳

一个让人头疼的算法题-如何安排会议

题目:有20个会议室可供使用,使用者需在前一天提交申请,注明时间段比如(8,9)意为要求在8点-9点借用会议室,当然申请的人很多,时间段也不尽相同,管理员为了这事每天晚上都忙得焦头烂额。所以要聪明的你写一个算法帮助管理员解决这个问题,使会议室的安排得最合理?

  • 写回答

3条回答 默认 最新

  • ayling520 2010-10-20 11:06
    关注

    [code="java"]
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;

    public class ConferenceManager
    {
    /**
    * 会议室使用记录
    */
    public static final Map> conMap = new HashMap>();

    /**
     * 会议室
     */
    public static final Map<Integer, int[]> timeMap = new HashMap<Integer, int[]>();
    
    // 初始化会议室
    static
    {
        for (int i = 0; i < 20; i++)
        {
            // 20间会议室,都为null
            conMap.put(i, null);
    
            // 每间会议室的时间段都为0,表示没人使用
            timeMap.put(i, new int[24]);
        }
    }
    
    /**
     * 订会议室
     * 
     * @param name
     *            使用人
     * @param startTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @return 订的会议室信息,如果没有会议室返回null
     */
    public static Conference bespoke(String name, Integer startTime,
            Integer endTime)
    {
        // 参数验证
        if (startTime > 24 || startTime < 0 || endTime > 24 || endTime < 0
                || startTime >= endTime)
        {
            throw new IllegalArgumentException();
        }
    
        Conference con = null;
    
        // 每个会议室的时间段遍历
        for (final Entry<Integer, int[]> entrys : timeMap.entrySet())
        {
            // 该时间段是否有人使用,0无人使用,1有人使用
            int temp = 0;
            int[] dayTime = entrys.getValue();
    
            // 判断一段时间内是否有人使用会议室
            for (int i = startTime; i < endTime; i++)
            {
                if (dayTime[i] == 1)
                {
                    temp = 1;
                    break;
                }
            }
    
            if (temp == 1)
            {
                continue;
            }
    
            // 将会议室的时间段标识为有人使用
            for (int i = startTime; i < endTime; i++)
            {
                dayTime[i] = 1;
            }
            con = new Conference();
            con.setName(name);
            con.setDivan(entrys.getKey());
            con.setStartTime(startTime);
            con.setEndTime(endTime);
    
            List<Conference> conList = conMap.get(entrys.getKey());
    
            if (conList == null)
            {
                conList = new ArrayList<Conference>();
                conMap.put(entrys.getKey(), conList);
            }
    
            // 添加会议室使用记录
            conList.add(con);
            return con;
        }
        return con;
    }
    
    public static void main(String[] args)
    {
    
        // 测试代码
        Conference con = bespoke("pengwenchao", 8, 9);
    
        for (int i = 0; i < 20; i++)
        {
            con = bespoke("pengwenchao", 0, 23);
        }
    
        con = bespoke("pengwenchao11", 10, 12);
    
    
        con = bespoke("pengwenchao11", 9, 10);
    
        // 打印会议室的使用
        System.out.println(conMap);
    }
    

    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误