Created
November 27, 2018 15:25
-
-
Save CompSciRocks/83ac93a976bc92eae316ff1928df078a to your computer and use it in GitHub Desktop.
Solution for the Frog Simulation free response on the 2018 AP computer science exam - https://compsci.rocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double runSimulation(int num) { | |
int good = 0; | |
for (int i=0; i<num; i++) | |
if (simulate()) | |
good++; | |
return (double)good / num; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean simulate() { | |
int hops = 0; | |
int cntPos = 0; | |
while (hops < maxHops && cntPos >= 0 && cntPos < goalDistance) { | |
cntPos += hopDistance(); | |
hops++; | |
} | |
return cntPos >= goalDistance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment