📌  相关文章
📜  使用没有连续 2 的 1 和 2 计算达到分数的方法

📅  最后修改于: 2021-09-17 07:38:26             🧑  作者: Mango

板球运动员必须得分 N 次,条件是他只能跑 1 次或 2 次,连续跑不应该是 2 次。找出他可以采取的所有可能组合。
例子:

Input : N = 4 
Output : 4
1+1+1+1, 1+2+1, 2+1+1, 1+1+2

Input : N = 5
Output : 6

来源:Oracle 校园访谈

这个问题是游戏中达到给定分数的方法计数的变体,可以在 O(n) 时间和 O(n) 辅助空间中解决。
下面是上述问题的递归解决方案。

C++
// A simple recursive implementation for
// counting ways to reach a score using 1 and 2 with
// consecutive 2 allowed
#include 
using namespace std;
 
int CountWays(int n, bool flag)
{
    if (n == 0) // base case
        return 1;
 
    int sum = 0;
 
    // 2 is not scored last time so we can score either 2 or 1
    if (flag == false && n > 1)
        sum = sum + CountWays(n - 1, false) + CountWays(n - 2, true);
 
    // 2 is scored last time so we can only score 1
    else
        sum = sum + CountWays(n - 1, false);
 
    return sum;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << CountWays(n, false);
    return 0;
}


Java
// A simple recursive implementation for
// counting ways to reach a score using 1 and 2 with
// consecutive 2 allowed
 
import java.io.*;
 
class GFG {
   static  int CountWays(int n, boolean flag)
{
    if (n == 0) // base case
        return 1;
 
    int sum = 0;
 
    // 2 is not scored last time so we can score either 2 or 1
    if (flag == false && n > 1)
        sum = sum + CountWays(n - 1, false) + CountWays(n - 2, true);
 
    // 2 is scored last time so we can only score 1
    else
        sum = sum + CountWays(n - 1, false);
 
    return sum;
}
    // Driver code
    public static void main (String[] args) {
            int n = 5;
            System.out.println(CountWays(n, false));
    }
}


Python3
# A simple recursive implementation for
# counting ways to reach a score using 1 and 2 with
# consecutive 2 allowed
 
def CountWays(n,flag):
 
    # base case
    if n ==0:
        return 1
    sum =0
    # 2 is not scored last time so
    # we can score either 2 or 1
    if flag ==False and n>1:
        sum = sum+CountWays(n-1,False)+CountWays(n-2,True)
    else:
    # 2 is scored last time so we can only score 1
        sum = sum+CountWays(n-1,False)
    return sum
 
# Driver code
if __name__=='__main__':
    n = 5
    print(CountWays(n,False))
 
# This code is contributed by
# Shrikant13


C#
// A simple recursive implementation
// for counting ways to reach a score
// using 1 and 2 with consecutive 2 allowed
using System;
 
class GFG
{
static int CountWays(int n, bool flag)
{
    if (n == 0) // base case
        return 1;
 
    int sum = 0;
 
    // 2 is not scored last time so
    // we can score either 2 or 1
    if (flag == false && n > 1)
        sum = sum + CountWays(n - 1, false) +
                    CountWays(n - 2, true);
 
    // 2 is scored last time so
    // we can only score 1
    else
        sum = sum + CountWays(n - 1, false);
 
    return sum;
}
 
// Driver Code
static public void Main ()
{
    int n = 5;
    Console.WriteLine(CountWays(n, false));
}
}
 
// This code is contributed by Sach_Code


PHP
 1)
        $sum = $sum + CountWays($n - 1, false) +
                      CountWays($n - 2, true);
 
    // 2 is scored last time so
    // we can only score 1
    else
        $sum = $sum + CountWays($n - 1, false);
 
    return $sum;
}
 
// Driver Code
$n = 5;
echo CountWays($n, false);
 
// This code is contributed
// by Sach_Code
?>


Javascript


C++
// A memoization based implementation for
// counting ways to reach a score using 1 and 2 with
// consecutive 2 allowed
#include 
using namespace std;
 
const int MAX = 101;
int dp[MAX][2];
 
int CountWays(int n, int flag = 0)
{
    // if this state is already visited return
    // its value
    if (dp[n][flag] != -1)
        return dp[n][flag];
 
    // base case
    if (n == 0)
        return 1;
 
    // 2 is not scored last time so we can
    // score either 2 or 1
    int sum = 0;
    if (flag == 0 && n > 1)
        sum = sum + CountWays(n - 1, 0) + CountWays(n - 2, 1);
 
    // 2 is scored last time so we can only score 1
    else
        sum = sum + CountWays(n - 1, 0);
 
    return dp[n][flag] = sum;
}
 
int main()
{
    int n = 5;
    memset(dp, -1, sizeof(dp));
    cout << CountWays(n);
    return 0;
}


Java
import java.util.Arrays;
 
// A memoization based implementation for
// counting ways to reach a score using
// 1 and 2 with consecutive 2 allowed
class GfG
{
 
    static int MAX = 101;
    static int dp[][] = new int[MAX][2];
 
    static int CountWays(int n, int flag)
    {
        // if this state is already visited return
        // its value
        if (dp[n][flag] != -1)
        {
            return dp[n][flag];
        }
 
        // base case
        if (n == 0)
        {
            return 1;
        }
 
        // 2 is not scored last time so we can
        // score either 2 or 1
        int sum = 0;
        if (flag == 0 && n > 1)
        {
            sum = sum + CountWays(n - 1, 0) +
                    CountWays(n - 2, 1);
        }
         
        // 2 is scored last time so we can only score 1
        else
        {
            sum = sum + CountWays(n - 1, 0);
        }
 
        return dp[n][flag] = sum;
    }
 
    public static void main(String[] args)
    {
        int n = 5;
        for (int i = 0; i < MAX; i++)
        {
            Arrays.fill(dp[i], -1);
        }
        System.out.println(CountWays(n, 0));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# A memoization based implementation for
# counting ways to reach a score using
# 1 and 2 with consecutive 2 allowed
 
MAX = 101
dp = [[-1 for i in range(2)]
          for i in range(MAX)]
 
def CountWays(n, flag):
     
    # if this state is already visited
    # return its value
    if (dp[n][flag] != -1):
        return dp[n][flag]
 
    # base case
    if (n == 0):
        return 1
 
    # 2 is not scored last time so
    # we can score either 2 or 1
    sum = 0
    if (flag == 0 and n > 1):
        sum = (sum + CountWays(n - 1, 0) +
                     CountWays(n - 2, 1))
 
    # 2 is scored last time so we
    # can only score 1
    else:
        sum = sum + CountWays(n - 1, 0)
 
    dp[n][flag] = sum
    return dp[n][flag]
 
# Driver Code
if __name__ == '__main__':
    n = 5
    print(CountWays(n, 0))
 
# This code is contributed by
# Surendra_Gangwar


C#
// A memoization based implementation for
// counting ways to reach a score using
// 1 and 2 with consecutive 2 allowed
using System;
 
class GfG
{
 
    static int MAX = 101;
    static int [,]dp = new int[MAX, 2];
 
    static int CountWays(int n, int flag)
    {
        // if this state is already visited return
        // its value
        if (dp[n, flag] != -1)
        {
            return dp[n, flag];
        }
 
        // base case
        if (n == 0)
        {
            return 1;
        }
 
        // 2 is not scored last time so we can
        // score either 2 or 1
        int sum = 0;
        if (flag == 0 && n > 1)
        {
            sum = sum + CountWays(n - 1, 0) +
                    CountWays(n - 2, 1);
        }
         
        // 2 is scored last time so we can only score 1
        else
        {
            sum = sum + CountWays(n - 1, 0);
        }
 
        return dp[n,flag] = sum;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5;
        for (int i = 0; i 


Javascript


输出:
6

该问题具有最优子结构性质,因为该问题可以使用子问题的解来解决。
以下是上述问题的 Dp 解决方案

C++

// A memoization based implementation for
// counting ways to reach a score using 1 and 2 with
// consecutive 2 allowed
#include 
using namespace std;
 
const int MAX = 101;
int dp[MAX][2];
 
int CountWays(int n, int flag = 0)
{
    // if this state is already visited return
    // its value
    if (dp[n][flag] != -1)
        return dp[n][flag];
 
    // base case
    if (n == 0)
        return 1;
 
    // 2 is not scored last time so we can
    // score either 2 or 1
    int sum = 0;
    if (flag == 0 && n > 1)
        sum = sum + CountWays(n - 1, 0) + CountWays(n - 2, 1);
 
    // 2 is scored last time so we can only score 1
    else
        sum = sum + CountWays(n - 1, 0);
 
    return dp[n][flag] = sum;
}
 
int main()
{
    int n = 5;
    memset(dp, -1, sizeof(dp));
    cout << CountWays(n);
    return 0;
}

Java

import java.util.Arrays;
 
// A memoization based implementation for
// counting ways to reach a score using
// 1 and 2 with consecutive 2 allowed
class GfG
{
 
    static int MAX = 101;
    static int dp[][] = new int[MAX][2];
 
    static int CountWays(int n, int flag)
    {
        // if this state is already visited return
        // its value
        if (dp[n][flag] != -1)
        {
            return dp[n][flag];
        }
 
        // base case
        if (n == 0)
        {
            return 1;
        }
 
        // 2 is not scored last time so we can
        // score either 2 or 1
        int sum = 0;
        if (flag == 0 && n > 1)
        {
            sum = sum + CountWays(n - 1, 0) +
                    CountWays(n - 2, 1);
        }
         
        // 2 is scored last time so we can only score 1
        else
        {
            sum = sum + CountWays(n - 1, 0);
        }
 
        return dp[n][flag] = sum;
    }
 
    public static void main(String[] args)
    {
        int n = 5;
        for (int i = 0; i < MAX; i++)
        {
            Arrays.fill(dp[i], -1);
        }
        System.out.println(CountWays(n, 0));
    }
}
 
/* This code contributed by PrinciRaj1992 */

蟒蛇3

# A memoization based implementation for
# counting ways to reach a score using
# 1 and 2 with consecutive 2 allowed
 
MAX = 101
dp = [[-1 for i in range(2)]
          for i in range(MAX)]
 
def CountWays(n, flag):
     
    # if this state is already visited
    # return its value
    if (dp[n][flag] != -1):
        return dp[n][flag]
 
    # base case
    if (n == 0):
        return 1
 
    # 2 is not scored last time so
    # we can score either 2 or 1
    sum = 0
    if (flag == 0 and n > 1):
        sum = (sum + CountWays(n - 1, 0) +
                     CountWays(n - 2, 1))
 
    # 2 is scored last time so we
    # can only score 1
    else:
        sum = sum + CountWays(n - 1, 0)
 
    dp[n][flag] = sum
    return dp[n][flag]
 
# Driver Code
if __name__ == '__main__':
    n = 5
    print(CountWays(n, 0))
 
# This code is contributed by
# Surendra_Gangwar

C#

// A memoization based implementation for
// counting ways to reach a score using
// 1 and 2 with consecutive 2 allowed
using System;
 
class GfG
{
 
    static int MAX = 101;
    static int [,]dp = new int[MAX, 2];
 
    static int CountWays(int n, int flag)
    {
        // if this state is already visited return
        // its value
        if (dp[n, flag] != -1)
        {
            return dp[n, flag];
        }
 
        // base case
        if (n == 0)
        {
            return 1;
        }
 
        // 2 is not scored last time so we can
        // score either 2 or 1
        int sum = 0;
        if (flag == 0 && n > 1)
        {
            sum = sum + CountWays(n - 1, 0) +
                    CountWays(n - 2, 1);
        }
         
        // 2 is scored last time so we can only score 1
        else
        {
            sum = sum + CountWays(n - 1, 0);
        }
 
        return dp[n,flag] = sum;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5;
        for (int i = 0; i 

Javascript


输出:
6

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程