EE_DSnP 板


LINE

※ 引述《ric2k1 (Ric)》之铭言: : Permutation without recursion 然後还要 ascending order 其实蛮困难的. : 抱歉当初出题时没有想清楚. : 想不出来可以不要写, : 或是参考下以下网页 (但是他只有 permutations, 没有 sorting): : http://www.geocities.com/permute_it/ Recursive function 的基本特性就是 1. The task itself is a series of repeated actions. 2. The series of the repeated actions can be characterized by certain index. 3. Each of the repeated action can be formulated by the index. For example, the Ficonacci series can be formulated as: f(n) = f(n-1)+f(n-2). The factorial can be formulated as f(n) = (n)*f(n-1). These are trivial cases. How to formulate the "permutation" function? 此外, recursive function 的基本作法有以下要点: 1. Identify the base case. For example, the base case for Fibonacci is when n = 1 or 0, while for factorial it is n = 1. 2. Assume the "recursed cases" have been completed. What is the task for this case in terms of the recursed results? For Fibonacci, it is just "addition", and for factorial, it is "multiplication". What's the base case for "permutation"? What is the task of permutation in terms of its recursed results? 以下不想被雷到的人可以按左键离开... 第一个问题, 假设我们要 permute n 个数字, 我们首先将它们排成一列 (in ascending order), >>> { x(0), x(1), ...., x(n-1) }, x(i) 表示数列中 index = i 的数字 permutation function 可用此数列中的 index 作为 recursive function 的 index. Let the recursive function called "permute(int i)". Obviously, the permutation task should start by calling "permute(0)". The recursive function is then: permute(i) = do_something_on_top_of (permute(i+1)); 第二个问题, The base case should be when there is only one number to permute. This is when the i = n-1 in permute(i). Clearly, there is only one way to permute when there is only one number. Please note that you should print out the permutation when you reach the base case. What to print? We will come back to this later. 第三个问题, 也是关键, permute(i) 与 permute(i+1) 的关系是什麽? 先想一下当 permute(i+1) return 回来, 就表示 以 { x(0)... x(i) } 为 prefix, 而对於 { x(i+1)... x(n-1) } 的所有 permutations 都已经产生过了, 接下来呢? 接下来就是从 { x(i+1)... x(n-1) } 中挑 "下一个没当过 x(i) 的最小" 的数目, say x(j), 来与 x(i) 交换, 然後再 call permute(i+1) 一次, 直到找不到没有做过的 "下一个数目", 就可以 return 这层 recursion. 前面问过在 base case 时要 print 什麽呢? 其实如果你 implement 的得宜的话, 就是把 swap 过的数列印出来就好了. 而且这样正好符合 ascending order. ===== FYI, 我写的 permute(int i) 的 body 包含印数字大概只有 20 行而已... ... 不是说你一定要写那麽短, 而是想告诉你不要害怕, 没那麽难!! 大家加油!! --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.121.132.121 ※ 编辑: ric2k1 来自: 59.121.132.121 (12/01 22:36)
1F:→ ric2k1:其他是I/O(读数字)以及class & helper functions (eg.swap) 12/01 22:58







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP