题目:有20个会议室可供使用,使用者需在前一天提交申请,注明时间段比如(8,9)意为要求在8点-9点借用会议室,当然申请的人很多,时间段也不尽相同,管理员为了这事每天晚上都忙得焦头烂额。所以要聪明的你写一个算法帮助管理员解决这个问题,使会议室的安排得最合理?
[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]
你这个好像是设计,设计好了数据结构就完成了。
就好像是电影院放映电影一样,什么时间放什么电影,这个电影要在什么时间放,放多长时间,然后下一个电影什么时间放。
你的时间段就好像电影开始和结束的时间。
不难设计。
[code="java"]
public class Conference
{
/**
* 使用人
*/
private String name;
/**
* 开始时间
*/
private Integer startTime;
/**
* 结束时间
*/
private Integer endTime;
/**
* 会议室号码
*/
private Integer divan;
public Integer getDivan()
{
return divan;
}
public void setDivan(Integer divan)
{
this.divan = divan;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Integer getStartTime()
{
return startTime;
}
public void setStartTime(Integer startTime)
{
this.startTime = startTime;
}
public Integer getEndTime()
{
return endTime;
}
public void setEndTime(Integer endTime)
{
this.endTime = endTime;
}
@Override
public String toString()
{
return name + "先生/女士:在" + startTime + "点-" + endTime + "点订了" + divan
+ "号会议室。";
}
}
[/code]