0

我有一个像人一样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();
    }
}
4

2 回答 2

1

您可以使用LINQ来计算每个的当前总和List

int _maxSlotA = 180;
int _maxSlotB = 240;

List<int> _slotA = new List<int>();
List<int> _slotB = new List<int>();

foreach(int i in list)
{
    if (_slotA.Sum() < _maxSlotA)
    {
        _slotA.Add(i);
    }
    elseif (_slotB.Sum() < _maxSlotB)
    {
        _slotB.Add(i);
    }
}
于 2013-03-31T05:59:18.563 回答
1

输入样本 I: xPeople -- 60,45,45,45,45,30,60,60,15

//OutPut
Slot I: [60, 45, 45, 30]
Slot II: [45, 45, 60, 60]
Un-Allocated:[0, 0, 0, 0, 0, 0, 0, 0, 15]

输入样本二: xPeople -- 75,45,45,45,45,30,60,60,15

//OutPut
Slot I: [75, 45, 45, 15]
Slot II: [45, 45, 30, 60, 60]
Un-Allocated:[0, 0, 0, 0, 0, 0, 0, 0, 0]

输入样本 III: xPeople -- 128,11,8,69,6,76,41,54,5,4,2,3,2,100

//OutPut
Slot I: [128, 11, 8, 33]
Slot II: [36, 6, 76, 41, 54, 5, 4, 2, 3, 2, 11]
Un-Allocated:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89]

这段代码是用 测试IDE过的,我可能在很多情况下都测试过,如果你发现有任何问题,请在我的帖子中发表评论。

import java.util.ArrayList;

public class Test1 {

    public static void main(String args[])
    {

        ArrayList<Integer> xPeople=new ArrayList<Integer>();
        xPeople.add(60); xPeople.add(45); xPeople.add(45);
        xPeople.add(45); xPeople.add(45); xPeople.add(30);
        xPeople.add(60); xPeople.add(60); xPeople.add(15);

        ArrayList<Integer> xSlotOne=new ArrayList<Integer>();
        ArrayList<Integer> xSlotTwo=new ArrayList<Integer>();

        int xSlotOneCnt=0;
        int xSlotTwoCnt=0;

        for(int i=0; i<xPeople.size();i++) 
        {

            if(xSlotOneCnt<180)
            {
                if (xSlotOneCnt + xPeople.get(i) >180)
                {
                    if(xPeople.indexOf(180 - xSlotOneCnt) != -1)
                    {
                        xSlotOne.add(xPeople.get(xPeople.indexOf(180 - xSlotOneCnt)));
                        xPeople.set(xPeople.indexOf(180 - xSlotOneCnt),0);
                    }
                    else
                    {
                        xSlotOne.add(180 - xSlotOneCnt);
                        xPeople.set(i,  xPeople.get(i) - (180 - xSlotOneCnt));
                    }

                    xSlotOneCnt += 180 - xSlotOneCnt;
                }
                else
                {
                    xSlotOne.add(xPeople.get(i));
                    xSlotOneCnt += xPeople.get(i);

                    xPeople.set(i, 0);
                }
            }

                          //The code inside this if statement is as same the
                          //code which is inside the above If statement[if(xSlotOneCnt<180)]
                          //So please use a function in this case to avoid code repetetion.

            if(xSlotTwoCnt < 240 && xPeople.get(i) > 0)
            {

                                if (xSlotTwoCnt + xPeople.get(i) >240)
                {
                    if(xPeople.indexOf(240 - xSlotTwoCnt) != -1)
                    {
                        xSlotTwo.add(xPeople.get(xPeople.indexOf(240 - xSlotTwoCnt)));
                        xPeople.set(xPeople.indexOf(240 - xSlotTwoCnt),0);
                    }
                    else
                    {
                        xSlotTwo.add(240 - xSlotTwoCnt);
                        xPeople.set(i,  xPeople.get(i) - (240 - xSlotTwoCnt));
                    }

                    xSlotTwoCnt += 240 - xSlotTwoCnt;
                }
                else
                {
                    xSlotTwo.add(xPeople.get(i));
                    xSlotTwoCnt += xPeople.get(i);
                    xPeople.set(i, 0);
                }
            }

        }

        System.out.println("Slot I: " + xSlotOne);
        System.out.println("Slot II: " + xSlotTwo);
        System.out.println("Un-Allocated:" + xPeople);

    }

}
于 2013-03-31T09:37:11.287 回答