问题描述
这是我要做的事情:
这个问题涉及一个玩具熊的游戏。游戏开始的时候我会给你一些熊。然后你开始给我一些熊,但是你
必须遵循这些规则(其中n是你b
的熊数):
如果n是偶数,那么你可以准确地回馈n / 2只熊。 (提示:要测试
是否n是偶数,使用表达式((n%2)== 0)。)
如果n可以被3或4整除,然后你可以将n的最后两位数乘以
并将这许多熊给回来。 (顺便说一句,n $ / b $ b的最后一位是n%10,倒数第二位是(n%100)/ 10;这条规则可能不是$ b如果后两位数中的任何一位为0,则使用$ b。)
如果n可以被5整除,那么你可以准确地回馈42只熊。
游戏的目标是最终得到42只熊。
例如,假设你从250只熊开始。然后你可以这样做
做出这些动作:
从250头开始。
因为250可以被5整除,你可以返回42只熊,剩下
你有208只熊。
因为208是偶数,你可以退回一半的熊,留给你
104熊。
由于104是偶数,你可以退回一半熊,给你带来
52熊。
由于52是可以被4整除,你可以将最后两位数乘以
(得到10)并返回这10只熊。这给你带来42美元b $ b熊。
你已经达到了目标!
现在,你要写一个程序,如果我给你是熊,返回
如果你有可能赢得比赛,那就是真的。你的程序
必须使用递归检查所有可能的方式来申请
规则。
用法:
True
True
False
True
False
如你所见,我的程序必须使用递归。
我想出了这个想法,但我不确定它是否正确或是否有任何未成年人的b $ b我可以轻松解决的错误:
def bears(n):
如果n == 42:
返回True
如果n%5 == 0:
承担(n-42)
如果n%2 == 0:
承担(n / 2)
如果n%3 == 0或n%4 == 0:
one =(n%10)
two =((n%100)/ 10)
如果一个!= 0和两个!= 0:
承担(n-(一*) 2))
返回False
如果游戏达到42,则应返回True,否则为False。如果程序
从未命中42并返回True,则返回False。我想出了
基本情况,但是当我输入熊(250)时我仍然得到假。任何帮助
将非常感激!
这听起来非常类似于家庭作业,所以很可能
这里没有回答。 (也不应该在这里问过。)如果,在你试图编写这个程序的时候,你有一些关于
Python的问题,那么我鼓励问这些问题在这里。
加里赫伦
True
True
False
True
False
如你所见,我的程序必须使用递归。 />
我来了我有这个想法,但我不确定它是否正确或是否存在
我可以轻易解决的任何小错误:
def bears(n):
如果n == 42:
返回True
如果n%5 == 0:
承担( n-42)
如果n%2 == 0:
承担(n / 2)
如果n%3 == 0或n%4 == 0:
one =(n%10)
two =((n%100)/ 10)
if一个!= 0和两个!= 0:
承担(n-(一*二))
返回False
如果游戏命中42,则应返回True,否则返回False。如果程序
从未命中42并返回True,则返回False。我想出了
基本情况,但是当我输入熊(250)时我仍然得到假。任何帮助
将非常感激!
从概念上讲,使用递归表达更容易 - 但是每个基于递减的
递归算法都可以重写为使用迭代(和
反之亦然)。
你想:
返回熊(n - 42)
同上
idem
这听起来非常像家庭作业,
确实。
加里,你应该在回答之前阅读整个帖子。 OP''
尝试解决问题的方法如下,以及问题。
(剪辑)
here is what I have to do:
This question involves a game with teddy bears. The game starts when I
give you some bears. You then start giving me back some bears, but you
must follow these rules (where n is the number of bears that you
have):
If n is even, then you may give back exactly n/2 bears. (Hint: To test
whether n is even, use the expression ((n % 2) == 0).)
If n is divisible by 3 or 4, then you may multiply the last two digits
of n and give back this many bears. (By the way, the last digit of n
is n%10, and the next-to-last digit is (n%100)/10; this rule may not
be used if either of the last two digits is 0.)
If n is divisible by 5, then you may give back exactly 42 bears.
The goal of the game for you is to end up with EXACTLY 42 bears.
For example, suppose that you start with 250 bears. Then you could
make these moves:
Start with 250 bears.
Since 250 is divisible by 5, you may return 42 of the bears, leaving
you with 208 bears.
Since 208 is even, you may return half of the bears, leaving you with
104 bears.
Since 104 is even, you may return half of the bears, leaving you with
52 bears.
Since 52 is divisible by 4, you may multiply the last two digits
(resulting in 10) and return these 10 bears. This leaves you with 42
bears.
You have reached the goal!
Now, you are to write a program that, if I give you n bears, returns
true if it is at all possible for you to win the game. Your program
must use recursion to check all possible ways in which you can apply
the rules.
Usage:
True
True
False
True
False
As you see my program must use recursion.
I came up with this idea but I am not sure if its right or are there
any minor errors that I can easily fix:
def bears (n):
if n==42:
return True
if n%5==0:
bears(n-42)
if n%2==0:
bears(n/2)
if n%3==0 or n%4==0:
one = (n%10)
two = ((n%100)/10)
if one!=0 and two!=0:
bears(n-(one*two))
return False
If a game hits 42 it should return True, otherwise False. If program
never hits 42 and return True, then it returns False. I figured out
base case, but I still get False when I enter bears(250). Any help
would be very appreciated!
This sounds very much like a homework assignment, and so should probably
not be answered here. (Neither should it have been asked here.) If,
in your attempt to write this program, you have some questions about
Python, then I encourage to ask those questions here.
Gary Herron
True
True
False
True
False
As you see my program must use recursion.
I came up with this idea but I am not sure if its right or are there
any minor errors that I can easily fix:
def bears (n):
if n==42:
return True
if n%5==0:
bears(n-42)
if n%2==0:
bears(n/2)
if n%3==0 or n%4==0:
one = (n%10)
two = ((n%100)/10)
if one!=0 and two!=0:
bears(n-(one*two))
return False
If a game hits 42 it should return True, otherwise False. If program
never hits 42 and return True, then it returns False. I figured out
base case, but I still get False when I enter bears(250). Any help
would be very appreciated!
It''s conceptually easier to express using recursions - but every
recursion-based algorithm can be rewritten to use iteration (and
vice-versa).
You want:
return bears(n - 42)
idem
idem
This sounds very much like a homework assignment,
Indeed.
Garry, you should have read the whole post before answering. The OP''s
attempt at solving the problem was below, along with the question.
(snip)
这篇关于python递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!