板球运动员必须得分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
资料来源:校园甲骨文专访
这个问题是在游戏中达到给定分数的计数方式的变化,可以在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
输出:
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 */
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
输出:
6