|
|
Recursive vs. Looping (WHILE, FOR.. etc)
[复制链接]
|
|
|
发表于 18-8-2011 01:03 AM
|
显示全部楼层
Recursive 長期 looping 會 stack overflow 的.
馬拉棧 发表于 8-7-2011 09:18 PM 
如果你设计到这样。就算用loop 或while, 也一样overflow .
还是关心好程式怎么设计吧。 |
|
|
|
|
|
|
|
|
|
|
发表于 18-8-2011 09:21 AM
|
显示全部楼层
回复 hooi1983
可能是小弟的心理作用吧,以前读书时,我写IF,老师给少分,写case的比较高分,还 ...
forexen 发表于 4-7-2011 06:05 PM 
可能你用错地方了 |
|
|
|
|
|
|
|
|
|
|
发表于 18-8-2011 09:52 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 18-8-2011 10:00 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 31-12-2011 07:50 PM
|
显示全部楼层
回复 forexen
最后再重申:switch适用于条件为single numeric的检测例如var == 1,var==2,var==‘4’,而if...else则适用于non numeric分歧检测,例如string == “Hello”,在适当的地方使用适当的方式,那才是获得高分的理由。
geekman 发表于 18-8-2011 09:52 AM 
你给的non numeric分歧检测好像有点不恰当因为switch也能 string == “Hello” 啊
比如多重compare,如以下:
 |
|
|
|
|
|
|
|
|
|
|
发表于 12-1-2012 10:40 AM
|
显示全部楼层
本帖最后由 苦瓜汤 于 12-1-2012 10:41 AM 编辑
回复 16# forexen
回复 14# forexen
你觉得这个比较高分,
- if (animal == "golden retriever" || animal == "labrado"){
- bark();
- }else if(animal == "pigeon" || animal == "eagle" || animal == "sparrow"){
- chirps();
- }else if(animal == "siamese" || animal == "persian"){
- meow();
- }else{
- talk();
- }
复制代码 还是这个?
- switch(animal){
- case "golden retriever":
- case "labrado":
- bark();
- break;
- case "pigeon":
- case "eagle":
- case "sparrow":
- chirps();
- break;
- case "siamese":
- case "persian":
- meow();
- break;
- default:
- talk();
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 12-1-2012 04:36 PM
|
显示全部楼层
都是背多分..... |
|
|
|
|
|
|
|
|
|
|
发表于 8-10-2012 09:32 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 23-11-2012 12:08 AM
|
显示全部楼层
不同的时候时候适合不同的做法。
do {
至少做一次
} while ( 检查条件 )
==================
while ( 检查条件 ) {
条件符合才做
}
==================
for ( i = 0 ; i < 固定或半固定次数 ; i ++ ) {
处理
}
==================
foreach ( i in arrayi ) {
处理 i, 属于 arrayi 的一个单位
}
==================
而 recursive 大多使用多层、蜘蛛网状式搜查
int loopcount ( icount, data ) {
if ( data ... ) {
icount = loopcount( icount+ 1, data[+1] ) ;
} else {
}
return icount ;
}
==================
不同的分叉、判断也需要用不同的方法
if ( a == 1 ) {
if ( b == 2 ) {
处理 a = 1, b != 2
} else if ( c == 3 ) {
处理 a=1, b=2, c=3
} // endif c
} // endif b
} // endif a
==================
switch ( sel ) {
case " 1 " :
选择 1
break;
case " 2 " :
选择 2
break;
case " 3 " :
选择 3
break;
default :
其他
break;
}
本帖最后由 flashang 于 23-11-2012 12:22 AM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 14-12-2012 11:36 AM
|
显示全部楼层
给我是用LOOP
Recursion是很方便但是有时候数据太大会导致Stack Overflow
像MSSQL里的StoredProc就只限制32个Recursion
我个人经验是通过数据库来得到自己要的排列,然后通过LOOP来做出自己要的东西
|
|
|
|
|
|
|
|
|
|
|
发表于 15-3-2013 04:23 PM
|
显示全部楼层
chrizyuen2 发表于 22-3-2011 10:54 PM 
1) recursive loop 根本不能做 endless looping 或长周期 looping
2) 我直接用thread, 没事做,就让t ...
1) 未必。recusrive和iteration是可以对换的。
Endless loop- for(;;) {
- // do something
- }
复制代码 Recursive loop 和 iteration 都可已设置terminating condition.如过你不terminate,iteration是可以重复一辈子。 |
|
|
|
|
|
|
|
|
|
|
发表于 15-3-2013 05:57 PM
|
显示全部楼层
hongster 发表于 15-3-2013 04:23 PM 
1) 未必。recusrive和iteration是可以对换的。
Endless loopRecursive loop 和 iteration 都可已设置 ...
我想把C:\ folder include subfolder内的全部的files名字 放进一个List内。
你写个平常loop给我看看。- static void DirSearch(string sDir)
- {
- try
- {
- foreach (string d in Directory.GetDirectories(sDir))
- {
- foreach (string f in Directory.GetFiles(d))
- {
- Console.WriteLine(f);
- }
- DirSearch(d);
- }
- }
- catch (System.Exception excpt)
- {
- Console.WriteLine(excpt.Message);
- }
- }
复制代码 本帖最后由 chrizyuen2 于 15-3-2013 06:04 PM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 20-3-2013 10:06 AM
|
显示全部楼层
我使用PHP码做示范。我的答案关键在于queue数据结构的使用,我把$dirs当queue使用。- <?php
- function dirSearch($dir) {
- $dir = rtrim($dir, '/'); // Optional
- $dirs = array($dir);
- while ($currentDir = array_shift($dirs)) {
- foreach (scandir($currentDir) as $file) {
- if (('.' == $file) || ('..' == $file))
- continue;
- $path = $currentDir.'/'.$file;
- if (is_dir($path)) {
- $dirs[] = $path;
- continue;
- }
- else {
- echo $path."\n";
- }
- }
- }
- }
- ?>
复制代码 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|