[Locked] Paint Fence

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Locked] Paint Fence相关的知识,希望对你有一定的参考价值。

Paint Fence

There is a fence with n posts, each post can be painted with one of the k colors.

You have to paint all the posts such that no more than two adjacent fence posts have the same color.

Return the total number of ways you can paint the fence.

Note:
n and k are non-negative integers.

分析:

  这题看起来能直接用公式输出答案吧...如果觉得公式算起来麻烦,那么对于连续排列串,而且只需要得到可以涂的种类数,故应可用动态规划解决。

代码:

int totalways(int n, int k) {
    if(n == 0 || n == 1 || k == 0)
        return k;
    int ls = k, ld = k * (k - 1);
    for(int i = 2; i < n; i++) {
        int temp = ls;
        ls = ld;
        ld = (temp + ld) * (k - 1);
    }
    return ls + ld;
}

 

以上是关于[Locked] Paint Fence的主要内容,如果未能解决你的问题,请参考以下文章

vivado pll 中locked的输出原理

User status is locked 请翻译一下 准确点,分多

the account is locked怎么解决

the account is locked怎么解决

the account is locked怎么解决

[Locked] Alien Dictionary