Computer Science, asked by cutestuff20680, 1 month ago

Given the consecutive scores of a cricket match, calculate the total runs scored, the number of wickets, the extras, the runs scored by each batsman, the number of overs bowled. Notes: wkt - wicket, wd - wide ball, add one run to the scoreboard, bowl again, lb - leg bye, and nb - no-ball, add one run to the scoreboard, bowl again. The sample input is - 1 0 1wd 0 3 2 0 0 2nb 0wkt 6 1 0 4 and sample output is - total runs scored: 20, number of wickets: 1, extras: 2, scores by batsmen: 1st batsmen: 3 runs; 2nd batsmen: 8 runs; 3rd batsmen: 7 runs.​

Answers

Answered by ravilaccs
0

Answer:

Program is constructed

Explanation:

A cricket player has to score N runs, with the condition he can take either 1 or 2 runs only and consecutive runs should not be 2. Find all the possible combinations he can take.

Examples:

Input : N = 4

Output : 4

1+1+1+1, 1+2+1, 2+1+1, 1+1+2

Input : N = 5

Output : 6

This problem is a variation of the count number of ways to reach a given score in a game and can be solved in O(n) time and constant auxiliary space.

The first run scored could be either :

a) 1. Now the player has to score N-1 runs.

b) or 2. Since 2’s can not be consecutive runs, the next run scored has to be 1. After that, the player has to score N-(2+1) runs.

Below is the recursive solution to the above problem.

Recursion relation would be:

CountWays(N) = CountWays(N-1) + CountWays(N-2)

Following recursive solution takes exponential time and space (similar to Fibonacci numbers).

Program:

// A simple recursive implementation for // counting ways to reach a score using 1 and 2 with // consecutive 2 allowed

#include <iostream>

using namespace std;

int CountWays(int n)

{

// base cases

if (n == 0) {

 return 1;

}

if (n == 1) {

 return 1;

}

if (n == 2) {

 return 1 + 1;

}

// For cases n > 2

return CountWays(n - 1) + CountWays(n - 3);

}

// Driver code

int main()

{

int n = 10;

cout << CountWays(n);

return 0;

}

Similar questions