33,008
社区成员
发帖
与我相关
我的任务
分享
public static int shooting90()
{
int[] factorials = new int[10];
for (int f = factorials[0] = 1, i = 2; i <= 10; i++) factorials[i - 1] = (f *= i);
return shooting(0, 10, 10, 100 - 90, factorials);
}
private static int shooting(ulong currentValue, int rings, int maxValue, int sum, int[] factorials)
{
int value = 0, nextValue = maxValue;
if (sum <= maxValue)
{
int thisValue, lastValue = sum, valueCount = 0, count = factorials[9];
if (rings > 2) count /= factorials[rings - 2];
for (ulong values = currentValue; values != 0; values >>= 4)
{
if ((thisValue = (int)values & 15) == lastValue) valueCount++;
else
{
if (valueCount != 0)
{
count /= factorials[valueCount];
valueCount = 0;
}
lastValue = thisValue;
}
}
if (valueCount != 0) count /= factorials[valueCount];
value += count;
nextValue = sum - 1;
}
if (--rings != 0) while (nextValue != 0) value += shooting((currentValue << 4) | (uint)nextValue, rings, nextValue, sum - nextValue--, factorials);
return value;
}