All But One Shall Die!
-
"...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:
-
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!
Herman Melville for me, I'm afraid!
-
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
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; }
-
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