题目:有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]本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 ios 新安装app收不到fcm推送
- ¥15 有没有实力的写手?有过成品的优先
- ¥15 图像信息库的建立与识别
- ¥15 韩国网站购物,KG支付的支付回调如何解决
- ¥15 workstation导入ovf文件,报错,怎么解决呢?
- ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
- ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
- ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
- ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
- ¥15 应用商店如何检测在架应用内容是否违规?