Grave digging?
Here is my solution in C#. I spent about 20 minutes trying to make the code smaller but I couldn't seem to get it any smaller then what it was without making the variables names smaller of course.
I don't even think this is right. I didn't even follow the rules correctly (disqualified)
Any who, continues to flip the "Coin" until you get heads twice in a row.PHP Code:
static void Main(string[] args)
{
int conHeads = 0, numThrows = 0, landHeads = 0;
while(conHeads < 2)
{
Thread.Sleep(new Random().Next(10, 75));
if (GoThrow() == true)
{
conHeads++; landHeads++;
}
else
{
conHeads = 0;
}
//conHeads = (GoThrow() == true ? conHeads += 1 : conHeads = 0);
//Wouldn't work with conHeads++... ^without times it landed on heads.
numThrows++;
}
Console.WriteLine("Number of throws: " + numThrows);
Console.WriteLine("Times landed on heads: " + landHeads);
Decimal avg = numThrows / landHeads;
Console.WriteLine("Average?: " + Math.Round(avg, 2));
Console.ReadLine();
}
static Boolean GoThrow()
{
// 0 = heads.
return (new Random().Next(0, 2)) == 0 ? true : false;
}
Results 1 to 5 of 5
Threaded View
- 23 Feb. 2012 06:37am #2