problem no 76:21 stick problem

  Question : lets say you have 21 sticks.There is a competion in between you and your computer.yours will be first turn.you can pick maximum 4 sticks .after your turn computer will pick some sticks.THE ONE WHO PICKS UP THE 21ST STICK WILL BE THE LOOSER.

       Now your task is to write the program in python in such a way that computer will not loose at any cost.

 
 
Answer:The  following program written in python  is in favour of computer.This program always 
makes computer win.
 
stick=21
rem_stick=21
while stick!=1:

print("its your turn")
print("note that you cannot pick more than 4 sticks")
p=int(input())
z=5-p
print("its my turn")
print("i will pick",z,"sticks")
rem_stick=rem_stick-(5)
print("only",rem_stick,"remaining now")
if(rem_stick==1):
break;
print("only one stick is remaining")
print("youre a looser ,i won")

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.