Fixed a bug with the scoring in simon says,
Recursion practice
Problem 1, from the book
Code: Select all
int Increase(int years)
{
if (years == 1)
{
return 15000;
}
return Increase(years - 1) * 1.04 + 400;
}
https://community.topcoder.com/stat?c=p ... nt&pm=6011
my function
Code: Select all
int NumTrucks(int numCrates, int loadSize)
{
int pile1 = numCrates / 2;
int pile2 = numCrates - pile1;
if (numCrates <= loadSize)
{
return 1;
}
else if (pile2 <= loadSize)
{
return 2;
}
else if (pile1 <= loadSize)
{
return NumTrucks(pile2, loadSize) + 1;
}
return NumTrucks(pile1, loadSize) + NumTrucks(pile2, loadSize);
}
@Chili
Thanks chili I guess I'm pretty stubborn/persistent or whatever which might be good.
13 is pretty good I managed to get to 18 ones before my brain exploded
@Zedtho
I'm not sure what you mean Zed.
I divide by 6 since I want to find out the odds. The value returned will be less than 1 (0.0154) 1.54% so I can't use integers.
The function is dumb and just for practice so it's only supposed to be called with 6 as parameter (one for each side of a dice)
the reason 0 returns 1 is that when I reach number 1 on the dice, I want to return 1 / 6
The function call in that return statement will now have 0 as argument so it will return 1 which means 1 will return 1 * 1/6 which is the same thing as 1/6.
I could however change it like this
if (n <= 1.0f)
{
return 1.0f / 6;
}
which would give the same result.