Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. All But One Shall Die!

All But One Shall Die!

Scheduled Pinned Locked Moved The Lounge
question
24 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • OriginalGriffO OriginalGriff

    "...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee." Don't think Ahab was too happy really... :laugh:

    J Offline
    J Offline
    JMK89
    wrote on last edited by
    #21

    OriginalGriff wrote:

    "...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee."

    That quote always reminds me of the Simpsons!

    OriginalGriffO 1 Reply Last reply
    0
    • J JMK89

      OriginalGriff wrote:

      "...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee."

      That quote always reminds me of the Simpsons!

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #22

      Herman Melville for me, I'm afraid!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Try this:

        int index = 1;
        var prisoners = Enumerable.Range(1, 1000).ToList();
        while (prisoners.Count != 1)
        {
        while (index < prisoners.Count)
        {
        prisoners.RemoveAt(index);

            // We've killed a prisoner, so the rest of the list has moved up one place.
            // Therefore, we only need to move to the next place to skip the next prisoner.
            index++;
        }
        
        if (index == prisoners.Count + 1)
        {
            // We killed the last one in the list; start the next round with the second in the list:
            index = 1;
        }
        else
        {
            // We killed the second to last; start the next round with the first:
            index = 0;
        }
        

        }

        Console.WriteLine(prisoners[0]);


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        E Offline
        E Offline
        ednrg
        wrote on last edited by
        #23

        Found the error in my ways :) It now works.

           static int FindLastManStanding(int startingPosition = 2)
            {
                int retVal = -1;
                bool lastWasKilled = false;
                List dead = new List();
        
                if (startingPosition == 1)
                    captures.RemoveAt(0);
        
                int max = captures.Max();
        
                for (int i = 1; i <= captures.Count; i++)
                {
                    if (i % 2 == 0)
                    {
                        if ( max == captures\[i - 1\])
                            lastWasKilled = true;
        
                        dead.Add(captures\[i - 1\]);
                    }
                }
        
                dead.ForEach(x => captures.Remove(x));
        
                startingPosition = lastWasKilled ? 2 : 1;
        
                if (captures.Count == 1)
                    retVal = captures\[0\];
                else
                    retVal = FindLastManStanding(startingPosition);
        
                return retVal;
            }
        
        1 Reply Last reply
        0
        • N Nagy Vilmos

          Genghis Kahn, being in a good mood decides to spare one life of his 1,000 captives. He has them stand in a circle and runs a sword through every second man. If the first man is #1 [amongst Kahn's many crimes was a love for VB], which man will be left alive?

          speramus in juniperus

          E Offline
          E Offline
          ednrg
          wrote on last edited by
          #24

          Is this the shortest solution?

          var switcher = true;

          var range = Enumerable.Range(1,1000).ToList();

          while( range.Count > 1)
          range = range.Where(x => (switcher = !switcher) == false).ToList();

          range.Dump(); // (LinqPad)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups