#include<stdio.h>
#include<stdlib.h>
#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define NTAB 32
#define NDIV (1+(IM-1)/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)
float ran1(long *idum)
{
int j;
long k;
static long iy=0;
static long iv[NTAB];
float temp;
if (*idum <= 0 || !iy) {
if (-(*idum) < 1) *idum=1;
else *idum = -(*idum);
for (j=NTAB+7;j>=0;j--) {
k=(*idum)/IQ;
*idum=IA*(*idum-k*IQ)-IR*k;
if (*idum < 0) *idum += IM;
if (j < NTAB) iv[j] = *idum;
}
iy=iv[0];
}
k=(*idum)/IQ;
*idum=IA*(*idum-k*IQ)-IR*k;
if (*idum < 0) *idum += IM;
j=iy/NDIV;
iy=iv[j];
iv[j] = *idum;
if ((temp=AM*iy) > RNMX) return RNMX;
else return temp;
}
#undef IA
#undef IM
#undef AM
#undef IQ
#undef IR
#undef NTAB
#undef NDIV
#undef EPS
#undef RNMX
int main(int argc, char *argv[])
{
int i, j, k, n, iter=10000;
int RSw, RSl, RSpw, MFYw, MFYl, MFYpw, RSvMFYgr, RSng, MFYng, mutual, method;
int good=0, evil=0, purgatory=0;
float RSwp, MFYwp, num, RSodds=0.5, MFYodds=0.5;
long *idum, lj, lk;
char line[999], c[40];
fprintf(stderr, "current # of Red Sox wins:");
fscanf(stdin, "%d", &RSw);
fprintf(stderr, "current # of Red Sox losses:");
fscanf(stdin, "%d", &RSl);
RSwp = (float)(RSw)/(float)(RSw+RSl);
RSng = 162 - (RSw + RSl);
fprintf(stderr, "current # of MFY wins:");
fscanf(stdin, "%d", &MFYw);
fprintf(stderr, "current # of MFY losses:");
fscanf(stdin, "%d", &MFYl);
MFYwp = (float)(MFYw)/(float)(MFYw+MFYl);
MFYng = 162 - (MFYw + MFYl);
fprintf(stderr, "current # of Red Sox vs MFY games remaining:");
fscanf(stdin, "%d", &mutual);
method=0;
while(method<1 || method > 2){
fprintf(stderr, "Enter (1) for 50/50 method, (2) for current win pct:");
fscanf(stdin, "%d", &method);
}
if(method==2){
RSodds=RSwp;
MFYodds=MFYwp;
}
fprintf(stderr, "Enter random seed:");
fscanf(stdin, "%ld", &lj);
lk=-lj;
idum=&lk;
num=ran1(idum);
idum=&lj;
for(n=0;n<iter;n++){
RSpw=0;
for(i=0;i<(RSng-mutual);i++){
num=ran1(idum);
if(num<RSodds) RSpw++;
}
MFYpw=0;
for(i=0;i<(MFYng-mutual);i++){
num=ran1(idum);
if(num<MFYodds) MFYpw++;
}
for(i=0;i<mutual;i++){
num=ran1(idum);
if(num<0.5) RSpw++;
else MFYpw++;
}
if((RSw+RSpw) > (MFYw+MFYpw)) good++;
else
if((RSw+RSpw) == (MFYw+MFYpw)) purgatory++;
else evil++;
}
fprintf(stdout, "Red Sox win the division %f percent of the time.\n", (float)(good)/(float)(iter));
fprintf(stdout, "MFY win the division %f percent of the time.\n", (float)(evil)/(float)(iter));
fprintf(stdout, "Tie for the division %f percent of the time.\n", (float)(purgatory)/(float)(iter));
}