﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-beautykingdom-最新评论</title><link>http://www.cppblog.com/beautykingdom/CommentsRSS.aspx</link><description /><language>zh-cn</language><pubDate>Fri, 12 Oct 2012 01:17:38 GMT</pubDate><lastBuildDate>Fri, 12 Oct 2012 01:17:38 GMT</lastBuildDate><generator>cnblogs</generator><item><title>re: memcached完全剖析系列教程《转》</title><link>http://www.cppblog.com/beautykingdom/archive/2012/06/16/179023.html#179024</link><dc:creator>zgpxgame</dc:creator><author>zgpxgame</author><pubDate>Sat, 16 Jun 2012 01:23:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2012/06/16/179023.html#179024</guid><description><![CDATA[mark<img src ="http://www.cppblog.com/beautykingdom/aggbug/179024.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">zgpxgame</a> 2012-06-16 09:23 <a href="http://www.cppblog.com/beautykingdom/archive/2012/06/16/179023.html#179024#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 用prctl给线程命名</title><link>http://www.cppblog.com/beautykingdom/archive/2012/06/02/100419.html#177181</link><dc:creator>none</dc:creator><author>none</author><pubDate>Sat, 02 Jun 2012 03:34:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2012/06/02/100419.html#177181</guid><description><![CDATA[用top -H 可以的。<br>[root@linux-x86 test]# top -H<br>  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND<br>27036 root      25   0 16184  524  420 R 52.7  0.1   0:11.45 xx<br>26832 root      15   0 90116 3644 2896 R  5.3  0.5   0:00.44 sshd<br>    1 root      15   0 10348  744  624 S  0.0  0.1   0:00.75 init<br>    2 root      RT  -5     0    0    0 S  0.0  0.0   0:01.43 migration/0<br><img src ="http://www.cppblog.com/beautykingdom/aggbug/177181.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">none</a> 2012-06-02 11:34 <a href="http://www.cppblog.com/beautykingdom/archive/2012/06/02/100419.html#177181#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 用prctl给线程命名</title><link>http://www.cppblog.com/beautykingdom/archive/2012/05/27/100419.html#176408</link><dc:creator>dhao123@sina.com</dc:creator><author>dhao123@sina.com</author><pubDate>Sun, 27 May 2012 13:52:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2012/05/27/100419.html#176408</guid><description><![CDATA[请问大侠： 用top命令的时候可以显示修改后的线程名么？如何做呢？<img src ="http://www.cppblog.com/beautykingdom/aggbug/176408.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">dhao123@sina.com</a> 2012-05-27 21:52 <a href="http://www.cppblog.com/beautykingdom/archive/2012/05/27/100419.html#176408#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 解决Linux pthread_create内存泄漏问题</title><link>http://www.cppblog.com/beautykingdom/archive/2012/05/03/105664.html#173561</link><dc:creator>朱先生</dc:creator><author>朱先生</author><pubDate>Thu, 03 May 2012 02:07:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2012/05/03/105664.html#173561</guid><description><![CDATA[我试过，每一种方法有的时候不行。<br>第二种是可以的。<br><img src ="http://www.cppblog.com/beautykingdom/aggbug/173561.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">朱先生</a> 2012-05-03 10:07 <a href="http://www.cppblog.com/beautykingdom/archive/2012/05/03/105664.html#173561#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 著名程序库的比较和学习经验</title><link>http://www.cppblog.com/beautykingdom/archive/2011/12/12/105440.html#161968</link><dc:creator>buy dissertation</dc:creator><author>buy dissertation</author><pubDate>Mon, 12 Dec 2011 06:16:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2011/12/12/105440.html#161968</guid><description><![CDATA[Many university students have no writing skills, nevertheless they still need to compose masters thesis. What should they do in such situation? I recommend to look for a support at the buy thesis service, just because this really works.  <img src ="http://www.cppblog.com/beautykingdom/aggbug/161968.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">buy dissertation</a> 2011-12-12 14:16 <a href="http://www.cppblog.com/beautykingdom/archive/2011/12/12/105440.html#161968#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: linux的消息队列与共享内存编程</title><link>http://www.cppblog.com/beautykingdom/archive/2011/05/27/120300.html#147345</link><dc:creator>朱志超</dc:creator><author>朱志超</author><pubDate>Fri, 27 May 2011 03:01:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2011/05/27/120300.html#147345</guid><description><![CDATA[内容选择得很好，谢谢<img src ="http://www.cppblog.com/beautykingdom/aggbug/147345.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">朱志超</a> 2011-05-27 11:01 <a href="http://www.cppblog.com/beautykingdom/archive/2011/05/27/120300.html#147345#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 著名程序库的比较和学习经验</title><link>http://www.cppblog.com/beautykingdom/archive/2010/06/15/105440.html#117976</link><dc:creator>LillianHancock</dc:creator><author>LillianHancock</author><pubDate>Tue, 15 Jun 2010 09:04:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2010/06/15/105440.html#117976</guid><description><![CDATA[Your hot enough research close to this post comes side by side with the thesis topics. Thus, you can even work for &lt;a href=&quot;<a target="_new" href="http://www.master-dissertations.com&quot;&gt;thesis">http://www.master-dissertations.com&quot;&gt;thesis</a> writing service&lt;/a&gt;. <img src ="http://www.cppblog.com/beautykingdom/aggbug/117976.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">LillianHancock</a> 2010-06-15 17:04 <a href="http://www.cppblog.com/beautykingdom/archive/2010/06/15/105440.html#117976#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 解决Linux pthread_create内存泄漏问题[未登录]</title><link>http://www.cppblog.com/beautykingdom/archive/2010/06/02/105664.html#117026</link><dc:creator>jack</dc:creator><author>jack</author><pubDate>Wed, 02 Jun 2010 09:08:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2010/06/02/105664.html#117026</guid><description><![CDATA[不错，支持一个。<img src ="http://www.cppblog.com/beautykingdom/aggbug/117026.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">jack</a> 2010-06-02 17:08 <a href="http://www.cppblog.com/beautykingdom/archive/2010/06/02/105664.html#117026#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 浅谈游戏服务器---功能模块上来看[未登录]</title><link>http://www.cppblog.com/beautykingdom/archive/2010/02/02/106871.html#107001</link><dc:creator>cppexplore</dc:creator><author>cppexplore</author><pubDate>Tue, 02 Feb 2010 06:08:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2010/02/02/106871.html#107001</guid><description><![CDATA[不错 好文!!  期待博主继续<img src ="http://www.cppblog.com/beautykingdom/aggbug/107001.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">cppexplore</a> 2010-02-02 14:08 <a href="http://www.cppblog.com/beautykingdom/archive/2010/02/02/106871.html#107001#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 全面整理的C++面试题</title><link>http://www.cppblog.com/beautykingdom/archive/2009/12/13/102633.html#103080</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Sat, 12 Dec 2009 16:39:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2009/12/13/102633.html#103080</guid><description><![CDATA[4。分析一下<br>#include&lt;iostream.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;malloc.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;memory.h&gt;<br>typedef struct  AA<br>{<br>        int b1:5;<br>        int b2:2;<br>}AA;<br>void main()<br>{<br>       AA aa;<br>       char cc[100];<br>       strcpy(cc,&quot;0123456789abcdefghijklmnopqrstuvwxyz&quot;);<br>       memcpy(&amp;aa,cc,sizeof(AA));<br>       cout &lt;&lt; aa.b1 &lt;&lt;endl;<br>       cout &lt;&lt; aa.b2 &lt;&lt;endl;<br>}<br><br>答案： -16和１<br>首先sizeof(AA)的大小为4,b1和b2分别占5bit和2bit.<br>经过strcpy和memcpy后,aa的4个字节所存放的值是:<br>0,1,2,3的ASC码，即00110000,00110001,00110010,00110011<br>所以，最后一步：显示的是这４个字节的前５位，和之后的２位<br>分别为：10000,和01<br>因为int是有正负之分　　所以是-16和１<br><br>5。求函数返回值，输入x=9999; <br>int func （ x ）<br>{ <br>    int countx = 0; <br>    while ( x ) <br>    { <br>        countx ++; <br>        x = x&(x-1); <br>    } <br>    return countx; <br>} <br>结果呢？<br><br>答案：知道了这是统计9999的二进制数值中有多少个1的函数，且有<br>9999＝9×1024＋512＋256＋15<br><br>9×1024中含有1的个数为2；<br>512中含有1的个数为1；<br>256中含有1的个数为1；<br>15中含有1的个数为4；<br>故共有1的个数为8，结果为8。<br>1000 - 1 = 0111，正好是原数取反。这就是原理。<br>用这种方法来求1的个数是很效率很高的。<br>不必去一个一个地移位。循环次数最少。<br><br>6。int a,b,c 请写函数实现C=a+b ,不可以改变数据类型,如将c改为long int,关键是如何处理溢出问题<br>答案：bool add (int a, int b,int *c)<br>{<br>*c=a+b;<br>return (a>0 && b>0 &&(*c<a || *c<b) || (a<0 && b<0 &&(*c>a || *c>b)));<br>}<br>这里，第三个或条件没看明白，觉得逻辑上出现不了啊。<br><br>7。分析：<br>struct bit <br>{   int a:3; <br>    int  b:2; <br>    int c:3; <br>}; <br>int main() <br>{ <br>  bit s; <br>  char *c=(char*)&s; <br>   cout<<sizeof(bit)<<endl;<br>  *c=0x99;<br>   cout << s.a <<endl <<s.b<<endl<<s.c<<endl; <br>     int a=-1;<br>   printf("%x",a);<br>  return 0; <br>} <br>输出为什么是？<br><br>答案：4<br>1<br>-1<br>-4<br>ffffffff<br>因为0x99在内存中表示为 100 11 001 , a = 001, b = 11, c = 100（在vc环境中，一般是由右到左进行分配的）<br>当c为有符合数时, c = 100, 最高1为表示c为负数，负数在计算机用补码表示，所以c = -4;同理 <br>b = -1;<br>当c为有符合数时, c = 100,即 c = 4,同理 b = 3<br><br>8。改错：<br>#include <stdio.h><br><br>int main(void) {<br><br>    int **p;<br>    int arr[100];<br><br>    p = &arr;<br><br>    return 0;<br>}<br><br>答案：搞错了,是指针类型不同,<br>int **p; //二级指针<br>&arr; //得到的是指向第一维为100的数组的指针<br>应该这样写#include <stdio.h><br>int main(void) {<br>int **p, *q;<br>int arr[100];<br>q = arr;<br>p = &q;<br>return 0;<br><br>9。下面这个程序执行后会有什么错误或者效果:<br> #define MAX 255<br> int main()<br>{<br>   unsigned char A[MAX],i; //i被定义为unsigned char<br>   for (i=0;i<=MAX;i++)<br>      A[i]=i;<br>}<br><br>答案：死循环加数组越界访问（C/C++不进行数组越界检查）<br>MAX=255 <br>数组A的下标范围为:0..MAX-1,这是其一..<br>其二.当i循环到255时,循环内执行:<br>  A[255]=255;<br>这句本身没有问题..但是返回for (i=0;i<=MAX;i++)语句时,<br>由于unsigned char的取值范围在(0..255),i++以后i又为0了..无限循环下去.<br><br>11。struct name1{<br>   char  str;<br>   short x;<br>   int   num;<br>}<br><br>struct name2{<br>   char str;<br>   int num;<br>   short x;<br>}<br><br>sizeof(struct name1)=？？,sizeof(struct name2)=？？<br><br>答案：sizeof(struct name1)=8,sizeof(struct name2)=12<br>在第二个结构中，为保证num按四个字节对齐，char后必须留出3字节的空间；同时为保证整个结构的自然对齐（这里是4字节对齐），在x后还要补齐2个字节，这样就是12字节。<br><img src ="http://www.cppblog.com/beautykingdom/aggbug/103080.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2009-12-13 00:39 <a href="http://www.cppblog.com/beautykingdom/archive/2009/12/13/102633.html#103080#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 微软面试中简单的算法题目(转)</title><link>http://www.cppblog.com/beautykingdom/archive/2009/12/06/102675.html#102676</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Sun, 06 Dec 2009 15:33:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2009/12/06/102675.html#102676</guid><description><![CDATA[1．烧一根不均匀的绳子，从头烧到尾总共需要 1 个小时，问如何用烧绳子<br>的方法来确定半小时的时间呢？<br>2．10 个海盗抢到了100 颗宝石，每一颗都一样大小且价值连城。他们决定<br>这么分：<br>（1）抽签决定自己的号码（1~10）；<br>（2）首先，由1 号提出分配方案，然后大家表决，当且仅当超过半数的人<br>同意时，按照他的方案进行分配，否则将被扔进大海喂鲨鱼；<br>（3）如果1 号死后，再由2 号提出分配方案，然后剩下的4 个人进行表决，<br>当且仅当超过半数的人同意时，按照他的方案进行分配，否则将被扔入大海喂鲨<br>鱼；<br>（4）依此类推??<br>条件：每个海盗都是很聪明的人，都能很理智地做出判断，从而做出选择。<br>问题：第一个海盗提出怎样的分配方案才能使自己的收益最大化？<br>3．为什么下水道的盖子是圆的？<br>4．中国有多少辆汽车？<br>5．你让工人为你工作7 天，回报是一根金条，这根金条平分成相连的7 段，<br>你必须在每天结束的时候给他们一段金条。如果只允许你两次把金条弄断，你如<br>何给你的工人付费？<br>6．有一辆火车以每小时15 公里的速度离开北京直奔广州，同时另一辆火车<br>以每小时20 公里的速度从广州开往北京。如果有一只鸟，以30 公里每小时的速<br>度和两辆火车同时启动，从北京出发，碰到另一辆车后就向相反的方向返回去飞，<br>就这样依次在两辆火车之间来回地飞，直到两辆火车相遇。请问，这只鸟共飞行<br>了多长的距离？<br>7．你有两个罐子以及50 个红色弹球和50 个蓝色弹球，随机选出一个罐子，<br>随机选出一个弹球放入罐子，怎样给出红色弹球最大的选中机会？在你的计划<br>里，得到红球的几率是多少？<br>8．想像你站在镜子前，请问，为什么镜子中的影像可以左右颠倒，却不能<br>上下颠倒呢？<br>9．如果你有无穷多的水，一个3 公升的提捅，一个5 公升的提捅，两只提<br>捅形状上下都不均匀，问你如何才能准确称出4 公升的水？<br>10．你有一桶果冻，其中有黄色、绿色、红色三种，闭上眼睛抓取同种颜色<br>的两个。抓取多少次就可以确定你肯定有两个同一颜色的果冻？<br>11．连续整数之和为1000 的共有几组？<br>12．从同一地点出发的相同型号的飞机，可是每架飞机装满油只能绕地球飞<br>半周，飞机之间可以加油，加完油的飞机必须回到起点。问至少要多少架次，才<br>能满足有一架绕地球一周。<br>参考答案：<br>1．两边一起烧。<br>2．96，0，1，0，1，0，1，0，1，0。<br>3．因为口是圆的。<br>4．很多。<br>5．分1，2，4。<br>6．6/7 北京到广州的距离。<br>7．100%。<br>8．平面镜成像原理（或者是“眼睛是左右长的”）。<br>9．3 先装满，倒在5 里，再把3 装满，倒进5 里。把5 里的水倒掉，把3 里<br>剩下的水倒进5 里，再把3 装满，倒进5 里，ok！<br>10．一次。<br>11．首先1000 为一个解。连续数的平均值设为x，1000 必须是x 的整数倍。<br>假如连续数的个数为偶数个，x 就不是整数了。x 的2 倍只能是5，25，125 才行。<br>因为平均值为12.5,要连续80 个达不到。125/2=62.5 是可以的。即62，63，61，<br>64，等等。连续数的个数为奇数时，平均值为整数。1000 为平均值的奇数倍。<br>1000=2&#215;2&#215;2&#215;5&#215;5&#215;5；x 可以为2，4，8，40，200 排除后剩下40 和200 是<br>可以的。所以答案为平均值为62.5，40，200，1000 的4 组整数。<br>12．答案是5 架次。一般的解法可以分为如下两个部分：<br>（1）直线飞行<br>一架飞机载满油飞行距离为1，n 架飞机最远能飞多远？在不是兜圈没有迎<br>头接应的情况，这问题就是n 架飞机能飞多远？存在的极值问题是不要重复飞<br>行，比如两架飞机同时给一架飞机加油且同时飞回来即可认为是重复，或者换句<br>话说，离出发点越远，在飞的飞机就越少，这个极值条件是显然的，因为n 架飞<br>机带的油是一定的，如重复，则浪费的油就越多。比如最后肯定是只有一架飞机<br>全程飞行，注意“全程”这两个字，也就是不要重复的极值条件。如果是两架飞<br>机的话，肯定是一架给另一架加满油，并使剩下的油刚好能回去，就说第二架飞<br>机带的油耗在3 倍于从出发到加油的路程上，有三架飞机第三架带的油耗在5<br>倍于从出发到其加油的路程上，所以n 架飞机最远能飞行的距离为s=1+1/3+?<br>+1/（2n+1）这个级数是发散的，所以理论上只要飞机足够多最终可以使一架飞<br>机飞到无穷远，当然实际上不可能一架飞机在飞行1/（2n+1）时间内同时给n?1<br>个飞机加油。<br>（2）可以迎头接应加油<br>一架飞机载满油飞行距离为1/2，最少几架飞机能飞行距离1？也是根据不<br>要重复飞行的极值条件，得出最远处肯定是只有一架飞机飞行，这样得出由1/2<br>处对称两边1/4 肯定是一架飞机飞行，用上面的公式即可知道一边至少需要两架<br>飞机支持，（1/3+1/5）/2&gt;1/4（左边除以2 是一架飞机飞行距离为1/2），但<br>是有一点点剩余，所以想像为一个滑轮（中间一个飞机是个绳子，两边两架飞机<br>是个棒）的话，可以滑动一点距离，就说加油地点可以在一定距离内变动（很容<br>易算出来每架飞机的加油地点和加油数量，等等）<img src ="http://www.cppblog.com/beautykingdom/aggbug/102676.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2009-12-06 23:33 <a href="http://www.cppblog.com/beautykingdom/archive/2009/12/06/102675.html#102676#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: Browsers, processes, cookies and session state</title><link>http://www.cppblog.com/beautykingdom/archive/2009/04/07/78836.html#79138</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Mon, 06 Apr 2009 16:33:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2009/04/07/78836.html#79138</guid><description><![CDATA[每个IE Instance该是不同的进程吧，可以获取进程ID，在每个instance里建一个名称包含进程id的目录名，就可以分目录存储了吧。<img src ="http://www.cppblog.com/beautykingdom/aggbug/79138.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2009-04-07 00:33 <a href="http://www.cppblog.com/beautykingdom/archive/2009/04/07/78836.html#79138#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: Browsers, processes, cookies and session state</title><link>http://www.cppblog.com/beautykingdom/archive/2009/04/05/78836.html#79013</link><dc:creator>domolo</dc:creator><author>domolo</author><pubDate>Sun, 05 Apr 2009 07:57:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2009/04/05/78836.html#79013</guid><description><![CDATA[<br>文章说的很清楚，多谢<br><br>我有一个问题：<br>如何为每个ie instance ie实例的 Persistent cookies cookie 指定不同的存储目录？<img src ="http://www.cppblog.com/beautykingdom/aggbug/79013.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">domolo</a> 2009-04-05 15:57 <a href="http://www.cppblog.com/beautykingdom/archive/2009/04/05/78836.html#79013#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 从一道面试题看指针与数组的区别 </title><link>http://www.cppblog.com/beautykingdom/archive/2008/09/15/58105.html#61843</link><dc:creator>路过</dc:creator><author>路过</author><pubDate>Mon, 15 Sep 2008 03:06:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2008/09/15/58105.html#61843</guid><description><![CDATA[一个字，强！<img src ="http://www.cppblog.com/beautykingdom/aggbug/61843.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">路过</a> 2008-09-15 11:06 <a href="http://www.cppblog.com/beautykingdom/archive/2008/09/15/58105.html#61843#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 一个关于单向链表的面试题</title><link>http://www.cppblog.com/beautykingdom/archive/2008/09/14/61826.html#61828</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Sun, 14 Sep 2008 15:42:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2008/09/14/61826.html#61828</guid><description><![CDATA[还有一种算法，就是用有向图来实现(具体见下面代码)：<br>把链表看成一个有向图，深度优先遍历该有向图，判断有无循环出现。<br><br>懒得再用中文写一遍具体算法了，看下面的代码实现吧，英文注释解释的很清楚了。<br><br><br><br>时间复杂度 O(e), 链表边的总数。<br><br>空间复杂度 O(1).<br><br>有向图采用邻接表实现。<br><br><br>/* file: DFSDetectLoop.cpp */<br><br>/* <br><br> *  Detect if the graph has loop -- For both Undigraph and digraph<br><br> *  Complexity: O(e); e is the number of arcs in Graph.<br><br> *   <br><br> *  BUG Reported:<br><br> *  1. Apr-26-07<br><br> *     Not support Undigraph yet ! Fix me !!!<br><br> *     - Fixed on Apr-26-08.<br><br> *  <br><br> *  Return<br><br> *  1 - Loop detected.<br><br> *  0 - No loop detected.     <br><br> *  *<br><br> *  Algrithm:<br><br> *  1. Init all the nodes color to WHITE.<br><br> *  2. DFS graph<br><br> *      For each the nodes v in graph, do step (1) and (2).<br><br> *      (1) If v is WHITE, DFS from node v:<br><br> *          (a) Mark v as GRAY.   <br><br> *          (b) For every nodes tv adjacent with node v,<br><br> *              (i)   If the current visiting node is gray, then loop detected. exit.<br><br> *              (ii)  Goto Step (1).<br><br> *              (iii) All the nodes on sub-tree of tv have been visited. Mark node tv as BLACK.<br><br> *       (2) All the nodes on sub-tree of v have been visited. Mark node v as BLACK.<br><br> *       <br><br> * Function DFSDetectLoop is valid for both Undigraph and digraph.<br><br> * <br><br> * */<br><br>int DFSDetectLoop (ALGraph *graph, int VisitFunc (ALGraph *graph, int v))<br><br>{<br><br>    int v;<br><br><br><br>    for (v = 0; v &lt; graph-&gt;vexnum; v++)<br><br>    {   <br><br>        MarkNodeColor (graph, v, WHITE);<br><br>    }<br><br>    for (v = 0; v &lt; graph-&gt;vexnum; v++)<br><br>    {   <br><br>        if (graph-&gt;vertices[v].color == WHITE)<br><br>        {   <br><br>            /* We are good to call DFSDetectLoopSub the first<br><br>             * time with pv = -1, because no node equals -1.<br><br>             * */ <br><br>            if (1 == DFSDetectLoopSub (graph, v, -1, VisitFunc))<br><br>                return 1;<br><br>        }<br><br>        MarkNodeColor (graph, v, BLACK);<br><br>    }<br><br>    return 1;<br><br>}<br><br><br><br>/*          <br><br> * Start from node v, DFS graph to detect loop. <br><br> *  pv is the node that just visited v. pv is used to avoid v to visit pv again.<br><br> *  pv is introduced to support Undigraph.<br><br> *  <br><br> * NOTE: <br><br> *      Before calling DFSDetectLoopSub, make sure node v is not visited yet.<br><br> * */<br><br>int DFSDetectLoopSub (ALGraph *graph, int v, int pv, int VisitFunc (ALGraph *graph, int v))<br><br>{<br><br>   assert (graph-&gt;vertices[v].color == WHITE);<br><br><br><br>   MarkNodeColor (graph, v, GRAY);<br><br><br><br>   VisitFunc (graph, v);<br><br><br><br>   ArcNode *arc;<br><br>   arc = graph-&gt;vertices[v].firstarc;<br><br>   while (arc)<br><br>   {<br><br>        int tv = arc-&gt;adjvex;<br><br><br><br>        /* For Undigraph, if tv equals pv, this arc should not be count. <br><br>         * Because we have just visited from pv to v.<br><br>         * Just go ahead to check next vertex connected with v.<br><br>         * 1----2, after visit 1, we will visit 2, while visiting 2, 1 will be the 1st node visited.<br><br>         * <br><br>         * For digraph, we need to check loop even tv equals pv.<br><br>         * Because there is case that node v points to u, and u points to v.<br><br>         * */<br><br>        if ((graph-&gt;kind == AG) &amp;&amp; (tv != pv))<br><br>        {<br><br>            if ( graph-&gt;vertices[tv].color == GRAY )<br><br>            {<br><br>                cout &lt;&lt; &quot;Gray node visited at node: &quot; &lt;&lt; tv + 1 &lt;&lt;endl;<br><br>                cout &lt;&lt; &quot;DFSDetectLoopSub: Loop Detected at from node &quot; &lt;&lt; v + 1&lt;&lt;&quot; to &quot;&lt;&lt; tv + 1 &lt;&lt;&quot; !&quot; &lt;&lt;endl;<br><br>                return 1;<br><br>            }<br><br><br><br>            if (graph-&gt;vertices[tv].color == WHITE)<br><br>            {<br><br>                if (1 == DFSDetectLoopSub (graph, tv, v, VisitFunc))<br><br>                {<br><br>                    return 1;<br><br>                }<br><br>            }<br><br>            /* At this line: <br><br>             * (1)If tv's color is already BLACK; Go ahead checking next arc; <br><br>             * (2)If the sub-tree of node tv has all been visited, mark as BLACK and check next arc;<br><br>             * Backward tv to to v's other adjacent node. So tv should be marked as black.<br><br>             * */<br><br>            MarkNodeColor (graph, tv, BLACK);<br><br>        }<br><br><br><br>        arc = arc-&gt;nextarc;<br><br>   }<br><br>   return 0;<br><br>}<br><img src ="http://www.cppblog.com/beautykingdom/aggbug/61828.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2008-09-14 23:42 <a href="http://www.cppblog.com/beautykingdom/archive/2008/09/14/61826.html#61828#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>