Question

Coin Flip Easy game

741.pdf

The game has one simple rule: the player cannot flip a single coin,

instead, the player can choose one row (or one column) and flip all the

coins in that row (or that column) simultaneously.

The objective of the game is to find out a strategy to flip the coins so

that the number of head coins is maximized.

Can you write a program to help Tom to win this game?


0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>
 
using namespace std;
 
//class matrix
 
class matrix{
 
// number of rows
 
int r;
 
//no of columns
 
int c;
 
// array to store all the elements
 
int arr[100][100];
 
public:
 
//this function will get the input from the user
 
void get_matrix()
 
{
 
//take row and column
 
cin>>r;
 
cin>>c;
 
// take all the elements
 
for(int i=0;i<r;i++)
 
{
 
for(int j=0;j<c;j++)
 
int k;
 
cin>>k;
 
arr[i][j] = k;
 
}
 
}
 
}
 
// this function will flip the row
 
void invertrow(int r1)
 
{
 
//parse through the row and replace 1 with 0 and 0 with 1
 
for(int i=0;i<c;i++)
 
{
 
if(arr[r1][i]==0)
 
{
 
arr[r1][i]=1;
 
}
 
else
 
{
 
arr[r1][i] = 0;
 
}
 
}
 
}
 
// this function will invert the whole column
 
void invertcol(int c1)
 
{
 
//parse through the column and replace 1 with 0 and 0 with 1
 
for(int i=0;i<r;i++)
 
{
 
if(arr[i][c1]==0)
 
{
 
arr[i][c1]=1;
 
}
 
else
 
{
 
arr[i][c1] = 0;
 
}
 
}
 
}
 
// this function will calculate how many head can be possible
 
int maxhead()
 
{
 
// col1 will store the number of 1 in each column
 
// row 1 will store the number of 1 in each column
 
int col1[c];
 
int chalf = c/2;
 
int rhalf = r/2;
 
int row1[r];
 
//find the number of 1 in each column
 
for(int i=0;i<c;i++)
 
{
 
int col11 = 0;
 
for(int j=0;j<r;j++)
 
{
 
if(arr[j][i]==1)
 
col11++;
 
}
 
col1[i] = col11;
 
}
 
// if the number of 0 are more or equal than the number of 1 then flip
 
for(int i =0;i<c;i++)
 
{
 
if(col1[i]<=chalf)
 
{
 
invertcol(i);
 
}
 
}
 
//count head will find the number of head after all inversions.
 
int count_head =0;
 
//find the number of 1 in each row
 
for(int i=0;i<r;i++)
 
{
 
int row11 = 0;
 
for(int j=0;j<c;j++)
 
{
 
if(arr[i][j]==1)
 
row11++;
 
}
 
row1[i] = row11;
 
}
 
// if the number of 0 are more or equal than the number of 1 then flip
 
for(int i =0;i<r;i++)
 
{
 
if(row1[i]<=rhalf)
 
{
 
invertrow(i);
 
}
 
}
 
count_head =0;
 
//final program to calculate the number of head
 
for(int i =0;i<r;i++)
 
{
 
for(int j=0;j<c;j++)
 
// if the value is 1 then increase count
 
if(arr[i][j]==1)
 
count_head++;
 
}
 
}
 
return count_head;
 
}
 
};
 
int main()
 
{
 
int n ;
 
cin>>n;
 
// n represent the number of testcases.
 
matrix m[n];
 
// get all the matrix and store the object in m
 
for(int i=0;i<n;i++)
 
{
 
m[i].get_matrix();
 
}
 
//find max head and print them
 
for(int i=0;i<n;i++)
 
{
 
cout<<m[i].maxhead()<<"\n";
 
}
 
}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Coin Flip Easy game
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Coin flipping easy game

    The game has one simple rule: the player cannot flip a single coin,instead, the player can choose one row (or one column) and flip all thecoins in that row (or that column) simultaneously.The objective of the game is to find out a strategy to flip the coins sothat the number of head coins is maximized.Can you write a program to help Tom to win this game?741.pdf

  • USING RECURSIONN!!! JAVA CODE Coin game: Alice and Bob are playing a game using a bunch...

    USING RECURSIONN!!! JAVA CODE Coin game: Alice and Bob are playing a game using a bunch of coins. The players pick several coins out of the bunch in turn. Each time a player is allowed to pick 1, 2 or 4 coins, and the player that gets the last coin is the winner. Assume that both players are very smart and he/she will try his/her best to work out a strategy to win the game. For example, if there are...

  • Billy and Cam are playing the following game: each player has a coin and decides whether...

    Billy and Cam are playing the following game: each player has a coin and decides whether to leave it as heads or tails before showdown (both player reveals their coin simultaneously). If both coins are heads, Billy wins $2. If both are tails, Billy wins $0.50. Otherwise, Cam wins $1. Find the optimal strategy for Billy.

  • A subtraction game Subtraction games are two-player games in which there is a pile of objects,...

    A subtraction game Subtraction games are two-player games in which there is a pile of objects, say coins. There are two players, Alice and Bob, who alternate turns subtracting 4.9. A SUBTRACTION GAME 19 from the pile some number of coins belonging to a set S (the subtraction set). Alice goes first. The first player who is unable to make a legal move loses. For example, suppose the initial pile contains 5 coins, and each player can, on his turn,...

  • The points problem

    (The Problem of the Points) Ari and Ben flip coins. If the coin shows head, Ari gets a point. If the coin shows tail, Ben gets a point. The player who is the first to get N points wins the game. After they have been playing for a while, Ari notices: „I still need n points to win.” Ben replies: „I need m points to win.”What is the probability that Ari wins the game?Hint: Ari suggests: „Let’s flip another n+m‐1...

  • What if I had an unfair coin flip game where a head had a likelihood of...

    What if I had an unfair coin flip game where a head had a likelihood of one in a million bu realization of $1,000,000 while a tail would mean $100,000 with the appropriate probability when the coin can only be one of two realizations. Find the expected value of this game and contrast your result with calculations associated with uninformed weights on the realizations What is the point?

  • (1 point) Consider a game played by flipping biased coins where the probability of heads is...

    (1 point) Consider a game played by flipping biased coins where the probability of heads is 0.14. You first choose the number of coins you want to flip You must pay $1.5 for each coin you choose to flip. You flip all the coins at the same time. You win $1000 if one or more coins comes up heads How many coins should you flip to maximize your expected profit? Answer: What is your maximum expected profit? Answer: $ (Your...

  • Probability Puzzle 3: Flipping Coins If you flip a coin 3 times, the probability of getting...

    Probability Puzzle 3: Flipping Coins If you flip a coin 3 times, the probability of getting any sequence is identical (1/8). There are 8 possible sequences: HHH, HHT, HTH, HTT, THH, THT, TTH, TTT Let's make this situation a little more interesting. Suppose two players are playing each other. Each player choses a sequence, and then they start flipping a coin until they get one of the two sequences. We have a long sequence that looks something like this: HHTTHTTHTHTTHHTHT.......

  • In a game, a single event consists of fair six-sided die begin thrown followed by flip...

    In a game, a single event consists of fair six-sided die begin thrown followed by flip of a fair two-sided coin. a. state the number of possible outcome in the sample space b. find the probability that a single randomly-selected turn will be include the coin toss coming up "heads" c.find the probability that a single randomly - selected turn in include a "6" coming up on the die d.find the probability that a single randomly-selected turn will include a...

  • Q.1 (25') Pony is playing coin tossing game with Yanny. They found the coin have 4...

    Q.1 (25') Pony is playing coin tossing game with Yanny. They found the coin have 4 heads and 6 tails in 10 flips. Let p be the probability for obtaining a head, based on the first 10 flips a) Can we conclude it is a biased or fair coin base on the result above? b) Plot the Bernoulli's PMF What is the probability for obtaining 6 heads in 10 flips using the same coin? d) What is the probability for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT