tags:

views:

192

answers:

1

I'm having a great deal of difficulty trying to figure out the logic behind this problem. I have developed everything else, but I really could use some help, any sort of help, on the part I'm stuck on.

Back story:

*A group of actors waits in a circle. They "count off" by various amounts. The last few to audition are thought to have the best chance of getting the parts and becoming stars.

Instead of actors having names, they are identified by numbers. The "Audition Order" in the table tells, reading left-to-right, the "names" of the actors who will be auditioned in the order they will perform.*

Sample output:

alt text

etc, all the way up to 10.

What I have so far:

using System;
using System.Collections;
using System.Text;

namespace The_Last_Survivor
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare Variables
            int NumOfActors = 0;
            System.DateTime dt = System.DateTime.Now;
            int interval = 3;
            ArrayList Ring = new ArrayList(10);

            //Header
            Console.Out.WriteLine("Actors\tNumber\tOrder");

            //Add Actors
            for (int x = 1; x < 11; x++)
            {
                NumOfActors++;

                Ring.Insert((x - 1), new Actor(x));

                foreach (Actor i in Ring)
                {
                    Console.Out.WriteLine("{0}\t{1}\t{2}", NumOfActors, i, i.Order(interval, x));
                }

                Console.Out.WriteLine("\n");
            }

            Console.In.Read();
        }

        public class Actor
        {
            //Variables
            protected int Number;

            //Constructor
            public Actor(int num)
            {
                Number = num;
            }

            //Order in circle
            public string Order(int inter, int num)
            {
                //Variable
                string result = "";
                ArrayList myArray = new ArrayList(num);

                //Filling Array
                for (int i = 0; i < num; i++)
                    myArray.Add(i + 1);

                //Formula
                foreach (int element in myArray)
                {
                    if (element == inter)
                    {
                        result += String.Format(" {0}", element);
                        myArray.RemoveAt(element);
                    }
                }   
                return result;
            }

            //String override
            public override string ToString()
            {
                return String.Format("{0}", Number);
            }
        }
    }
}

The part I'm stuck on is getting some math going that does this: alt text

Can anyone offer some guidance and/or sample code?

PROGRESS ONE

New code

public string Order(int inter, int num) { //Variable string result = ""; int pos = 0; ArrayList myArray = new ArrayList();

            //Filling Array
            for (int i = 0; i < num + 1; i++)
                myArray.Add(i+1);

            while (myArray.Count > 1) 
            {
                pos = (pos + inter) % myArray.Count;
                result += (myArray[pos] + " ");
                myArray.RemoveAt(pos);
            }


            result += (myArray[0]);
            myArray.Clear();
            return result;

Issue: Actors are off by one: alt text

+2  A: 

The basic idea is you find the next person with the formula

next position = (current position + count) modulo number of people

And every iteration has one fewer persons in it.

Here it is in python. "count" is 2, because we start counting at zero, which makes almost every problem involving modulo a bit simpler.

people=[1,2,3,4,5]
people=['a','b','c','d','e']
count=2  # base 0 counting

pos=0
while len(people) > 1:
    pos = (pos + count) % len(people)
    print "at pos",pos,"eliminating person",people[pos],'from',people,
    del people[pos]
    print 'leaving',people
print 'winner is',people[0]

giving

at pos 2 eliminating person c from ['a','b','c','d','e'] leaving ['a','b','d','e']
at pos 0 eliminating person a from ['a','b','d','e'] leaving ['b','d','e']
at pos 2 eliminating person e from ['b','d','e'] leaving ['b','d']
at pos 0 eliminating person b from ['b','d'] leaving ['d']
winner is d
Mark Harrison
Hi Mark, thanks for the help. It definitely went a long way, as my program is almost done. I'm still having an issue though. My actors are off by one... can't seem to find where it is that's doing that. Can you take a look at the edit I made above?
Stradigos
Never mind! Fixed it!
Stradigos