我有一个像人一样List<int>
的价值观60,45,45,45,45,30,60,60,15
我也有两个插槽。第一个插槽可以占用180
人
问题是我需要循环List<int>
以用 180 人填充 Slot1
var slotCount=0;
foreach(int i in list)
{
slot[slotCount]=i;
slotCount++;
//Here till 60+45+45=150 its ok.
//But if i add next item 45, I cross my slot limit(195).
//So i need to pick 30 from list, so it will 180
}
一旦这个插槽被 180 填充,我需要创建另一个插槽并添加剩余的插槽。
我正在为这个逻辑而苦苦挣扎。欢迎任何算法/方法!
笔记:
第一个插槽总是 180 第二个插槽可以是 0-180 或最大 240
如果列表有更多项目,我们将其安排在第二天,通过为第 2 天再次创建 slot1 和 slot 2
这是我尝试但失败的方法:(
class Group
{
public string Name { get; set; }
public int Count { get; set; }
}
class Slot
{
public int MaxSize { get; set; }
public List<Group> Groups { get; set; }
public int OccupiedSize
{
get
{
int count = 0;
foreach (Group g in Groups)
{
count += g.Count;
}
return count;
}
}
}
class Schedule
{
public Slot MorningSlot { get; set; }
public Slot EveningSlot { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Group> groups = new List<Group>{new Group{Count=60},
new Group{Count=45},new Group{Count=45},new Group{Count=45},
new Group{Count=45}, new Group{Count=30},new Group{Count=60},
new Group{Count=60},new Group{Count=15}
};
int eventsCount = groups.Count;
List<Schedule> shedules = new List<Schedule>();
while (eventsCount > 0)
{
Schedule sched = new Schedule();
sched.MorningSlot = new Slot();
sched.MorningSlot.MaxSize = 180;
sched.EveningSlot = new Slot();
sched.EveningSlot.MaxSize = 240;
sched.MorningSlot.Groups = new List<Group>();
sched.EveningSlot.Groups = new List<Group>();
foreach (Group g in groups.ToList())
{
if (sched.MorningSlot.OccupiedSize + g.Count
<= sched.MorningSlot.MaxSize)
{
sched.MorningSlot.Groups.Add(g);
groups.Remove(g);
eventsCount--;
}
else if (sched.EveningSlot.OccupiedSize + g.Count
<= sched.EveningSlot.MaxSize)
{
sched.EveningSlot.Groups.Add(g);
groups.Remove(g);
eventsCount--;
}
}
shedules.Add(sched);
}
Console.ReadLine();
}
}