troy_mccormick Posted July 24, 2008 Share Posted July 24, 2008 Hey Everyone, I'm trying to work this out in my head and I need your help because I stink at math! Here is what I am trying to do: Say I have ten tables with ten people at each table. How would you program something so that as people leave, the tables are kept as close as possible to the same number of people while eliminating tables when possible? This is what I've come up with so far...but I don't think it will help with the eliminating of tables portion: <?php $num_people = 100; $num_tables = 10; $avg_num_people = floor($num_people / $num_tables); $needs_people = array(); $loses_people = array(); foreach($tables AS $table) { if ($table["num_people"] < $avg_people) { $num_people_needed = $avg_people - $table["num_people"]; $needs_people[$table] = $num_people_needed; } elseif ($table["num_people"] > $avg_people) { $num_people_lose = $table["num_people"] - $avg_people; $loses_people[$table] = $num_people_lose; } } ?> Anyone help a crappy math guy out?!?! Thanks!! Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 24, 2008 Share Posted July 24, 2008 there are probably a hundred ways to do this, but here's my opinion: track the number of people at each table. as the table is at a deficiency of two, take the table with the largest number of people, and send one over. after each person-transfer, check the number of people missing and the number of people remaining; once the least-occupied table matches the number of empty slots at others, redistribute at random so that you end up with full tables once again. here's some pseudo-code: // number of tables // number of people // calculate starting spots at each table // trigger the departure of a person // drop one from the table where he was sitting // check if the starting spots minus the spots still occupied at that table is 2 // if so, move one guy from the table with the most spots to the one the guy just left // grab the number of people at the least occupied table // grab the total number of empty spots at all OTHER tables // if they match, redistribute the least occupied table and drop it from the list does that make sense? you'll need to re-organize your $tables array so that you can sort by number of spots occupied. perhaps a structure like this: $tables['num_people'][TABLE_ID] => value; would work. Quote Link to comment Share on other sites More sharing options...
troy_mccormick Posted July 24, 2008 Author Share Posted July 24, 2008 // number of tables // number of people // calculate starting spots at each table // trigger the departure of a person // drop one from the table where he was sitting // check if the starting spots minus the spots still occupied at that table is 2 // if so, move one guy from the table with the most spots to the one the guy just left // grab the number of people at the least occupied table // grab the total number of empty spots at all OTHER tables // if they match, redistribute the least occupied table and drop it from the list Brilliant!!! Thank you very much!! Sometimes I can be a total dunce Thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.