/*
American Computer Science League
Contest #1, 2006-2007
Intermediate Division #5
"Probability"
Jacob Hurwitz
9th grade
Team #8009
*/
#include <iostream>
using namespace std;
short getIn(short
option); //gets the input
void doCase(short
sitCase); //does case calculations
void doDisp();
//displays answer
short num, bag[2], total, rep,
mar[16];
int fnum, fden;
/*
num = #marbles, bag[0] = #reds,
bag[1] = #blues, total = bag[0] + bag[1]
rep = replacement?, mar = sequence
of picks
fnum =
fraction numerator, fden = fraction denominator
*/
//Processes the input.
int
main()
{
while
(true)
{
//Gets
variables
num = getIn(0);
bag[0] = getIn(0);
bag[1] = getIn(0);
total = bag[0] + bag[1]; //Calculates total
if (num > 1)
rep = getIn(1); //If necessary,
get rep
for (short i=0; i<num; i++)
mar[i] = getIn(2);
//Get mar[i] (sequence of picks)
//Initializes
variables for setting later
fnum = 1;
fden = 1;
short sitCase = 0; //Initializes
variable to store situation case
//Determines
the situation case
if (num == 1) //One marble
sitCase = 1;
else if (num > 1 && rep) //More than one with
replacement
sitCase = 2;
else if (num > 1 && !rep) //More than one without
replacement
sitCase = 3;
else
cout << "Error categorizing
situation case" << endl;
doCase(sitCase); //Calls appropriate function
}
return
0;
}
//Gets the input
short getIn(short
option)
{
char
tmp[4]; //Temp variable
short
input; //The actual returned input
cin >> tmp;
if
(option==0) //If getting a number...
{
input = atoi(tmp);
//Convert char to short
}
else
if (option==1) //If getting Y/N...
{
switch(tmp[0])
{
case 'N': input = 0; break;
case 'Y': input = 1; break;
default: cout << "Error
deciphering replacement option Y/N" << endl;
break;
}
}
else
if (option==2) //If getting R/B/r/b
{
switch(tmp[0])
{
case 'R': input = 0; break;
case 'b': input = 0; break;
case 'B': input = 1; break;
case 'r': input = 1; break;
default: cout << "Error
deciphering marble color R/B/r/b" << endl;
break;
}
}
return
input; //Returns input
}
//Does situation case calculations
void doCase(short
sitCase)
{
for
(short i=0; i<num; i++)
{
fnum *= bag[ mar[i] ]; //Numerator multiplied by # that color
fden *= total; //Denominator
multiplied by # total
if (sitCase == 3)
{
bag[ mar[i] ]--; //# that color
decreased
total--; //# total decreased
}
}
cout << fnum
<< "/" << fden << endl; //Displays numerator / denominator
}