【发布时间】:2015-08-30 16:44:45
【问题描述】:
我正在尝试执行以下操作: 对于考试,我有 x 个椅子和 x 个学生。椅子可能会有所不同,人员可能会因班级和年份而异。
示例:有 112 把椅子和 102 名学生。我需要把椅子坐满,这样同年的学生就不会挨着了。
椅子并不总是排成一排,所以位置也可能不同。
我做了什么:
- 我有两个包含椅子列的数组:$chairs1: A, D, F,... 和 $chairs2=B, E, G,... (我已经过滤了 C 行,对于示例)这样,我可以将第三年放在数组 1 中,将第四年放在数组 2 中。这样,第一年的学生不会彼此相邻
- 我有一个数组,其中包含每年的学生人数。所以 $numberofpupilsperyear[1] = 第一年的学生人数。
- 我有 7 个其他数组,每年一个,其中包含以下文本:$class-$examn-$classnumber。仅供参考:3Wb-English-1、3Wb-English-2、3Wb-English-3、...
- 所有椅子的编号从 A1 到 R19。但并非所有椅子都存在于每个地方。所以我有一个带有现有椅子的数组 $allchairs[] 在那个特定的房间里检查椅子是否存在。
现在我需要用最大的 yeararray 填充最大的 chairarray。 因此,如果 $chairs1 有 80 张椅子,并且第 2 年的学生最多,则应从 $chairs1 开始填充,从第 2 年的学生开始,每年的学生人数第二多。
到目前为止我所拥有的,加上以上所有。
if($chairs1 >= $chairs2)
{
//fill chairs1
$currentnumber = 1;
$counter=0;
while ($currentnumber < 20)
{
foreach($chairs1 as $key => $value)
{
//make coordinate of the chair
$currentletter = $value;
//check if chair exists
if(in_array($currentletter.$currentnumber, $allchairs))
{
//Fill this place with a pupil like for example: 3Wb-English-1
//But I'm a bit stuck here...
//I was trying with the following
$biggestyear = $number_of_students[$counter];
echo "biggestyear: ".$biggestyear;
${$classlist.$biggestyear};
}
}
$currentnumber++;
}
//now fill chairs2
else
{
//fill chairs2 first and then chairs1
}
我希望你明白我的意思...如果我需要从头重新考虑整个伪代码,请提供一些提示。
【问题讨论】:
标签: php arrays loops foreach while-loop