gpt4 book ai didi

c# - 在循环中迭代项目

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:29 25 4
gpt4 key购买 nike

我有一个 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,我需要创建另一个插槽并添加剩余的。

我正在为这个逻辑而苦苦挣扎。欢迎任何算法/方法!

注意:

1st slot is always 180 2nd slot can be 0-180 or maximum 240

If the list has more items we schedule it for next day, by creating slot1 & slot 2 again for Day 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();
}
}

最佳答案

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

}

}

关于c# - 在循环中迭代项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15726734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com