Playing cards game

2019-06-11 17:07发布

Here I have N number of cards numbered from 1 to N placed in a round table such that card 1 is between card 2 and card N . All the cards are initially upside down. The aim is to turn all the cards face up.

Let's say we touch a card i, touching the card i will turn the cards i-1,i,i+1 face up. similarly touching the card N will turn the cards N-1,N,1st card face up. I want to determine the minimum number of touches required to face up all the cards.

here is what i have been trying in python

q = int(raw_input())
if q==1 or q==2:
   print "1"
else:
   r = q%3
   l = q/3
   print r+l

q can be as big as 10^20.

What's wrong with above logic and In case the above logic is completely wrong what should be the correct approach.

1条回答
Emotional °昔
2楼-- · 2019-06-11 17:37

It should be something along the lines of:

answer = q / 3 (+ 1 if q is not a multiple of 3)

So an easy way to neat way to code this'd be:

q = int (raw_input()) # This isn't safe since it causes Type Errors easily but... whatever...
print (q / 3) + 1 * (q % 3 > 0) # Because 1 * True = 1, 1 * False = 0
查看更多
登录 后发表回答