玩纸牌游戏 [英] Playing cards game

查看:78
本文介绍了玩纸牌游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我在一张圆桌中放置了N张从1到N的卡,这样卡1在卡2和卡N之间.所有卡最初都是上下颠倒的.目的是使所有卡面朝上.

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.

比方说,我们触摸卡i,然后触摸卡i会使卡i-1,i,i + 1朝上.同样,触摸卡片N将使卡片N-1,N,1st正面朝上. 我想确定使所有卡面朝上所需的最少触摸次数.

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.

这是我在python中尝试过的

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可能高达10 ^ 20.

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.

推荐答案

应该类似于以下内容:

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

这篇关于玩纸牌游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆