Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C# Problem

Name: Anonymous 2011-10-15 17:16

I'm trying to write a simple program in C# that does the following:

I have an array of integers numbered sequentially from 1 to 2000. I loop through and discard the first integer, and move the second integer to the end of the array. Then I discard the 3rd, and the 4th becomes the end integer. Discard the 5th, and the 6th becomes the end integer, etc.

This process will continue until it ultimately ends in one last integer remaining. How do I find it?

A smaller example of what I'm talking about from 1 to 10:

Start with [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Then [3 4 5 6 7 8 9 10 2]
Then [5 6 7 8 9 10 2 4]
Then [7 8 9 10 2 4 6]
Then [9 10 2 4 6 8]
Then [2 4 6 8 10]
Then [6 8 10 4]
Then [10 4 8]
Then [8 4]
Finally ending with [4]

And I'm not sure whether to use an array and somehow remove numbers from it (changing its size dynamically)or maybe to use a List<int>?

Name: Anonymous 2011-10-15 17:17

This is what I have so far, but it's throwing an enumerable error:

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("How many total cards? ");
            int tot = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Finding last card between 1 and {0}", tot);
            Console.WriteLine("Last card = {0}", FindLastCard(tot));
            Console.ReadKey();
        }

        static string FindLastNumber(int total)
        {
            int numbersLeft = total;
            List<int> lstNumbers = new List<int>();
            for (int x = 1; x <= total; x++)
            {
                lstNumbers.Add(x);
            }
            while (numbersLeft > 1)
            {
                numbersLeft = Iterate(ref lstNumbers);
            }
            return lstNumbers.ToString();
        }

        static int Iterate(ref List<int> numbers)
        {
            int count = 1;
            foreach (int num in numbers)
            {
                if (count % 2 != 0) numbers.RemoveAt(count - 1); // discard first
                count++;
            }
            return count;
        }
    }


and I'm sure it's wrong and could be done more efficiently. Just not sure how.

Name: Anonymous 2011-10-15 17:38

not sure about C# but pretty easy with brain fuck. I strongly suggest you to drop these lesser languages such as C#
+[++++++++++>,
----------]<[<
]>[-]>[[.>]+[<
]>[-]>[-<+>]<[
->>[>]<+[<]<]>
>[>]<-<[<]++++
++++++.[-]>]

Name: Anonymous 2011-10-16 11:48

NO

Don't change these.
Name: Email:
Entire Thread Thread List