All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > General
2000 cards (Posted on 2009-01-04) Difficulty: 2 of 5
There are cards labeled from 1 to 2000. The cards are arranged and placed in a pile.

The top card is placed on the table, then the next card at the bottom of the pile.

Then the next card is placed on the table to the right of the first card, and the next card is placed at the bottom of the pile.

This process is continued until all the cards are on the table.

The final order (from left to right) is 1, 2, 3, ... , 2000.

In the original pile, how many cards were above card labeled 1999?

See The Solution Submitted by pcbouhid    
No Rating

Comments: ( Back to comment list | You must be logged in to post comments.)
Another Computer Spoiler | Comment 3 of 10 |
The answer is mentioned at the end of this post.

In the code replace values for these variables appropriately to get result for any given pair of positive integers.

totalNumberOfCards = 2000;
int cardNumberBeingSought=1999;


Following is the code in C# 2.0.

private class Card
        {
            private int _position;
            
            public int Position
            {
                get
                {
                    return _position;
                }
            }

            private int _value;

            public int Value
            {
                get
                {
                    return _value;
                }
                set
                {
                    _value = value;
                }
            }

            public Card(int position, int? value)
            {
                _position = position;
                if (value != null)
                {
                    _value = (int)value;
                }
                else
                {
                    _value = -1;
                }
            }

            public Card(Card card)
            {
                _position = card.Position;

                _value = card.Value;
            }
        }


//////////////////////////////


private void calculatePositionButton_Click(object sender, EventArgs e)
        {
            int totalNumberOfCards = 2000;
            int cardNumberBeingSought = 1999;
            List<Card> currentPack = new List<Card>();
            int i;
            for (i = 0; i < totalNumberOfCards; i++)
            {
                currentPack.Add(new Card(i+1, null));
            }
            List<Card> nextPack;
            Card requiredCard;
            i = 0;
            int j = 0;
            bool found = false;
            while (true)
            {
                i = 0;
                nextPack = new List<Card>();
                // give value to and throw away the cards that are supposed to be placed on table.
                while (i < currentPack.Count)
                {
                    currentPack[i].Value = ++j;

                    // if the card's value is what we seek then break the loop!
                    if (j == cardNumberBeingSought)
                    {
                        found = true;
                        break;
                    }

                    if (i + 1 < currentPack.Count)
                    {
                        nextPack.Add(new Card(currentPack[i + 1]));
                    }
                    i += 2;
                }
                if (found)
                {
                    requiredCard = new Card(currentPack[i]);
                    break;
                }
                currentPack = nextPack;
            }

            MessageBox.Show("The original position of card 1999 is " + requiredCard.Position);
        }

The answer comes out as: The original position of the card is 1536. Hence there are 1535 cards above it.

Edited on January 4, 2009, 7:40 pm
  Posted by elementofsurprize on 2009-01-04 19:36:38

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (12)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information