Need help with problem

This is a discussion on Need help with problem within the C++ Programming forums, part of the General Programming Boards category; I have a problem im not sure where to begin with: There is a round table, i have to write ...

  1. #1
    Unregistered
    Guest

    Lightbulb Need help with problem

    I have a problem im not sure where to begin with:
    There is a round table, i have to write a program to determine which chair will remain (number of chairs input by user) in the end. First chair remains , then the next one is removed, and so on. I am unsure on how to approach the problem.
    -agonzalez

  2. #2
    31173 h4x0r gnu-ehacks's Avatar
    Join Date
    Nov 2001
    Posts
    556
    Greetings. This may take a tiny bit of creativity, but it can't be done, if you work at it hard enough.

    First you create an array, like so:
    bool chair[5];

    Now, you don't need to use the number 5, this is just the number I decide to use. It can be any number of chairs that needs to be used.

    Second, cycle through them, and see if it's number is the right one...Like so.

    if (chair[n] == x)
    {
    chair[n] == false;
    }

    In this code snippet, x is the number that is suppose to be kicked off, and n is the number of the chair you are trying to find. This is just one way to do this. In programming, there are MANY ways to complete most tasks. Good Luck!
    What will people say if they hear that I'm a Jesus freak?
    What will people do if they find that it's true?
    I don't really care if they label me a Jesus freak, there is no disguising the truth!

    Jesus Freak, D.C. Talk

    -gnu-ehacks

  3. #3
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    LOL
    This may take a tiny bit of creativity, but it can't be done,
    if you work at it hard enough.
    There is no hope like no hope at all.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  4. #4
    Unregistered
    Guest
    the post was not quite what i was looking for, maybe pasting the exact question can be more helpful.

    Once upon a time, King Arthur had a kind and beautiful daughter, who had grown up. The King wanted the Princess to get married, who would only marry someone very intelligent. "Let the knights sit around a round table. Keep the first one alive, but kill the next one. Go around the table until only one survival. The person who knows where to sit is the one I would marry." The Princess smiled to add that "No one will really be killed of course."

    You are given a laptop to travel back in time to get the hand of the princess. Write a program to find the place to sit for any number of seats. You need to test your understanding of the rule first. Make sure you agree with the following: when the total number of seats is 1, 3, 5, 11, the survivor is 1, 3, 3, 7, respectively.

    -agonzalez

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    You could use a 2-way linked list (without thinking about it for very long, that's what i'd probably do).

    So you have a node on the linked list for each chair and each node has previous and next pointers (standard stuff). All you need to do is set up an initial load of pointers all pointing to the next in the circle and with an ID for which chair position they are - then run a loop round removing as per the problem until there is no 'next' position. You should be left with one node (the one you want).

    Tada!

    Almosthere
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    This one seems to beg using a circular linked list. If you've studied linked lists, you probably already have 90% of the code to do this problem. Just create the list, then use a loop to eliminate every other item until only one item is left.

    Using an array is pretty straightforward also, but not as elegant.

  7. #7
    31173 h4x0r gnu-ehacks's Avatar
    Join Date
    Nov 2001
    Posts
    556
    LoL, whoops, I didn't mean can't, I meant can.
    What will people say if they hear that I'm a Jesus freak?
    What will people do if they find that it's true?
    I don't really care if they label me a Jesus freak, there is no disguising the truth!

    Jesus Freak, D.C. Talk

    -gnu-ehacks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21