Jump to content

[SOLVED] Possible Math Related Question


troy_mccormick

Recommended Posts

Hey Everyone,

 

I'm trying to work this out in my head and I need your help because I stink at math!  :D

 

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!!  :D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

// 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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.