#1
02.06.2017 um 14:01 Uhrich habe die zahlen mit UInt mal etwas höher gestellt und 250000000 durchläufe gemacht (hat eine halbe ewigkeit gedauert xD ) in diesem fall hat er es 684281 mal geschafft und die quote liegt bei 0,2737124% :P
C# :: Aufgabe #167 :: Lösung #5
using System;
namespace DrunkenMan
{
class Program
{
static void Main(string[] args)
{
DrunkenMan bob = new DrunkenMan();
int wincount = 0;
int playcount = 2500000;
for(int i = 0; i < playcount; i++)
{
bob.reset();
while (true)
{
bob.randomStep();
if (bob.hasLost())
{
break;
}
if (bob.hasWon())
{
wincount++;
break;
}
}
}
Console.WriteLine("Von {0} Versuchen, hat der Mann es {1} mal durch den Tunnel geschafft.",playcount,wincount);
double perc = (double)wincount / (double)playcount * 100.0;
Console.WriteLine("Dies entspricht einer Erfolgsquote von {0}%",perc);
Console.ReadKey();
}
}
class DrunkenMan
{
static Random rnd = new Random();
int length = 1;
int width = 6;
public void randomStep()
{
switch (rnd.Next(1,9))
{
case 1:
length++;
width--;
break;
case 2:
length++;
break;
case 3:
length++;
width++;
break;
case 4:
width--;
break;
case 5:
width++;
break;
case 6:
length--;
width--;
break;
case 7:
length--;
break;
case 8:
length--;
width++;
break;
}
}
public void reset()
{
length = 1;
width = 6;
}
public bool hasLost()
{
if (length < 1) return true;
if(width < 1 || width > 11)
{
if (length <= 20) return true;
}
return false;
}
public bool hasWon()
{
if (length > 20) return true;
return false;
}
}
}
Kommentare:
Pr0gr4mm3r
Punkte: 60
2 Lösungen
1 Kommentare