📣 VIP通行证夏日特惠 限时立减$68
查看: 2222| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

Recursion题目求解

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 kakachelsea 于 2014-7-6 10:20 编辑

最近在自学CS61A(python),做homework3的时候遇到一道题,想了半天也解决不了,希望大家帮忙想个算法。。。谢谢!
题目的关键是不能有任何assignment语句,也就是要用recursion,可我想不出做法。
题目如下:

Write a function that takes a positive integer n and returns the number of ten-pairs it contains. A ten-pair is a pairs of digits within n that sum to 10. Do not use any assignment statements.

The number 7,823,952 has 3 ten-pairs. The first and fourth digits sum to 7+3=10, the second and third digits sum to 8+2=10, and the second and last digit sum to 8+2=10:


def ten_pairs(n):    """Return the number of ten-pairs within positive integer n.   
>>> ten_pairs(7823952)   
3   
>>> ten_pairs(55055)   
6   
>>> ten_pairs(9641469)   
6   
"""


评分

参与人数 1大米 +3 收起 理由
lindaxia2003 + 3 给自学的伙伴来加分

查看全部评分


上一篇:【学JAVA中】问一下八皇后问题,是在看不懂了
下一篇:如果这样的类生成,该怎么办?
🔗
pyemma 2014-7-6 12:35:14 | 只看该作者
全局:
递归参数中设置一项保存当前和来进行判断呢,就是类似dfs(int i, int sum)这样
回复

使用道具 举报

🔗
 楼主| kakachelsea 2014-7-6 13:15:09 | 只看该作者
全局:
pyemma 发表于 2014-7-6 12:35
递归参数中设置一项保存当前和来进行判断呢,就是类似dfs(int i, int sum)这样

这个我倒是想到了,但是我现在纠结于base case 和 recursive case怎么写。。。

我想了一些方法,但是感觉都不行。。
回复

使用道具 举报

🔗
 楼主| kakachelsea 2014-7-6 13:15:20 | 只看该作者
全局:
pyemma 发表于 2014-7-6 12:35
递归参数中设置一项保存当前和来进行判断呢,就是类似dfs(int i, int sum)这样

这个我倒是想到了,但是我现在纠结于base case 和 recursive case怎么写。。。

我想了一些方法,但是感觉都不行。。
回复

使用道具 举报

🔗
grassgigi 2014-7-6 14:05:25 | 只看该作者
全局:
本帖最后由 grassgigi 于 2014-7-6 14:07 编辑

my random thoughts...:

the recursion will start from the left most digit and go right (e.g. '7' for 7823952)
for each digit D, it will check it's sum with each of its right side digits(for 7, it checks 7+8, 7+2.....7+2)

regradless of how it is implemented, you should keep tracking three things in the recursion call:
1) the position of digit D
2) the position of right side digit that is summing with D in this recursion (Let's call it X)
3) total number of ten-pairs

the base case would be:
if digit X is the right most bit(e.g. '2' in 7823952), we right-shift digit D to its right neighbour
if digit D is the least significant bit, it means all digit pairs are compared, we can exit the call and return the total number of ten-pairs.
回复

使用道具 举报

🔗
 楼主| kakachelsea 2014-7-6 14:16:43 | 只看该作者
全局:
grassgigi 发表于 2014-7-6 14:05
my random thoughts...:

the recursion will start from the left most digit and go right (e.g. '7' f ...

Thank you first for your help!

So how do you keep track of the position of digit D and X? Sequences like string and list are not taught prior to this homework, which means that this problem may be solved without using sequence selection.

Can you elaborate on that?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表