Programmer

Thursday, 29 March 2018

Flip Coin Program


Little Elephant was fond of inventing new games. After a lot of research, Little Elephant came to know that most of the animals in the forest were showing less interest to play the multi-player games.Little Elephant had started to invent single player games, and succeeded in inventing the new single player game named COIN FLIP.

In this game the player will use N coins numbered from 1 to N, and all the coins will be facing in "Same direction" (Either Head or Tail),which will be decided by the player before starting of the game.

The player needs to play N rounds.In the k-th round the player will flip the face of the all coins whose number is less than or equal to k. That is, the face of coin i will be reversed, from Head to Tail, or, from Tail to Head, for i = k.

Elephant needs to guess the total number of coins showing a particular face after playing N rounds. Elephant really becomes quite fond of this game COIN FLIP, so Elephant plays G times. Please help the Elephant to find out the answer.

Example:
******* 
Arrangement of coins are H H H H H, and now Elephant will play 5 rounds and coin faces will be changed as follows
After the 1st Round: T H H H H
After the 2nd Round: H T H H H
After the 3rd Round: T H T H H
After the 4th Round: H T H T H
After the 5th Round: T H T H T


SOLUTION:
**********
#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
int n, i, j, k;
char ch, str[20];
clrscr();
cout<<"Enter No. of Coins : ";
cin>>n;
cout<<"Enter face of coin (T/ H): ";
cin>>ch;
for(i=0;i<n;i++)
{
str[i]=ch;
cout<<str[i]<<" ";
}
cout<<endl<<endl<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(str[j]=='H')
{
str[j]='T';
}
else if(str[j]=='T')
{
str[j]='H';
}
cout<<str[j]<<" ";
}
for(k=j;k<n;k++)
{
cout<<str[k]<<" ";
}
cout<<endl;
}
getch();
}

No comments:

Post a Comment

Program To Print Particular Line From File.

#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { ifstream fin; int count=0,num,l=1; char ...