Skip to content

Instantly share code, notes, and snippets.

@JohnMertz
Created October 9, 2015 18:37
Show Gist options
  • Save JohnMertz/ea89ffed8d4337eb012b to your computer and use it in GitHub Desktop.
Save JohnMertz/ea89ffed8d4337eb012b to your computer and use it in GitHub Desktop.
How many cards do you have to deal before an entire suit has been dealt?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(){
long long int total = 0;
long long int i = 0;
for (i = 0; i<1000000; i++){
int s = 13;
int c = 13;
int h = 13;
int d = 13;
int attempts = 0;
int choice;
srand(time(0));
while (s != 0 && c != 0 && h !=0 && d != 0){
choice = 1 + (rand() % (s+c+h+d));
if (choice > s+c+h && choice <= s+c+h+d){
d--;
attempts++;
}
else if (choice > s+c && choice <= s+c+h){
h--;
attempts++;
}
else if (choice > s && choice <= s+c){
c--;
attempts++;
}
else if (choice > 0 && choice <= s){
s--;
attempts++;
}
else {
printf("Something has gone horribly wrong!");
}
}
total += attempts;
}
printf("Average: %f\n", (float)total / (i+1));
}
@JohnMertz
Copy link
Author

I suppose that checking <= s+c+h+d and > 0 are both redundant. And having gone that far, checking <= s is also redundant since the third else-if might as well be an else...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment