我正在尝试执行以下操作: 对于考试,我有x个椅子和x个学生。椅子可能会变得多样化,每个班级和每年的人都会变得多样化。
示例:有112把椅子和102名学生。我需要填充椅子,以便同年的学生不会彼此相邻。
椅子并不总是排成一排,所以这些地方也可能不同。
我做了什么:
现在我需要用最大的年龄来填补最大的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
}
我希望你理解我的意思......如果我需要在开始时重新考虑整个伪代码,请提供一些提示。
答案 0 :(得分:1)
简而言之,如果我是正确的:
你试图完成的是一个椅子网格(无论网格是否大小相同),根据课程分配学生,所以同一个班级不是彼此相邻。
在这种情况下,主席号码/姓名,班级号码和考试都是“元数据”,不会为真正的问题添加任何内容。
所以,我首先要设计一个椅子网格,在那里必须可以挡住椅子(例如,指定“不可用”)。
因为代码比单词更好:
$grid = new ChairGrid(6, 18);
// set non-available chairs
$grid->get(2,5)->assigned = 'NA';
$grid->get(1,10)->assigned = 'NA';
// change names as you wish
$grid->get(0,12)->name = 'BG1';
print($grid);
$pupils = array(
1 => 20,
2 => 30,
3 => 20,
4 => 20,
5 => 20
);
// returns left-overs
var_dump($grid->assign($pupils));
print($grid);
Chargrid班:
class ChairGrid {
private $sizeX;
private $sizeY;
private $grid;
public function __construct($sizeX, $sizeY) {
$this->grid = array();
$this->sizeX = $sizeX;
$this->sizeY = $sizeY;
for($x = 0; $x < $sizeX; $x++) {
$this->grid[$x] = array();
for($y = 0; $y < $sizeY; $y++) {
$chair = new Chair(chr(ord('A') + $y) . $x, $x, $y);
$this->grid[$x][$y] = $chair;
}
}
}
/**
* @param $x
* @param $y
*
* @return Chair
*/
public function get($x, $y) {
if (isset($this->grid[$x][$y])) {
return $this->grid[$x][$y];
}
return null;
}
public function assign($pupils) {
$retval = array();
foreach($pupils as $class=>$number) {
$retval[$class] = 0;
$column = 0;
$row = -1;
for($i=0; $i < $number;$i++) {
$done = false;
while(!$done) {
$row++;
if ($row >= $this->sizeY) {
$row = 0;
$column++;
if ($column >= $this->sizeX) {
$retval[$class]++;
break;
}
}
$chair = $this->get($column, $row);
if ($chair instanceof Chair && $chair->assigned === null) {
// var_dump($chair->getX() . ',' . $chair->getY() . ' --?--> '.$class);
$chairLeft = $this->get($chair->x - 1, $chair->y);
$chairRight = $this->get($chair->x + 1, $chair->y);
if ($chairRight === null) {
$chairFirst = $this->get(0, $chair->y);
if ($chairLeft === null || $chairLeft->assigned !== $class) {
// assign
$chair->assigned = $class;
$done = true;
}
else if ($chairFirst->assigned !== $class) {
$prev = null;
for($moveColumn = $this->sizeX - 1; $moveColumn >= 0; $moveColumn--) {
$current = $this->get($moveColumn, $row);
if ($current->assigned !== 'NA') {
if ($prev instanceof Chair) {
$prev->assigned = $current->assigned;
}
$prev = $current;
}
}
// we cannot assign, so check "right" in first column, if we can assign, shift column
$chairFirst->assigned = $class;
$done = true;
}
}
else if (($chairLeft === null || $chairLeft->assigned !== $class) && ($chairRight === null || $chairRight->assigned !== $class)) {
$chair->assigned = $class;
$done = true;
}
}
}
}
}
return $retval;
}
public function __toString() {
$string = array();
for($y = 0; $y < $this->sizeY; $y++) {
$tmp = '';
for($x = 0; $x < $this->sizeX; $x++) {
$tmp .= '[' . str_pad($this->grid[$x][$y]->name, 3, ' ', STR_PAD_RIGHT) . '|' . str_pad($this->grid[$x][$y]->assigned, 3, ' ', STR_PAD_RIGHT) . ']';
}
$string[] = $tmp;
}
return implode("\n", $string) . "\n";
}
}
简易椅类:
class Chair {
public $name;
public $assigned = null;
public $x;
public $y;
public function __construct($name, $x, $y) {
$this->name = $name;
$this->x = $x;
$this->y = $y;
}
}
答案 1 :(得分:1)
我用一些随机数据制作了这段代码来测试它。您可以使用fill_seats函数并在其中添加正确的参数。
$chairs1 = range(1, 15); //Make 5 arrays of chairs
$chairs2 = range(16, 40);
$chairs3 = range(41, 60);
$chairs4 = range(61, 68);
$chairs5 = range(69, 100);
$chairs = array($chairs1,$chairs2,$chairs3,$chairs4,$chairs5); // add them to 1 array as subarrays
// Fill 5 classes with students
for($i = 1;$i <= 10; $i++){
$year3[] = '3Wb-English-' . $i;
if($i<8){
$year2[] = '2Wb-English-' . $i;
}
if($i<7){
$year1[] = '1Wb-English-' . $i;
}
if($i<9){
$year5[] = '5Wb-English-' . $i;
}
if($i<6){
$year4[] = '4Wb-English-' . $i;
}
}
$years = array($year1,$year2,$year3,$year4,$year5); // same as chairs
//sort the array according to the number of items in the array (biggest first)
usort($years, 'sort_by_count');
usort($chairs, 'sort_by_count');
// Remove some chairs from all of them
$all_chairs = range(1,100);
unset($all_chairs[5]);
unset($all_chairs[16]);
unset($all_chairs[44]);
unset($all_chairs[65]);
unset($all_chairs[37]);
// loop over them and fill in the seats (you can do this now because arrays are already sorted by count)
foreach($years as $k => $y){
$filled_seats[] = fill_seats($chairs[$k],$y,$all_chairs);
}
function fill_seats($chairs, $students, $all_chairs){
$i = 0;
foreach($chairs as $chair){
// Seat not available
if(!in_array($chair,$all_chairs)){
$filled_chairs[$chair] = 'N/A';
continue;
}
// Still some students left
if(isset($students[$i])){
$filled_chairs[$chair] = $students[$i];
// No students left, leave them empty
}else{
$filled_chairs[$chair] = '';
}
$i++;
}
return $filled_chairs;
}
function sort_by_count($a, $b) {
$a = count($a);
$b = count($b);
return ($a == $b) ? 0 : (($a > $b) ? -1 : 1);
}
结果print_r($ filled_seats):
Array
(
[0] => Array
(
[69] => 3Wb-English-1
[70] => 3Wb-English-2
[71] => 3Wb-English-3
[72] => 3Wb-English-4
[73] => 3Wb-English-5
[74] => 3Wb-English-6
[75] => 3Wb-English-7
[76] => 3Wb-English-8
[77] => 3Wb-English-9
[78] => 3Wb-English-10
[79] =>
[80] =>
[81] =>
[82] =>
[83] =>
[84] =>
[85] =>
[86] =>
[87] =>
[88] =>
[89] =>
[90] =>
[91] =>
[92] =>
[93] =>
[94] =>
[95] =>
[96] =>
[97] =>
[98] =>
[99] =>
[100] =>
)
[1] => Array
(
[16] => 5Wb-English-1
[17] => N/A
[18] => 5Wb-English-2
[19] => 5Wb-English-3
[20] => 5Wb-English-4
[21] => 5Wb-English-5
[22] => 5Wb-English-6
[23] => 5Wb-English-7
[24] => 5Wb-English-8
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] => N/A
[39] =>
[40] =>
)
[2] => Array
(
[41] => 2Wb-English-1
[42] => 2Wb-English-2
[43] => 2Wb-English-3
[44] => 2Wb-English-4
[45] => N/A
[46] => 2Wb-English-5
[47] => 2Wb-English-6
[48] => 2Wb-English-7
[49] =>
[50] =>
[51] =>
[52] =>
[53] =>
[54] =>
[55] =>
[56] =>
[57] =>
[58] =>
[59] =>
[60] =>
)
[3] => Array
(
[1] => 1Wb-English-1
[2] => 1Wb-English-2
[3] => 1Wb-English-3
[4] => 1Wb-English-4
[5] => 1Wb-English-5
[6] => N/A
[7] => 1Wb-English-6
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
)
[4] => Array
(
[61] => 4Wb-English-1
[62] => 4Wb-English-2
[63] => 4Wb-English-3
[64] => 4Wb-English-4
[65] => 4Wb-English-5
[66] => N/A
[67] =>
[68] =>
)
)