Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Book-cricket problem solution

YASH PAL, 31 July 2024
In this HackerEarth Book-cricket problem solution The rules of a cricket game are as follows:  
  1. The game is played for N overs.
  2. Each over consists of 6 balls.
  3. The batting team has P number of players.
  4. At the start of the game, the first 2 players come out to play. The first player is called the striker (the one facing the current ball) and the second player is called the non-striker.
  5. The striker continues being the striker until he either gets OUT (he will be replaced by the next player who is yet to play) or his strike gets ROTATED (the striker becomes the non-striker and the non-striker becomes the striker).
  6. Each ball consists of one of the following events:
  7. 0 : This adds no score to the striker.
  8. 1 : Adds one run to the striker and the strike gets ROTATED.
  9. 2 : Adds two runs to the striker.
  10. 4 : Adds four runs to the striker.
  11. 6 : Adds six runs to the striker.
  12. W : The striker is OUT and will be replaced by next player.
  13. At the end of every over, the strike will be ROTATED again.
Write a program to implement the scoreboard of a cricket game.
HackerEarth Book-cricket problem solution

HackerEarth Book-cricket problem solution.

#include<bits/stdc++.h>
using namespace std;

void eval()
{
int n,players;
cin>>n>>players;
string s;
cin>>s;
vector<int> score(players);
int p1=0,p2=1;
int next = 2;
for(int i=0;i<6*n;i++)
{
if(p1>=players || p2>=players)
assert(0);
if(s[i]=='W')
p1=next++;
else if(s[i]=='6')
score[p1]+=6;
else if(s[i]=='4')
score[p1]+=4;
else if(s[i]=='2')
score[p1]+=2;
else if(s[i]=='1')
{
score[p1]+=1;
swap(p1,p2); //rotate strike
}
else if(s[i]=='0')
;
else
assert(0); //undefined character
if(i%6==5)
{
swap(p1,p2); //rotate strike
}

}
for(int i=0;i<players;i++)
{
cout<<"Player "<<i<<": ";
if(i==p1 || i==p2)
cout<<score[i]<<"*"<<endl;
else if(next>i)
cout<<score[i]<<endl;
else
cout<<"DNBn";
}

}

int main()
{
int t;
cin>>t;
for(int te=1;te<=t;te++)
{
cout<<"Case "<<te<<":n";
eval();
}
}

Second solution

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

void solve() {
int n, p; cin >> n >> p;
assert(n >= 1 && n <= 10000);
assert(p >= 2 && p <= 10);
string s; cin >> s;
assert((int)s.size() == n*6);
int a[p]; memset(a,0,sizeof(a));
int playing[p]; memset(playing,0,sizeof(playing));
bool ok[p]; for(int i = 0 ; i < p ; i++) ok[i] = 0;
int st = 0, ru = 1;
int nxt = 2;
playing[st] = 1, playing[ru] = 1;
ok[st] = 1, ok[ru] = 1;
for(int i = 0 ; i < s.size() ; i++) {
assert(s[i] == 'W' || s[i] == '0' || s[i] == '1' || s[i] == '2' || s[i] == '3' || s[i] == '4' || s[i] == '5' || s[i] == '6');
if(s[i] == 'W') {
playing[st] = 2;
st = nxt; nxt = nxt + 1;
playing[st] = 1;
ok[st] = 1;
} else if((s[i] - '0') % 2 == 0) {
a[st] += (s[i] - '0');
} else if((s[i] - '0') % 2 == 1) {
a[st] += (s[i] - '0');
swap(st, ru);
}
if(i % 6 == 5) {
swap(st, ru);
}
}
for(int i = 0 ; i < p ; i++) {
if(playing[i] == 2) {
cout << "Player " << i + 1 << ": ";
cout << a[i] << "n";
} else if(ok[i]) {
cout << "Player " << i + 1 << ": ";
cout << a[i] << "*n";
} else {
cout << "Player " << i + 1 << ": ";
cout << "DNBn";
}
}
}

int main() {
int t; cin >> t;
assert(t >= 1 && t <= 10);
for(int i = 1 ; i <= t ; i++) {
cout << "Case " << i << ":n";
solve();
}
}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes