迭代循环中的项目

时间:2013-03-31 05:44:58

标签: c# java javascript algorithm language-agnostic

我有一个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

     

如果列表中有更多项目,我们会在第二天通过创建来安排   slot1&amp;第2天再次插入第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();
    }
}

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分: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]

输入样本II: 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);

    }

}