Java で書かれた単純なブルート フォース ソリューションを次に示します。
/**
* Return maximum number of floors that could be checked with given number
* of marbles and drops.
*/
private static int getMaximumFloors (int marblesCount, int dropsCount)
{
if (marblesCount == 0) return 0;
else
{
int result = 0;
for (int i = 0; i < dropsCount; i++)
{
result += getMaximumFloors (marblesCount - 1, i) + 1;
}
return result;
}
}
/**
* Return minimum number of drops that is definitely enough to check
* given number of floors having given number of marbles.
*/
private static int getMinimumDrops (int marblesCount, int floorsCount)
{
int dropsCount = 0;
while (getMaximumFloors (marblesCount, dropsCount) < floorsCount)
dropsCount += 1;
return dropsCount;
}
public static void main (String [] args)
{
System.out.println (getMinimumDrops (2, 100));
}
C/C++ への移植は容易なはずです。
以下にいくつかの結果を示します。
2 marbles, 100 floors -> 14
3 marbles, 100 floors -> 9
4 marbles, 100 floors -> 8
5 marbles, 100 floors -> 7
6 marbles, 100 floors -> 7