采访 - 甲骨文

时间:2012-12-05 03:00:30

标签: algorithm

在游戏中,唯一可以得到的分数是2,3,4,5,6,7,8,它们可以被制作任意次数

团队可以玩的组合总数是多少,团队可以达到50分。

例如8,8,8,8,8,8,2是有效的8,8,8,8,8,4,4,2也是有效的。等...

4 个答案:

答案 0 :(得分:2)

问题可以通过动态编程解决,有2个参数:

  • i - 我们考虑过的指数
  • s - 总分。

f(i, s)将包含获得分数s的方式总数。

score[]成为可以制作的唯一正面分数的列表。

DP溶液的配方:

f(0, s) = 1, for all s divisible to score[0]
f(0, s) = 0, otherwise

f(i + 1, s) = Sum [for k = 0 .. floor(s/score[i + 1])] f(i, s - score[i + 1] * k)

答案 1 :(得分:1)

这看起来像硬币改变问题。我不久前写了一些Python代码。

已编辑的解决方案:

from collections import defaultdict

my_dicto = defaultdict(dict)

def row_analysis(v, my_dicto, coins):
    temp = 0
    for coin in coins:
        if v >= coin:
            if v - coin == 0: # changed from if v - coin in (0, 1):
                temp += 1
                my_dicto[coin][v] = temp
            else:                
                temp += my_dicto[coin][v - coin]
                my_dicto[coin][v] = temp
        else:
            my_dicto[coin][v] = temp
    return my_dicto

def get_combs(coins, value):
    '''
    Returns answer for coin change type problems.
    Coins are assumed to be sorted.

    Example:
        >>> get_combs([1,2,3,5,10,15,20], 50)
        2955
    '''
    dicto = defaultdict(dict)

    for v in xrange(value + 1):
        dicto = row_analysis(v, dicto, coins)

    return dicto[coins[-1]][value]

在你的情况下:

>>> get_combs([2,3,4,5,6,7,8], 50)
3095

答案 2 :(得分:1)

就像访问7分支决策树一样。

代码是:

class WinScore{
static final int totalScore=50;
static final int[] list={2,3,4,5,6,7,8};
public static int methodNum=0;

static void visitTree( int achieved , int index){
        if (achieved >= totalScore ){
                return;
        }
        for ( int i=index; i< list.length; i++ ){
                if ( achieved + list[i] == totalScore ) {
                        methodNum++;
                }else if (  achieved + list[i] < totalScore ){
                        visitTree( achieved + list[i], i );
                }
        }
}
public static void main( String[] args ){
        visitTree(0, 0);
        System.out.println("number of methods are:" + methodNum );

}
}
output:
number of methods are:3095

答案 3 :(得分:0)

偶然发现了这个问题 - 这是一个c#变体,可以让你探索不同的组合:

static class SlotIterator
{
    public static IEnumerable<string> Discover(this int[] set, int maxScore)
    {
        var st = new Stack<Slot>();
        var combinations = 0;
        set = set.OrderBy(c => c).ToArray();
        st.Push(new Slot(0, 0, set.Length));
        while (st.Count > 0)
        {
            var m = st.Pop();
            for (var i = m.Index; i < set.Length; i++)
            {
                if (m.Counter + set[i] < maxScore)
                {
                    st.Push(m.Clone(m.Counter + set[i], i));
                }
                else if (m.Counter + set[i] == maxScore)
                {
                    m.SetSlot(i);
                    yield return m.Slots.PrintSlots(set, ++combinations, maxScore);

                }
            }
        }
    }

    public static string PrintSlots(this int[] slots, int[] set, int numVariation, int maxScore)
    {
        var sb = new StringBuilder();
        var accumulate = 0;
        for (var j = 0; j < slots.Length; j++)
        {
            if (slots[j] <= 0)
            {
                continue;
            }
            var plus = "+";
            for (var k = 0; k < slots[j]; k++)
            {
                accumulate += set[j];
                if (accumulate == maxScore) plus = "";
                sb.AppendFormat("{0}{1}", set[j], plus);
            }
        }

        sb.AppendFormat("={0} - Variation nr. {1}", accumulate, numVariation);
        return sb.ToString();
    }
}
public class Slot
{
    public Slot(int counter, int index, int countSlots)
    {
        this.Slots = new int[countSlots];
        this.Counter = counter;
        this.Index = index;
    }

    public void SetSlot(int index)
    {
        this.Slots[index]++;
    }

    public Slot Clone(int newval, int index)
    {
        var s = new Slot(newval, index, this.Slots.Length);
        this.Slots.CopyTo(s.Slots, 0);
        s.SetSlot(index);
        return s;
    }

    public int[] Slots { get; private set; }

    public int Counter { get; set; }

    public int Index { get; set; }
}

示例:

    static void Main(string[] args)
    {

        using (var sw = new StreamWriter(@"c:\test\comb50.txt"))
        {
            foreach (var s in new[] { 2, 3, 4, 5, 6, 7, 8 }.Discover(50))
            {
                sw.WriteLine(s);
            }
        }
    }

产生3095种组合。