Playing cards game

半城伤御伤魂 提交于 2019-12-08 10:25:06

问题


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:


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


来源:https://stackoverflow.com/questions/31483662/playing-cards-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!