Header Ad

HackerEarth Book-cricket problem solution

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<<"DNB\n";
}

}

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 << "DNB\n";
}
}
}

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


Post a Comment

0 Comments