??xml version="1.0" encoding="utf-8" standalone="yes"?>久久无码人妻一区二区三区,欧美亚洲日本久久精品,久久久久久国产精品无码下载http://www.shnenglu.com/flyinghearts/zh-cnTue, 06 May 2025 19:10:03 GMTTue, 06 May 2025 19:10:03 GMT60SEH异常处理专利到期?/title><link>http://www.shnenglu.com/flyinghearts/archive/2014/06/15/207288.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Jun 2014 02:55:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2014/06/15/207288.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/207288.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2014/06/15/207288.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/207288.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/207288.html</trackback:ping><description><![CDATA[<div>  <br />  Borland的那个恶心的SEH异常处理专利QNo:5,628,016  Filed: June, 15, 1997Q,l于到期了。希望GCC/CLANG能快点完成win32的C++异常的SEH实现Q下一版本的GCCQ能让用户不再纠lSJLJQDwarf2的选择?br /><br /></div><img src ="http://www.shnenglu.com/flyinghearts/aggbug/207288.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2014-06-15 10:55 <a href="http://www.shnenglu.com/flyinghearts/archive/2014/06/15/207288.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>面试? 扑և数组中三个只出现一ơ的?/title><link>http://www.shnenglu.com/flyinghearts/archive/2013/03/21/198695.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Thu, 21 Mar 2013 14:00:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2013/03/21/198695.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/198695.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2013/03/21/198695.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/198695.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/198695.html</trackback:ping><description><![CDATA[<p style="text-align:center" align="center"><strong><span style="font-family:宋体;Times New Roman";Times New Roman";"><br /></span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";"></span></strong></p> <p><strong><span>    </span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">题目Q数l中有三个数只出Cơ,其它的数恰好出现两次Q找三个数?/span></strong></p> <p> </p> <p><span>    </span><span style="font-family: 宋体;Times New Roman";Times New Roman";">先考虑“只有两个数出C?#8221;的情况:可以扑ֈ一U方法,数l划分ؓ两部分,且让q两个数分别在不同部分,q样每部分所有数的异或|恰好分别{于q两个数。一U简单的分法是Q先计算两个数的异或?/span>M<span style="font-family:宋体;Times New Roman";Times New Roman";">Q等价于求数l中所有数的异或|Q求?/span>M<span style="font-family:宋体;Times New Roman";Times New Roman";">值的二进制表CZ的最低位</span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">Q其它位?/span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">也可以,只不q麻烦点Q在</span> +k<span style="font-family:宋体;Times New Roman";Times New Roman";">位,然后Ҏ</span> +k<span style="font-family:宋体;Times New Roman";Times New Roman";">位是否ؓ</span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">Q将原数l分Z部分?/span></p> <p><span>    </span></p> <p><span>    </span><span style="font-family: 宋体;Times New Roman";Times New Roman";">回到原题Q假设这三个不同的数是:</span>A<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>B<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>C<span style="font-family:宋体;Times New Roman";Times New Roman";">Q它们的异或?/span> <strong><span>X = A xor B xor C</span></strong><span style="font-family:宋体;Times New Roman";Times New Roman";">Q?/span>X<span style="font-family:宋体;Times New Roman";Times New Roman";">值可能ؓ</span>0<span style="font-family:宋体;Times New Roman";Times New Roman";">Q也可能不ؓ</span>0<span style="font-family:宋体;Times New Roman";Times New Roman";">。当</span>X<span style="font-family:宋体;Times New Roman";Times New Roman";">g?/span>0<span style="font-family:宋体;Times New Roman";Times New Roman";">Ӟ比如Q这三个Cؓ</span>3<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>5<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>7<span style="font-family:宋体;Times New Roman";Times New Roman";">Q?/span>X<span style="font-family:宋体;Times New Roman";Times New Roman";">gؓ</span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">Q,很难扑ֈ一U方法,原来的数组划分Z部分Q得这三个C都在同一部分。因而,<strong>要先对原来的数组q行一ơ替换:每个数?/strong></span><strong>X</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">q行异或</span></strong><span style="font-family:宋体;Times New Roman";Times New Roman";">。这样原来的三个数就变成了:</span></p> <p>B xor C<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>A xor C<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>A xor B<span style="font-family:宋体;Times New Roman";Times New Roman";">。记</span><strong>a = B xor C</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></strong><strong>b = A xor C</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></strong><strong><span>c = A xor B</span></strong><span style="font-family:宋体;Times New Roman";Times New Roman";">。新的异或?/span> x = <strong>a xor b xor c = 0</strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></p> <p> <span>  </span></p> <p><span>    </span><span style="font-family: 宋体;Times New Roman";Times New Roman";">׃</span>A<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>B<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>C<span style="font-family:宋体;Times New Roman";Times New Roman";">互不相等Q显然它们间的异或?/span><strong>a</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></strong><strong>b</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></strong><strong>c</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">都不?/span></strong><strong>0</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">Q且互不相等</span></strong><span style="font-family:宋体;Times New Roman";Times New Roman";">。(?/span>a<span style="font-family:宋体;Times New Roman";Times New Roman";">{于</span>b<span style="font-family:宋体;Times New Roman";Times New Roman";">Q则</span><span> 0 = a xor b = (B xor C) xor (A xor C) = A xor B != 0</span><span style="font-family: 宋体;Times New Roman";Times New Roman";">自相矛盾Q?/span></p> <p><span>   </span></p> <p style="text-indent:21.1pt;"><strong><span style="font-family:宋体;Times New Roman";Times New Roman";color:#993300;">若三个数的异或gؓ</span></strong><strong><span style="color:#993300;">0</span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman"; color:#993300;">Q且其中一个数?/span></strong><strong><span style="color:#993300;"> +n</span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";color:#993300;">位(</span></strong><strong><span style="color:#993300;">n</span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";color:#993300;">ZQ意|上ؓ</span></strong><strong><span style="color:#993300;">1</span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman"; color:#993300;">Q则另两个数?/span></strong><strong><span style="color:#993300;"> +n</span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";color:#993300;">位上必然一个ؓ</span></strong><strong><span style="color:#993300;">1</span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";color:#993300;">Q另一个ؓ</span></strong><strong><span style="color:#993300;">0</span></strong><span style="font-family:宋体;Times New Roman";Times New Roman";">Q不然的话,q三个数的异或值就不会?/span>0<span style="font-family:宋体;Times New Roman";Times New Roman";">Q,<strong><span style="color:#993300">因而根?/span></strong></span><strong><span style="color:#993300;"> +n</span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";color:#993300;">位ؓ</span></strong><strong><span style="color:#993300;">1</span></strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";color:#993300;">Q可以将q三个数分成两部?/span></strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span> </p> <p style="text-indent:21.0pt;"> </p> <p style="text-indent:21.1pt;"><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></strong><strong>f(x)</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></strong><strong>x</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">的二q制表示中最低位</span></strong><strong>1</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">的位|,?/span></strong><strong>f(a)</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></strong><strong>f(b)</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></strong><strong>f(c)</strong><strong><span style="font-family:宋体;Times New Roman";Times New Roman";">q三个数中有且只有两个数相等</span></strong><span style="font-family:宋体;Times New Roman";Times New Roman";">。证明:不妨?/span>f(a)<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>f(b)<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>f(c)<span style="font-family:宋体;Times New Roman";Times New Roman";">中最的?/span>f(a)<span style="font-family:宋体;Times New Roman";Times New Roman";">Q?/span>k = f(a)<span style="font-family:宋体;Times New Roman";Times New Roman";">Q则</span>b<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span>c<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span> +k<span style="font-family:宋体;Times New Roman";Times New Roman";">位上必然是一个ؓ</span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">Q一个ؓ</span>0<span style="font-family:宋体;Times New Roman";Times New Roman";">Q不妨设</span>b<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span> +k<span style="font-family:宋体;Times New Roman";Times New Roman";">位ؓ</span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">Q则Ҏ</span>f(x)<span style="font-family:宋体;Times New Roman";Times New Roman";">的定义以?/span>f(a)<span style="font-family:宋体;Times New Roman";Times New Roman";">最,可得</span>f(b)<span style="font-family:宋体;Times New Roman";Times New Roman";">{于</span>f(a)<span style="font-family:宋体;Times New Roman";Times New Roman";">Q?/span>f(c)<span style="font-family:宋体;Times New Roman";Times New Roman";">大于</span>f(a)<span style="font-family:宋体;Times New Roman";Times New Roman";">。因而,新数l的?/span>x<span style="font-family:宋体;Times New Roman";Times New Roman";">对应的所?/span>f(x)<span style="font-family:宋体;Times New Roman";Times New Roman";">的d或值等?/span>f(c)<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span></p> <p style="text-indent:21.0pt;"> </p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman";">假设</span>f(a)<span style="font-family:宋体;Times New Roman";Times New Roman";">{于</span>f(b)<span style="font-family:宋体;Times New Roman";Times New Roman";">Q?/span>m = f(c)<span style="font-family:宋体;Times New Roman";Times New Roman";">Q由?/span>c<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span> +m<span style="font-family:宋体;Times New Roman";Times New Roman";">位上?/span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">Q不妨设</span> b<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span> +m<span style="font-family:宋体;Times New Roman";Times New Roman";">位也?/span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">Q则</span>a<span style="font-family:宋体;Times New Roman";Times New Roman";">?/span> +m<span style="font-family:宋体;Times New Roman";Times New Roman";">位ؓ</span>0<span style="font-family:宋体;Times New Roman";Times New Roman";">。根?/span> +m<span style="font-family:宋体;Times New Roman";Times New Roman";">位是否ؓ</span>1<span style="font-family:宋体;Times New Roman";Times New Roman";">Q可新数组分ؓ两部分,每部分的异或值恰好都?/span><span> <span>a = B xor C</span></span><span style="font-family:宋体;Times New Roman";Times New Roman";">Q可以求出数</span>A<span style="font-family:宋体;Times New Roman";Times New Roman";">Q等?/span>a xor X<span style="font-family:宋体;Times New Roman";Times New Roman";">Q?/span><span> </span><span style="font-family:宋体;Times New Roman";Times New Roman";">数</span>A<span style="font-family:宋体;Times New Roman";Times New Roman";">攑օ原来的数l,问题转ؓ</span><span style="font-family:宋体;Times New Roman";Times New Roman";">“只有两个数出C?#8221;的情况,利用前面提到的方法算出另两个数。d需遍历数组</span>4<span style="font-family:宋体;Times New Roman";Times New Roman";">ơ?/span></p> <p> </p> <p> </p> <p><span style="font-family:宋体;Times New Roman";Times New Roman";">代码Q?/span> </p> <p> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:#804000">#include <cstdio></span></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:#804000">#include <cassert></span></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">static</span> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">inline</span> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> extract_rightmost_one</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">unsigned</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> value</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">{</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:blue">return</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> value </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">&</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">-</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">value</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">}</span></strong></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:green">// if a != b, m = extract_rightmost_one(a), n = extract_rightmost_one(b),</span></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:green">//             k = extract_rightmost_one(a ^ b)</span></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:green">// then m == n != k   or<span>   </span>m == k != n   or n == k != m</span></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">void</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> print_three_unique_number</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> arr</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">[],</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">unsigned</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> len</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong></p> <p style="text-autospace:none"><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">{</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> assert </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">len </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">>=</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#FF8000">3</span> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">&&</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> len </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">%</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#FF8000">2</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">);</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> beg </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> arr</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> end </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> arr </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">+</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> len</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor3 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#FF8000">0</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:blue">for</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#8000FF">int</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> beg</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">!=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> end</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">++</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor3 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">^=</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> flag1 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#FF8000">0</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:blue">for</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#8000FF">int</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> beg</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">!=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> end</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">++</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> flag1 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">^=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> extract_rightmost_one</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">^</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor3</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">);</span></strong></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> assert</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">flag1 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">!=</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#FF8000">0</span> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">&&</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">flag1 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">&</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">flag1 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">-</span></strong> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#FF8000">1u</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">))</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">==</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#FF8000">0u</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">);</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå; color:green">// flag1 == 2^^k</span></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor2 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#FF8000">0</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:blue">for</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#8000FF">int</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> beg</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">!=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> end</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">++</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">{</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">    </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> value </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">^</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor3</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">    </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:blue">if</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">value </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">&</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> flag1</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor2 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">^=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> value</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:green">//or:</span></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">    </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:green">// if ((value & flag1) == 0) xor2 ^= value;</span></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">}</span></strong></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> number1 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor2 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">^</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor3</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> flag2 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> extract_rightmost_one</span><strong><span style="font-size:9.0pt;font-family: ËÎÌå;color:navy">(</span></strong><span style="font-size: 9.0pt;font-family:ËÎÌå; color:black">xor2</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">);</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> number2 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">number1 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">&</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> flag2</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">?</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> number1 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">:</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#FF8000">0</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong> <span style="font-size:9.0pt;font-family:ËÎÌå;color:green">// </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:green;">put</span><span style="font-size:9.0pt; font-family:ËÎÌå;color:green"> number1 </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:green;">in</span><span style="font-size: 9.0pt;font-family:ËÎÌå; color:green">to the array</span></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:blue">for</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt; font-family:ËÎÌå;color:#8000FF">int</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> beg</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">!=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> end</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">++</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">{</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">    </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:blue">if</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">&</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> flag2</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">)</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> number2 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">^=</span></strong> <strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">*</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black">p</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">}</span></strong></p> <p style="text-autospace:none"> </p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> </span><span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">const</span> <span style="font-size:9.0pt;font-family:ËÎÌå;color:#8000FF">int</span><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> number3 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">=</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> xor2 </span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">^</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> number2</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">;</span></strong></p> <p style="text-autospace:none"><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> printf</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">(</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:gray">"%d %d %d\n"</span><strong><span style="font-size:9.0pt;font-family: ËÎÌå;color:navy">,</span></strong><span style="font-size: 9.0pt;font-family:ËÎÌå; color:black"> number1</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">,</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> number2</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">,</span></strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:black"> number3</span><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">);</span></strong></p> <p style="text-autospace:none"><strong><span style="font-size:9.0pt;font-family:ËÎÌå;color:navy">}</span></strong></p> <p style="text-autospace:none"><strong> </strong></p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman";">上面的代码ƈ不严格符?/span>C++<span style="font-family:宋体;Times New Roman";Times New Roman";">标准Q而是假定机器采用补码表示负数Q不采用补码表示的老古董,一般h也碰不到Q?/span> <span style="font-family:宋体;Times New Roman";Times New Roman";">另外要特别注意的是,一些计最好先转ؓ无符hQ避免溢出,最后再转回有符h。因为标准假定有W号数间的计不发生溢出Q当实际上发生溢出时Q就?/span>UB<span style="font-family:宋体;Times New Roman";Times New Roman";">行ؓQ编译器若进行些Ȁq的优化得不到正确l果?/span></p> <p style="text-indent:21.0pt;"> </p><img src ="http://www.shnenglu.com/flyinghearts/aggbug/198695.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2013-03-21 22:00 <a href="http://www.shnenglu.com/flyinghearts/archive/2013/03/21/198695.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mingw gcc的头文g存在l构定义错误Q!http://www.shnenglu.com/flyinghearts/archive/2013/03/21/198694.htmlflyingheartsflyingheartsThu, 21 Mar 2013 13:51:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2013/03/21/198694.htmlhttp://www.shnenglu.com/flyinghearts/comments/198694.htmlhttp://www.shnenglu.com/flyinghearts/archive/2013/03/21/198694.html#Feedback0http://www.shnenglu.com/flyinghearts/comments/commentRss/198694.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/198694.html准确的讲是mingw所用的sdk ——  w32apiQ存在结构定义错误?br /> 
 打开wingdi.h 定位?tagGCP_RESULTSW 的结构定义位|,
 
  typedef struct tagGCP_RESULTSW {
    DWORD lStructSize;
    LPWSTR lpOutString;
    UINT *lpOrder;
    int *lpDx;
    int *lpCaretPos;
    LPWSTR lpClass;         //正确的类型应该是 LPSTR
    LPWSTR lpGlyphs;
    UINT nGlyphs;
    int nMaxFit;
  } GCP_RESULTSW,*LPGCP_RESULTSW;

 Ҏmsdn, lpClass的类型应该是 LPSTRQ?w32api却将其声明ؓ LPWSTR?br /> 
  q个bugQ在2011q就有h报告了,据说cvs版本也修正了Q但是直到现在,官方下蝲q是2011q发布的那个有bug的版本(w32api-3.17-2-mingw32-dev.tar.lzmaQ?br /> 
  另外Qmingw-w64Q支持生?2位?4位程序)不存在这个问题?

 



flyinghearts 2013-03-21 21:51 发表评论
]]>
c++11 最反直觉的地方http://www.shnenglu.com/flyinghearts/archive/2012/11/22/195570.htmlflyingheartsflyingheartsThu, 22 Nov 2012 11:49:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2012/11/22/195570.htmlhttp://www.shnenglu.com/flyinghearts/comments/195570.htmlhttp://www.shnenglu.com/flyinghearts/archive/2012/11/22/195570.html#Feedback4http://www.shnenglu.com/flyinghearts/comments/commentRss/195570.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/195570.html
1 auto 会自动把 引用 去除?br /> 
  int& get();
  auto k = get();     // k的类型是intQ而不是int&
 
  Derived object;
  auto&    same_object = (Base&)object;      
  auto  another_object = (Base&)object;  //会重新构造个Base对象  
 
    
2 decltype 有时会自动把 引用 加上

  int x;
  decltype((x)) ?decltype(*&x) 的类型是int&Q而不是int
 
  在宏中用decltypeӞ要特别注意别多加了括受?br /> 
  下面q段代码错在哪里Q?br />    template<typename T, typename R>
    auto min(T t, R r) -> decltype(t < r ? t : r)
    {
      return (t < r ? t : r);
    }
    
  decltype(t < r ? t : r)的类型是T&或R&Q而不是所希望的T或RQ?br /> 
 
  标准是这栯定的Q?br />
The type denoted by decltype(e) is defined as follows:
  — if e is an unparenthesized id-expression or an unparenthesized class member
     access (5.2.5), decltype(e) is the type of the entity named by e. If there
     is no such entity, or if e names a set of overloaded functions, the program
     is ill-formed;
  — otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;
  — otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
  — otherwise, decltype(e) is the type of e.
 

3 std::move、std::forward、右值引?br /> 
  C++11 引入 叛_引用,可以做到Q函数{发、针对时对象优?br />  move是动词,从字面上理解好像是要Ud对象Q其实std::move只是单的类型{成右值引用而已Q!Q?可以理解?cast_to_rvalue_reference ?treat_as_temporal_object?br /> 

  void test1(int&&) {}
 
  void test2(int&& value)  //注意Qvalue的类型是intQ而不是int&&
  {
     test1(value);    //无法~译通过Q因为value的类型是intQ?必须转换cd
     test1(static_cast<int&&>(value));      //或?br />     test1(std::forward<int>(value));
  }
 
  test2函数中,value的类型是intQ而不是int&&?br />  q是一个不得已的选择。如果value的类型是int&&的话Q就会有副作用:
 
  void increase(int& value) { ++value; }
  void test3(int&& value) { increase(value); }
 
  char ch = 'a';
  test3(ch);  //本意是改变ch|但实际上chg会改变,改变的是临时对像
  通过转发函数test3Qincrease函数可以修改临时对像Q?br />  q造成E序员犯的错误(如上面的例子Q,难以在编译时p扑և来?br /> 
 
  std::forward<T>(value) {h?static_cast<T&&>(value)Q感觉后者更Ҏ理解?br />  std::forward 起到的{发作用。如果Tcd?R&?R&&Q经q类型{换后Q其cdq是和原来的一栗?br />  在C++11?
    R&  &   {同?R&   Q在c++03中,R& &q种写法是非法的Q?br />    R&& &   {同?R&
    R&  &&  {同?R&
    R&& &&  {同?R&&
 

flyinghearts 2012-11-22 19:49 发表评论
]]>
decltype的小“陷阱?/title><link>http://www.shnenglu.com/flyinghearts/archive/2012/08/14/187204.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Tue, 14 Aug 2012 13:19:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2012/08/14/187204.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/187204.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2012/08/14/187204.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/187204.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/187204.html</trackback:ping><description><![CDATA[int x;<br /><div>decltype(x) ?nbsp; decltype((x))    的类型是不一L?/div><div>decltype(x) ?nbsp; decltype(*&x)   的类型是不一L?br /><div></div><br /><div>decltype(x)的类型是 int<br />decltype((x)) ?decltype(*&x)  的类型是 int&<br /><div><br />C++11标准Q?/div></div></div><div>The type denoted by decltype(e) is defined as follows:<br />  — if e is an unparenthesized id-expression or an unparenthesized class member<br />     access (5.2.5), decltype(e) is the type of the entity named by e. If there<br />     is no such entity, or if e names a set of overloaded functions, the program<br />     is ill-formed;<br />  — otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;<br />  — otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;<br />  — otherwise, decltype(e) is the type of e.</div><img src ="http://www.shnenglu.com/flyinghearts/aggbug/187204.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2012-08-14 21:19 <a href="http://www.shnenglu.com/flyinghearts/archive/2012/08/14/187204.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>内嵌汇编实现的函数{?/title><link>http://www.shnenglu.com/flyinghearts/archive/2012/08/05/186382.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 05 Aug 2012 13:02:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2012/08/05/186382.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/186382.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2012/08/05/186382.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/186382.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/186382.html</trackback:ping><description><![CDATA[  弄着玩的。功能是单的实现函数转发Q即<br />      调用CALL(func)Q{用func()Q?div>      调用CALL(func, arg1, arg2) Q{用func(arg1, arg2)</div><br /><div>  代码中,宏CALL/STDCALL分别用来调用  __cdecl/__stdcall 调用规定的函?br />             unsafe_call 两者都可调用,但它不是多线E安全的?br /><br />  代码只支持x86 32位, 除内嵌汇~部分,量W合C++11标准?br />       </div>原理Q?br />     刚进入函数时Q?br />     [esp]           函数q回地址<br />     [esp + 4]     W一个参敎ͼ卌{发函数的地址<br /><div>     [esp + 8]     W二个参敎ͼ卌{发函数的的第一个参?/div>     ...  <br />    <br />      只要写三行汇~指令实C个c_call函数Q就可调用{发函?br /><div>      pop eax                             ; eax为函数返回地址<br />      xchg dword ptr[esp], eax     ; eax{发函数的地址Q[esp]为函数返回地址<br />      jmp eax<br />      <br /><div>     当{发函数是__cdeclQ即转发函数不会调节栈,׃在c_callQpop eaxQesp多加?Q因而在调用完c_call后应该手动将esp值减4Q保证栈q?br />    <div>    当{发函数是__stdcallQ{发函C调节栈,调用转发函数完毕后,栈已l保持^衡,因而调用c_call完毕Q不应该q行栈指针调节。似乎将c_call的调用改为__stdcall卛_Q但实际上c_call有变长参敎ͼҎ__stdcall没效果,每次调用~译器还是会自动生成调节栈指针代码。因而只能每ơ调用完毕,~译器给esp加了多少Q就手动减多。(~译器不一定会生成 call  xxxx; add esp, xxq样的代码,通过改函数返回地址Q忽略后面的add esp, xx指o是很p糕的做法。)</div></div> </div><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_202018" onclick="this.style.display='none'; Code_Closed_Text_202018.style.display='none'; Code_Open_Image_202018.style.display='inline'; Code_Open_Text_202018.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ContractedBlock.gif" height="16" align="top" width="11"><img id="Code_Open_Image_202018" style="display: none" onclick="this.style.display='none'; Code_Open_Text_202018.style.display='none'; Code_Closed_Image_202018.style.display='inline'; Code_Closed_Text_202018.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ExpandedBlockStart.gif" height="16" align="top" width="11"><span id="Code_Closed_Text_202018" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">call_redirect</span><span id="Code_Open_Text_202018" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">  1</span> <span style="color: #000000; ">#include </span><span style="color: #000000; "><</span><span style="color: #000000; ">cstdio</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #008080; ">  2</span> <span style="color: #000000; ">#include </span><span style="color: #000000; "><</span><span style="color: #000000; ">cstdarg</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #008080; ">  3</span> <span style="color: #000000; ">#include </span><span style="color: #000000; "><</span><span style="color: #000000; ">windows.h</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #008080; ">  4</span> <span style="color: #000000; "><br /></span><span style="color: #008080; ">  5</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> CALL(<img src="http://www.shnenglu.com/Images/dot.gif" alt="" />)    c_call(__VA_ARGS__); ASM_SUB_ESP(4);</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">  6</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> STDCALL(<img src="http://www.shnenglu.com/Images/dot.gif" alt="" />) c_call(__VA_ARGS__); ASM_SUB_ESP(MACRO_ARGS(__VA_ARGS__) * 4);</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">  7</span> <span style="color: #000000; "><br /></span><span style="color: #008080; ">  8</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> MACRO_EXPAND(x) x</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">  9</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> MACRO_NTH_ARG(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, <img src="http://www.shnenglu.com/Images/dot.gif" alt="" />) a9</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 10</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> MACRO_ARGS(<img src="http://www.shnenglu.com/Images/dot.gif" alt="" />) \</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 11</span> <span style="color: #000000; ">    MACRO_EXPAND(MACRO_NTH_ARG(__VA_ARGS__, </span><span style="color: #000000; ">9</span><span style="color: #000000; ">, </span><span style="color: #000000; ">8</span><span style="color: #000000; ">, </span><span style="color: #000000; ">7</span><span style="color: #000000; ">, </span><span style="color: #000000; ">6</span><span style="color: #000000; ">, </span><span style="color: #000000; ">5</span><span style="color: #000000; ">, </span><span style="color: #000000; ">4</span><span style="color: #000000; ">, </span><span style="color: #000000; ">3</span><span style="color: #000000; ">, </span><span style="color: #000000; ">2</span><span style="color: #000000; ">, </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, </span><span style="color: #000000; ">0</span><span style="color: #000000; ">))<br /></span><span style="color: #008080; "> 12</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 13</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#if</span><span style="color: #000000; ">  __GNUC__</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 14</span> <span style="color: #000000; ">  </span><span style="color: #008000; ">//</span><span style="color: #008000; "> please enable option, -masm=intel</span><span style="color: #008000; "><br /></span><span style="color: #008080; "> 15</span> <span style="color: #008000; "></span><span style="color: #000000; ">  </span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> ASM_SUB_ESP(x) asm("lea esp, dword ptr[esp - %0]" : : "i"(x))</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 16</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 17</span> <span style="color: #000000; ">  </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> c_call(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; "> pfn, <img src="http://www.shnenglu.com/Images/dot.gif" alt="" />) asm(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">c_call</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; "> 18</span> <span style="color: #000000; ">  asm(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">                          \n\</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 19</span> <span style="color: #000000; "></span><span style="color: #000000; ">    c_call:                      \n\<br /></span><span style="color: #008080; "> 20</span> <span style="color: #000000; ">      pop eax;                   \n\<br /></span><span style="color: #008080; "> 21</span> <span style="color: #000000; ">      xchg dword ptr[esp], eax;  \n\<br /></span><span style="color: #008080; "> 22</span> <span style="color: #000000; ">      jmp eax;                   \n\<br /></span><span style="color: #008080; "> 23</span> <span style="color: #000000; ">  </span><span style="color: #000000; ">"</span><span style="color: #000000; ">);</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 24</span> <span style="color: #000000; "></span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 25</span> <span style="color: #000000; ">  </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> unsafe_call(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; "> pfn, <img src="http://www.shnenglu.com/Images/dot.gif" alt="" />)  asm(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">unsafe_call</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; "> 26</span> <span style="color: #000000; ">  asm(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">                                   \n\</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 27</span> <span style="color: #000000; "></span><span style="color: #000000; ">      .lcomm old_ret_addr, </span><span style="color: #000000; ">4</span><span style="color: #000000; ">              \n\<br /></span><span style="color: #008080; "> 28</span> <span style="color: #000000; ">      .lcomm old_esp_value,</span><span style="color: #000000; ">4</span><span style="color: #000000; ">              \n\<br /></span><span style="color: #008080; "> 29</span> <span style="color: #000000; ">    unsafe_call:                          \n\<br /></span><span style="color: #008080; "> 30</span> <span style="color: #000000; ">      pop  dword ptr old_ret_addr;        \n\<br /></span><span style="color: #008080; "> 31</span> <span style="color: #000000; ">      mov  dword ptr old_esp_value, esp;  \n\<br /></span><span style="color: #008080; "> 32</span> <span style="color: #000000; ">      mov eax, dword ptr[esp];            \n\<br /></span><span style="color: #008080; "> 33</span> <span style="color: #000000; ">      mov dword ptr[esp], offset last;    \n\<br /></span><span style="color: #008080; "> 34</span> <span style="color: #000000; ">      jmp eax;                            \n\<br /></span><span style="color: #008080; "> 35</span> <span style="color: #000000; ">    last:                                 \n\<br /></span><span style="color: #008080; "> 36</span> <span style="color: #000000; ">      mov esp, dword ptr old_esp_value;   \n\<br /></span><span style="color: #008080; "> 37</span> <span style="color: #000000; ">      jmp  dword ptr old_ret_addr;        \n\<br /></span><span style="color: #008080; "> 38</span> <span style="color: #000000; ">  </span><span style="color: #000000; ">"</span><span style="color: #000000; ">);</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 39</span> <span style="color: #000000; "></span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 40</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#elif</span><span style="color: #000000; ">  _MSC_VER</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 41</span> <span style="color: #000000; ">  </span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> ASM_SUB_ESP(x) __asm lea esp, dword ptr [esp - x]</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 42</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 43</span> <span style="color: #000000; ">  __declspec(naked) </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> c_call(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; "> pfn, <img src="http://www.shnenglu.com/Images/dot.gif" alt="" />)<br /></span><span style="color: #008080; "> 44</span> <span style="color: #000000; ">  {<br /></span><span style="color: #008080; "> 45</span> <span style="color: #000000; ">    __asm {<br /></span><span style="color: #008080; "> 46</span> <span style="color: #000000; ">      pop eax;<br /></span><span style="color: #008080; "> 47</span> <span style="color: #000000; ">      xchg dword ptr[esp], eax;<br /></span><span style="color: #008080; "> 48</span> <span style="color: #000000; ">      jmp eax;<br /></span><span style="color: #008080; "> 49</span> <span style="color: #000000; ">    }<br /></span><span style="color: #008080; "> 50</span> <span style="color: #000000; ">  }<br /></span><span style="color: #008080; "> 51</span> <span style="color: #000000; ">  __declspec(naked) </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> unsafe_call(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; "> pfn, <img src="http://www.shnenglu.com/Images/dot.gif" alt="" />)<br /></span><span style="color: #008080; "> 52</span> <span style="color: #000000; ">  {<br /></span><span style="color: #008080; "> 53</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; "> old_ret_addr;<br /></span><span style="color: #008080; "> 54</span> <span style="color: #000000; ">    </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; "> old_esp_value;<br /></span><span style="color: #008080; "> 55</span> <span style="color: #000000; ">    __asm {<br /></span><span style="color: #008080; "> 56</span> <span style="color: #000000; ">      pop  old_ret_addr;<br /></span><span style="color: #008080; "> 57</span> <span style="color: #000000; ">      mov  old_esp_value, esp;<br /></span><span style="color: #008080; "> 58</span> <span style="color: #000000; ">      mov eax, dword ptr[esp];<br /></span><span style="color: #008080; "> 59</span> <span style="color: #000000; ">      mov dword ptr[esp], offset last;<br /></span><span style="color: #008080; "> 60</span> <span style="color: #000000; ">      jmp eax;<br /></span><span style="color: #008080; "> 61</span> <span style="color: #000000; ">    last:<br /></span><span style="color: #008080; "> 62</span> <span style="color: #000000; ">      mov esp, old_esp_value;<br /></span><span style="color: #008080; "> 63</span> <span style="color: #000000; ">      jmp  old_ret_addr;<br /></span><span style="color: #008080; "> 64</span> <span style="color: #000000; ">    }<br /></span><span style="color: #008080; "> 65</span> <span style="color: #000000; ">  }<br /></span><span style="color: #008080; "> 66</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 67</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#else</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 68</span> <span style="color: #000000; ">  </span><span style="color: #0000FF; ">#error</span><span style="color: #000000; "> "only gcc and msvc are supported"</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 69</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">#endif</span><span style="color: #000000; "><br /></span><span style="color: #008080; "> 70</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 71</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 72</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> show(</span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">*</span><span style="color: #000000; "> format, <img src="http://www.shnenglu.com/Images/dot.gif" alt="" />)<br /></span><span style="color: #008080; "> 73</span> <span style="color: #000000; ">{<br /></span><span style="color: #008080; "> 74</span> <span style="color: #000000; ">  va_list args;<br /></span><span style="color: #008080; "> 75</span> <span style="color: #000000; ">  va_start(args, format);<br /></span><span style="color: #008080; "> 76</span> <span style="color: #000000; ">  vprintf(format, args);<br /></span><span style="color: #008080; "> 77</span> <span style="color: #000000; ">  va_end (args);<br /></span><span style="color: #008080; "> 78</span> <span style="color: #000000; ">}<br /></span><span style="color: #008080; "> 79</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 80</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> mysleep() { Sleep(</span><span style="color: #000000; ">10</span><span style="color: #000000; ">); }<br /></span><span style="color: #008080; "> 81</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 82</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> test1()<br /></span><span style="color: #008080; "> 83</span> <span style="color: #000000; ">{<br /></span><span style="color: #008080; "> 84</span> <span style="color: #000000; ">  </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> a </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">5</span><span style="color: #000000; ">, b </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">6</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; "> 85</span> <span style="color: #000000; ">  show(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d + %d = %d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, a, b, a </span><span style="color: #000000; ">+</span><span style="color: #000000; "> b);<br /></span><span style="color: #008080; "> 86</span> <span style="color: #000000; ">  CALL((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)printf, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d + %d = %d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, a, b, a </span><span style="color: #000000; ">+</span><span style="color: #000000; "> b);<br /></span><span style="color: #008080; "> 87</span> <span style="color: #000000; ">  CALL((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)mysleep);<br /></span><span style="color: #008080; "> 88</span> <span style="color: #000000; ">  CALL((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)show,   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d + %d = %d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, a, b, a </span><span style="color: #000000; ">+</span><span style="color: #000000; "> b);<br /></span><span style="color: #008080; "> 89</span> <span style="color: #000000; ">}<br /></span><span style="color: #008080; "> 90</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 91</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> test2()<br /></span><span style="color: #008080; "> 92</span> <span style="color: #000000; ">{<br /></span><span style="color: #008080; "> 93</span> <span style="color: #000000; ">  STDCALL((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)MessageBoxA, NULL, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">text1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">caption1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, MB_OK);<br /></span><span style="color: #008080; "> 94</span> <span style="color: #000000; ">  STDCALL((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)Sleep, </span><span style="color: #000000; ">10</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; "> 95</span> <span style="color: #000000; ">  STDCALL((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)MessageBoxA, NULL, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">text2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">caption2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, MB_OK);<br /></span><span style="color: #008080; "> 96</span> <span style="color: #000000; ">  STDCALL((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)Sleep, </span><span style="color: #000000; ">100</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; "> 97</span> <span style="color: #000000; ">}<br /></span><span style="color: #008080; "> 98</span> <span style="color: #000000; "><br /></span><span style="color: #008080; "> 99</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> test3()<br /></span><span style="color: #008080; ">100</span> <span style="color: #000000; ">{<br /></span><span style="color: #008080; ">101</span> <span style="color: #000000; ">  </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> a </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">5</span><span style="color: #000000; ">, b </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">6</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">102</span> <span style="color: #000000; ">  unsafe_call((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)printf, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d + %d = %d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, a, b, a </span><span style="color: #000000; ">+</span><span style="color: #000000; "> b);<br /></span><span style="color: #008080; ">103</span> <span style="color: #000000; ">  unsafe_call((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)mysleep);<br /></span><span style="color: #008080; ">104</span> <span style="color: #000000; ">  unsafe_call((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)show,   </span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d + %d = %d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, a, b, a </span><span style="color: #000000; ">+</span><span style="color: #000000; "> b);<br /></span><span style="color: #008080; ">105</span> <span style="color: #000000; ">  unsafe_call((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)MessageBoxA, NULL, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">text1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">caption1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, MB_OK);<br /></span><span style="color: #008080; ">106</span> <span style="color: #000000; ">  unsafe_call((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)Sleep, </span><span style="color: #000000; ">10</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">107</span> <span style="color: #000000; ">  unsafe_call((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)MessageBoxA, NULL, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">text2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, </span><span style="color: #000000; ">"</span><span style="color: #000000; ">caption2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, MB_OK);<br /></span><span style="color: #008080; ">108</span> <span style="color: #000000; ">  unsafe_call((</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)Sleep, </span><span style="color: #000000; ">100</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">109</span> <span style="color: #000000; ">}<br /></span><span style="color: #008080; ">110</span> <span style="color: #000000; "><br /></span><span style="color: #008080; ">111</span> <span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> main()<br /></span><span style="color: #008080; ">112</span> <span style="color: #000000; ">{<br /></span><span style="color: #008080; ">113</span> <span style="color: #000000; ">  test1();<br /></span><span style="color: #008080; ">114</span> <span style="color: #000000; ">  test2();<br /></span><span style="color: #008080; ">115</span> <span style="color: #000000; ">  test3();<br /></span><span style="color: #008080; ">116</span> <span style="color: #000000; ">  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">117</span> <span style="color: #000000; ">}<br /></span><span style="color: #008080; ">118</span> <span style="color: #000000; "><br /></span><span style="color: #008080; ">119</span> <span style="color: #000000; "><br /></span><span style="color: #008080; ">120</span> <span style="color: #000000; "></span></span></div><img src ="http://www.shnenglu.com/flyinghearts/aggbug/186382.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2012-08-05 21:02 <a href="http://www.shnenglu.com/flyinghearts/archive/2012/08/05/186382.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一些老游戏CPU 100%占用的解x?/title><link>http://www.shnenglu.com/flyinghearts/archive/2012/05/15/175005.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Tue, 15 May 2012 12:26:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2012/05/15/175005.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/175005.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2012/05/15/175005.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/175005.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/175005.html</trackback:ping><description><![CDATA[<br />下蝲Q?a href="/Files/flyinghearts/peekmsg.7z" title="peekmsg">peekmsg</a><br /><br /><div>用法Q?br />     peekmsg.exe   游戏ȝ?br />     或用lordpeQ打开游戏ȝ序,导入peekmsg.dll中的init<br /><br /> </div><div>原理Q?br />       hook PeekMessage函数Q用Sleep、WaitForMultipleObjects{函数让出CPU旉片?<br /><br /> </div><img src ="http://www.shnenglu.com/flyinghearts/aggbug/175005.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2012-05-15 20:26 <a href="http://www.shnenglu.com/flyinghearts/archive/2012/05/15/175005.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>三国?剧本修改?1.2http://www.shnenglu.com/flyinghearts/archive/2012/05/15/175004.htmlflyingheartsflyingheartsTue, 15 May 2012 12:14:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2012/05/15/175004.htmlhttp://www.shnenglu.com/flyinghearts/comments/175004.htmlhttp://www.shnenglu.com/flyinghearts/archive/2012/05/15/175004.html#Feedback0http://www.shnenglu.com/flyinghearts/comments/commentRss/175004.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/175004.html内置剧本改ؓ加强版剧本,保存剧本Ӟ同时生成普通版/加强版剧本?br />Q没旉试Q可能在更改宝物所有者时Q会有一些bugQ?br />
三国?剧本修改?.2
 

flyinghearts 2012-05-15 20:14 发表评论
]]>
安全的整数比?/title><link>http://www.shnenglu.com/flyinghearts/archive/2012/03/30/169554.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Fri, 30 Mar 2012 12:16:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2012/03/30/169554.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/169554.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2012/03/30/169554.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/169554.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/169554.html</trackback:ping><description><![CDATA[<br />前几天在水母上看到的题:<br /><br /><div>正常的比?assert(-1 < 1U) 是会p|的。因?-1 会提升成无符h?br />写一个安全的比较函数Q?br />template <typename T1, typename T2><br />int SafeIntCompare(T1 i1, T2 i2);<br />如果 i1 真实?<  i2Q返?-1<br />     i1 真实?== i2Q返?nbsp; 0<br />     i1 真实?>  i2Q返?nbsp; 1</div><br /><br />只有当两个类型一个是有符受另一个是无符hQ才需要特D处理?br />对类型的W号判断Q可以直接判断该cd?1是否?,也可以用标准库std::numeric_limits<T>中的is_signed成员?br /><br />单的做法Q?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> SafeIntCompare(T1 v1, T2 v2)<br />{<br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> t1 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> std::numeric_limits</span><span style="color: #000000; "><</span><span style="color: #000000; ">T1</span><span style="color: #000000; ">></span><span style="color: #000000; ">::is_signed;<br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> t2 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> std::numeric_limits</span><span style="color: #000000; "><</span><span style="color: #000000; ">T2</span><span style="color: #000000; ">></span><span style="color: #000000; ">::is_signed;<br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (t1 </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> t2) {<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (t1 </span><span style="color: #000000; ">&&</span><span style="color: #000000; "> v1 </span><span style="color: #000000; "><</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (t2 </span><span style="color: #000000; ">&&</span><span style="color: #000000; "> v2 </span><span style="color: #000000; "><</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />  }<br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (v1 </span><span style="color: #000000; ">==</span><span style="color: #000000; "> v2) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (v1 </span><span style="color: #000000; "><</span><span style="color: #000000; "> v2)  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />}</span></div><br /><div>但由于进行比较的两个数可能分别是Q有W号数和无符hQ编译时~译器会l出大量的警告?br /><br />     要避免有W号数和无符h的进行直接比较,必d它们都{为同一个类型T。这个类型的定可以采用两种ҎQ?br /><br />     1 比较原来两个cd是否是有W号C及它们所占用的字节数Q来推断出应该将它们都{为哪U类型TQ这是vc那个safeint的做法?br /><br />     2 采用q个trickQ将q两个类型的敎ͼ数可以取0Q直接相加,得到的结果的cd是所求的。这是因为:两个数进行比较时Q采用的cd转换规则和两个数相加时所采用的规则是一致的?br /><br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_195336" onclick="this.style.display='none'; Code_Closed_Text_195336.style.display='none'; Code_Open_Image_195336.style.display='inline'; Code_Open_Text_195336.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ContractedBlock.gif" height="16" width="11" align="top"><img id="Code_Open_Image_195336" style="display: none" onclick="this.style.display='none'; Code_Open_Text_195336.style.display='none'; Code_Closed_Image_195336.style.display='inline'; Code_Closed_Text_195336.style.display='inline';" src="http://www.shnenglu.com/images/OutliningIndicators/ExpandedBlockStart.gif" height="16" width="11" align="top"><span id="Code_Closed_Text_195336" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">Ҏ后的代码</span><span id="Code_Open_Text_195336" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; "><br /><br /><br />template</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">bool</span><span style="color: #000000; ">></span><span style="color: #000000; "> </span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> Assert {};<br />template</span><span style="color: #000000; "><></span><span style="color: #000000; "> </span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> Assert</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">></span><span style="color: #000000; ">;<br /><br />template</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> is_first_negtive, </span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> is_second_negtive</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> SafeIntCmpImpl<br />{<br />  template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> int_cmp(T1 v1, T2 v2)<br />  {<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (v1 </span><span style="color: #000000; ">==</span><span style="color: #000000; "> v2) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (v1  </span><span style="color: #000000; "><</span><span style="color: #000000; "> v2) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />  }<br />};<br /><br />template</span><span style="color: #000000; "><></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> SafeIntCmpImpl</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">, </span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />{<br />  template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2, typename T3</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> int_cmp(T1 v1, T2 v2, T3)<br />  {<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> SafeIntCmpImpl</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">, </span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">></span><span style="color: #000000; ">::int_cmp(T3(v1), T3(v2));<br />  }<br /><br />  template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> int_cmp(T1 v1, T2 v2)<br />  {<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> v1 </span><span style="color: #000000; "><</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; "> </span><span style="color: #000000; ">?</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; "> : int_cmp(v1, v2, T1(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">) </span><span style="color: #000000; ">+</span><span style="color: #000000; "> T2(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">));<br />  }<br />};<br /><br />template</span><span style="color: #000000; "><></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> SafeIntCmpImpl</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">, </span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />{<br />  template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> int_cmp(T1 v1, T2 v2)<br />  {<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">SafeIntCmpImpl</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">, </span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">></span><span style="color: #000000; ">::int_cmp(v2, v1);<br />  }<br />};<br /><br /><br /><br />template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> SafeIntCompare(T1 v1, T2 v2)<br />{<br />  typedef std::numeric_limits</span><span style="color: #000000; "><</span><span style="color: #000000; ">T1</span><span style="color: #000000; ">></span><span style="color: #000000; "> M1;<br />  typedef std::numeric_limits</span><span style="color: #000000; "><</span><span style="color: #000000; ">T2</span><span style="color: #000000; ">></span><span style="color: #000000; "> M2;<br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> is_arg_valid </span><span style="color: #000000; ">=</span><span style="color: #000000; "> M1::is_integer </span><span style="color: #000000; ">&</span><span style="color: #000000; "> M2::is_integer;<br />  Assert</span><span style="color: #000000; "><</span><span style="color: #000000; ">is_arg_valid</span><span style="color: #000000; ">></span><span style="color: #000000; ">();<br />  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> SafeIntCmpImpl</span><span style="color: #000000; "><</span><span style="color: #000000; ">M1::is_signed, M2::is_signed</span><span style="color: #000000; ">></span><span style="color: #000000; ">::int_cmp(v1, v2);<br />}<br /></span></span></div><br />  但上面的写法有一个问题:如果一?short和一个unsigned charq行比较Q编译器都是转ؓintq行比较Q没有必要进行特D处理(上面的代码处理后会多一个与0的比较)。实际上Q如果两个类型都是{为有W号cdQ可以直接进行比较?/div>最l代码:<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; "><br />template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> IsSigned { <br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> value </span><span style="color: #000000; ">=</span><span style="color: #000000; "> T(</span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">) </span><span style="color: #000000; "><</span><span style="color: #000000; "> T(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">); <br />};<br /><br />template</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">bool</span><span style="color: #000000; ">></span><span style="color: #000000; "> </span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> Assert {};<br />template</span><span style="color: #000000; "><></span><span style="color: #000000; "> </span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> Assert</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">></span><span style="color: #000000; ">;<br /><br />template</span><span style="color: #000000; "><</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">></span><span style="color: #000000; "> </span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> Type {};<br />typedef Type</span><span style="color: #000000; "><</span><span style="color: #000000; ">0</span><span style="color: #000000; ">></span><span style="color: #000000; "> TagNormal;<br />typedef Type</span><span style="color: #000000; "><</span><span style="color: #000000; ">1</span><span style="color: #000000; ">></span><span style="color: #000000; "> TagFirstArgIsSigned;<br />typedef Type</span><span style="color: #000000; "><</span><span style="color: #000000; ">2</span><span style="color: #000000; ">></span><span style="color: #000000; "> TagSecondArgIsSigned;<br /><br />template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2, typename T3</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> SafeIntCompare(T1 v1, T2 v2, T3, TagNormal)<br />{<br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (v1  </span><span style="color: #000000; "><</span><span style="color: #000000; "> v2) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (v1 </span><span style="color: #000000; ">==</span><span style="color: #000000; "> v2) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />}<br /><br />template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2, typename T3</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> SafeIntCompare(T1 v1, T2 v2, T3 v3, TagFirstArgIsSigned)<br />{<br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (v1 </span><span style="color: #000000; "><</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> SafeIntCompare(T3(v1), T3(v2), v3, TagNormal());<br />}<br /><br />template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2, typename T3</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> SafeIntCompare(T1 v1, T2 v2, T3 v3, TagSecondArgIsSigned)<br />{<br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (v2 </span><span style="color: #000000; "><</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> SafeIntCompare(T3(v1), T3(v2), v3, TagNormal());<br />}<br /><br />template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2, typename T3</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> SafeIntCompare(T1 v1, T2 v2, T3 v3)<br />{<br />  typedef std::numeric_limits</span><span style="color: #000000; "><</span><span style="color: #000000; ">T1</span><span style="color: #000000; ">></span><span style="color: #000000; "> M1;<br />  typedef std::numeric_limits</span><span style="color: #000000; "><</span><span style="color: #000000; ">T2</span><span style="color: #000000; ">></span><span style="color: #000000; "> M2;<br />  typedef std::numeric_limits</span><span style="color: #000000; "><</span><span style="color: #000000; ">T3</span><span style="color: #000000; ">></span><span style="color: #000000; "> M3;<br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> is_arg_valid </span><span style="color: #000000; ">=</span><span style="color: #000000; "> M1::is_integer </span><span style="color: #000000; ">&</span><span style="color: #000000; "> M2::is_integer;<br />  Assert</span><span style="color: #000000; "><</span><span style="color: #000000; ">is_arg_valid</span><span style="color: #000000; ">></span><span style="color: #000000; ">();<br />  </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> type_idx </span><span style="color: #000000; ">=</span><span style="color: #000000; "> M3::is_signed </span><span style="color: #000000; ">?</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; "> : (M1::is_signed </span><span style="color: #000000; ">+</span><span style="color: #000000; "> M2::is_signed </span><span style="color: #000000; ">*</span><span style="color: #000000; "> </span><span style="color: #000000; ">2</span><span style="color: #000000; ">) </span><span style="color: #000000; ">%</span><span style="color: #000000; "> </span><span style="color: #000000; ">3</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> SafeIntCompare(v1, v2, v3, Type</span><span style="color: #000000; "><</span><span style="color: #000000; ">type_idx</span><span style="color: #000000; ">></span><span style="color: #000000; ">());<br />}<br /><br /><br />template</span><span style="color: #000000; "><</span><span style="color: #000000; ">typename T1, typename T2</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> SafeIntCompare(T1 v1, T2 v2)<br />{<br />  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> SafeIntCompare(v1, v2, T1(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">) </span><span style="color: #000000; ">+</span><span style="color: #000000; "> T2(</span><span style="color: #000000; ">0</span><span style="color: #000000;">));<br />}<br /></span></div><img src ="http://www.shnenglu.com/flyinghearts/aggbug/169554.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2012-03-30 20:16 <a href="http://www.shnenglu.com/flyinghearts/archive/2012/03/30/169554.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>面试题精?目录http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168270.htmlflyingheartsflyingheartsSun, 18 Mar 2012 14:24:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2012/03/18/168270.htmlhttp://www.shnenglu.com/flyinghearts/comments/168270.htmlhttp://www.shnenglu.com/flyinghearts/archive/2012/03/18/168270.html#Feedback0http://www.shnenglu.com/flyinghearts/comments/commentRss/168270.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/168270.html?nbsp; ?/div>


 1  求二叉树中相距最q的两个节点之间的距?br /> 2  判断二叉树是否^衡二叉树
 3  指定二叉树,l定两节Ҏ其最q共同父节点
 4  二叉树的q度遍历、逐层打印二叉树节Ҏ据、只打印某层节点数据
 5  在二叉树中找出和Q叶子到根节点\径上的所有节点的数据和)为指定值的所有\径?br /> 6  二叉查找树转ؓ有序的双链表
 7  求二叉树的镜?br /> 8  二叉树前序、中序、后序遍历的非递归实现
 9  求二叉树高度的非递归实现
10  q接二叉树同一层上的结?/div>




flyinghearts 2012-03-18 22:24 发表评论
]]>面试题精解之二: 字符丌Ӏ数l(1Q?/title><link>http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168269.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 18 Mar 2012 13:28:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168269.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/168269.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168269.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/168269.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/168269.html</trackback:ping><description><![CDATA[     摘要: 面试题精解之二: 字符丌Ӏ数l(1Q?  本篇文章发表在下面三个博客中Q如果出现排版问题,L步到另一个博客?http://www.shnenglu.com/flyinghearts http://www.cnblogs.com/flyinghearts http://blog.csdn.net/flyinghearts   1   ...  <a href='http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168269.html'>阅读全文</a><img src ="http://www.shnenglu.com/flyinghearts/aggbug/168269.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2012-03-18 21:28 <a href="http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168269.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>避免计算q程中出现溢出的一个技?/title><link>http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168267.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 18 Mar 2012 13:16:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168267.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/168267.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168267.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/168267.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/168267.html</trackback:ping><description><![CDATA[<div> <p> </p> <p style="text-indent: 21pt;"><span style="font-family: 宋体;">先看一道面试题Q?/span></p><p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman""><br /></span></p> <p style="text-indent:23.6pt;"><strong><span style="font-size:12.0pt;font-family: 宋体;Times New Roman";Times New Roman"">长度?/span></strong><strong><span style="font-size:12.0pt">n</span></strong><strong><span style="font-size:12.0pt;font-family: 宋体;Times New Roman";Times New Roman"">的数l,由数?/span></strong><strong><span style="font-size:12.0pt">1</span></strong><strong><span style="font-size:12.0pt;font-family: 宋体;Times New Roman";Times New Roman"">?/span></strong><strong><span style="font-size:12.0pt">n</span></strong><strong><span style="font-size:12.0pt;font-family: 宋体;Times New Roman";Times New Roman"">l成Q其中数?/span></strong><strong><span style="font-size:12.0pt">a</span></strong><strong><span style="font-size:12.0pt;font-family: 宋体;Times New Roman";Times New Roman"">不出玎ͼ数字</span></strong><strong><span style="font-size:12.0pt">b</span></strong><strong><span style="font-size:12.0pt;font-family: 宋体;Times New Roman";Times New Roman"">出现两次Q其它的数字恰好出现一ơ。怎样通过只读遍历一ơ数l,扑և数字</span></strong><strong><span style="font-size:12.0pt">a</span></strong><strong><span style="font-size:12.0pt;font-family: 宋体;Times New Roman";Times New Roman"">?/span></strong><strong><span style="font-size:12.0pt">b</span></strong><strong><span style="font-size:12.0pt;font-family: 宋体;Times New Roman";Times New Roman"">?/span></strong></p> <p> </p> <p> </p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">׃只能遍历一ơ,在遍历数l?/span>arr<span style="font-family:宋体;Times New Roman";Times New Roman"">Ӟ出</span> a<span style="font-family: 宋体;Times New Roman";Times New Roman"">?/span>b<span style="font-family:宋体;Times New Roman";Times New Roman"">的差|以及</span>a<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>b<span style="font-family: 宋体;Times New Roman";Times New Roman"">的^方差Q通过解方E,卛_求得</span>a<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>b<span style="font-family:宋体;Times New Roman";Times New Roman"">。具体做法ؓQ?/span></p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">设:</span></p> <p><span><span>      </span>s1 = 1 + 2 + ... + n           (= n * (n + 1) / 2)</span></p> <p><span><span>      </span>s2 = arr[0] + arr[1] + ... + arr[n - 1]</span></p> <p><span>    </span></p> <p><span><span>      </span>r1 = 1 + 4 + ... + n^2          (= n * (n + 1) * (2 * n + 1) / 6)</span></p> <p><span><span>      </span>r2 = arr[0]^2 + arr[1]^2 + ... + arr[n - 1]^2</span></p> <p><span><span>      </span></span></p> <p><span>     c = a - b = s1 - s2</span></p> <p><span>     d = a^2 - b^2 = r1 - r2</span></p> <p> <span>   </span><span style="font-family:宋体;Times New Roman";Times New Roman"">昄Q?/span> <span> a + b = (r1 - r2) / (s1 - s2) </span></p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">Ҏ</span>a+b<span style="font-family: 宋体;Times New Roman";Times New Roman"">的值和</span>a-b<span style="font-family:宋体;Times New Roman";Times New Roman"">的|很容易就可算?/span>a<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>b<span style="font-family: 宋体;Times New Roman";Times New Roman"">?/span></p> <p> </p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">法虽然单,但实现v来,却有一个很大问题:计算</span> s1<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>s2<span style="font-family: 宋体;Times New Roman";Times New Roman"">?/span>r1<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>r2<span style="font-family:宋体;Times New Roman";Times New Roman"">q?/span>4<span style="font-family: 宋体;Times New Roman";Times New Roman"">个数Ӟ计算q程中可能出现溢出,造成l果不准。由于最l目的是Z计算?/span>c<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>d<span style="font-family:宋体;Times New Roman";Times New Roman"">Q一个改q的Ҏ是:</span></p> <p><span> c = s1 - s2 = (1 - arr[0]) + (2 - arr[1]) + ... + (n - arr[n - 1])</span></p> <p><span> d = (1 - arr[0]^2) + (4 - arr[1]^2) + ... + (n^2 - arr[n - 1]^2)</span></p> <p><span style="font-family:宋体;Times New Roman";Times New Roman"">但这L做法Qƈ不能解决问题Q?/span>n<span style="font-family:宋体;Times New Roman";Times New Roman"">E微大点Q照样存在溢出问题?/span></p> <p> </p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">那么怎样才能避免计算溢出呢?{案很简单,<strong><span style="color:red">用模q算Q?/span></strong>每进行一ơ加减运时Q都取结果ؓ原结果除以一个够大的常?/span>M<span style="font-family:宋体;Times New Roman";Times New Roman"">的余数。这样加减运中Q就不会现现溢出问题。最后再?/span> c % M<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>d % M<span style="font-family:宋体;Times New Roman";Times New Roman"">Q推出</span>c<span style="font-family: 宋体;Times New Roman";Times New Roman"">?/span>d<span style="font-family:宋体;Times New Roman";Times New Roman"">的具体倹{比如说Q计?/span>s2<span style="font-family:宋体;Times New Roman";Times New Roman"">改ؓ计算Q?/span></p> <p> <span>  s2 % M = ((((arr[0] % M) + arr[1]) % M + ...) % M + arr[n - 1]) %M</span></p> <p><span style="font-family:宋体;Times New Roman";Times New Roman"">从表面上看,采用模运后Q计量会增加很多。但实际上,?/span>M<span style="font-family:宋体;Times New Roman";Times New Roman"">取合适的值时Q计量q不会增加!Q?/span></p> <p> </p> <p style="text-indent:21.75pt"><span style="font-family:宋体;Times New Roman";Times New Roman"">先回下计算机基本知识:两个?/span>N<span style="font-family:宋体;Times New Roman";Times New Roman"">位(寄存器ؓ</span>N<span style="font-family:宋体;Times New Roman";Times New Roman"">位)的二q制无符h?/span>a<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>b<span style="font-family: 宋体;Times New Roman";Times New Roman"">相加Q若l果溢出了,</span>CPU<span style="font-family:宋体;Times New Roman";Times New Roman"">会怎么处理Q当然是溢出的那一位忽略掉Q可能还要设|下溢出标志Q,得到的结果实际上是:</span>(a + b) mod 2^N<span style="font-family:宋体;Times New Roman";Times New Roman"">?strong><span style="color:red">无符h间的术q算Q本质上是模运?/span></strong>。现在的</span>CPU<span style="font-family:宋体;Times New Roman";Times New Roman"">?strong><span style="color:red">采用二补数来表示负整敎ͼ本质上也是运用模q算</span></strong>Q教U书二补数表示的负整数单定义ؓQ对正整数取反后?/span>1<span style="font-family:宋体;Times New Roman";Times New Roman"">Q,q与无符h间的q算是一致的Q在实现上,比用其它ҎQ比如说一补数Q表C整数Q要优美易实现?/span></p> <p style="text-indent:21.75pt"><span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>32<span style="font-family:宋体;Times New Roman";Times New Roman"">位^CQ?/span> <span>-x mod 2^32 = 2^32 – x (x > 0)</span><span style="font-family:宋体;Times New Roman";Times New Roman"">Q?/span> </p> <p style="text-indent:21.75pt"><span style="font-family:宋体;Times New Roman";Times New Roman"">因?/span>-1<span style="font-family:宋体;Times New Roman";Times New Roman"">的二q制表示是Q?/span>0xFFFFFFFF</p> <p> </p> <p style="text-indent:21.75pt"><span style="font-family:宋体;Times New Roman";Times New Roman"">了解了这些,׃会奇?/span>C/C++<span style="font-family:宋体;Times New Roman";Times New Roman"">标准的规定:无符h间的q算是模q算不会溢出Q有W号数{为无W数Q采用模q算后的倹{(Z兼容没采用二补数的机器,无符h转ؓ有符hӞ若无W号数的数DZ有符h可表C的范围Q结果是q_相关的。)</span></p> <p> </p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">因而,在对</span>32<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>CPU<span style="font-family: 宋体;Times New Roman";Times New Roman"">q_Q可以先有W号数{为无W号敎ͼ再取</span>M = 2 ^32<span style="font-family:宋体;Times New Roman";Times New Roman"">。需要特别注意的是,应该采用多少位的无符h保存计算中用到的数|如何避免模运可能带来的问题Q?/span></p> <p> </p> <p><span style="font-family:宋体;Times New Roman";Times New Roman"">① 无符hcd的选择Q?/span></p> <p style="margin-left:27.0pt; text-indent:-6.0pt;">a<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>b<span style="font-family: 宋体;Times New Roman";Times New Roman"">的取D围ؓQ?/span>[1, n]<span style="font-family:宋体;Times New Roman";Times New Roman"">Q?/span> </p> <p style="text-indent:21.0pt;">c % M = (a - b) % M <span style="font-family:宋体;Times New Roman";Times New Roman"">的取D围ؓQ?/span>[1, n] (a > b<span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>)<span style="font-family:宋体;Times New Roman";Times New Roman"">Q?/span><span>   [M - n, M - 1] (a < b</span><span style="font-family:宋体;Times New Roman";Times New Roman"">?/span>)</p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">q两个范围不能重叠,而因</span> n < M - n <span style="font-family:宋体;Times New Roman";Times New Roman"">?/span> 2 * n < M</p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">?/span> M<span style="font-family: 宋体;Times New Roman";Times New Roman"">?/span>2^32<span style="font-family:宋体;Times New Roman";Times New Roman"">的话Q且</span><span> n < 2^31</span><span style="font-family:宋体;Times New Roman";Times New Roman"">Q?/span> <span style="font-family: 宋体;Times New Roman";Times New Roman"">可以采用</span>32<span style="font-family:宋体;Times New Roman";Times New Roman"">位无W号数表C?/span>c<span style="font-family:宋体;Times New Roman";Times New Roman"">的倹{?/span></p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">Ҏ</span>c % M<span style="font-family:宋体;Times New Roman";Times New Roman"">值在哪一个范_可以定</span>a > b<span style="font-family:宋体;Times New Roman";Times New Roman"">q是</span>a < b<span style="font-family:宋体;Times New Roman";Times New Roman"">Q?/span></p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">׃q算q程中都是采用无W号数计,?/span> a < b<span style="font-family:宋体;Times New Roman";Times New Roman"">Ӟ必须q行如下调整Q?/span></p> <p><span>        c % M </span><span style="font-family: 宋体;Times New Roman";Times New Roman"">调整?/span> (-c) % M</p> <p><span>        d % M </span><span style="font-family: 宋体;Times New Roman";Times New Roman"">调整?/span> (-d) % M</p> <p style="text-indent:21.0pt;"><span style="font-family:宋体;Times New Roman";Times New Roman"">q样才能保证l果的正性?/span></p> <p> </p> <p><span style="font-family:宋体;Times New Roman";Times New Roman"">②</span> <span style="font-family: 宋体;Times New Roman";Times New Roman"">用公式计所有数字的和、^方和Ӟ可能出现的问题:</span></p> <p><span>   </span><span style="font-family:宋体;Times New Roman";Times New Roman"">模运满I</span><span> (a <strong>*</strong> b) % M  = ((a % M) <strong>*</strong> (b % M)) % M</span></p> <p><span>     </span><span style="font-family:宋体;Times New Roman";Times New Roman"">?strong>不满?/strong>Q?/span><span> (a<strong> /</strong> b) % M  = ((a % M) <strong>/</strong> (b % M)) % M</span></p> <p><span>   </span><span style="font-family:宋体;Times New Roman";Times New Roman"">在计?/span> (n * (n + 1) / 2) % M<span style="font-family:宋体;Times New Roman";Times New Roman"">Ӟ</span> <span style="font-family:宋体;Times New Roman";Times New Roman"">不能写成Q?/span></p> <p> <span>     s = ((n * (n + 1)) % M / 2) % M</span><span style="font-family:宋体;Times New Roman";Times New Roman"">Q?/span></p> <p><span>   </span><span style="font-family:宋体;Times New Roman";Times New Roman"">而应该写成:</span></p> <p><span>     if  (n % 2 == 0)   s = ((n / 2) * (n + 1)) % M</span></p> <p> <span>    else             s = (((n + 1) / 2) * n) % M</span></p> <p><span>   </span><span style="font-family:宋体;Times New Roman";Times New Roman"">或者:</span><span>s = (INT((n + 1) / 2) * (n + (n + 1) % 2)) % M </span><span style="font-family:宋体;Times New Roman";Times New Roman"">Q其?/span>INT(x)<span style="font-family:宋体;Times New Roman";Times New Roman"">为取数</span>x<span style="font-family: 宋体;Times New Roman";Times New Roman"">的整数部份)?/span></p> <p> </p> <p><span style="font-family:宋体;Times New Roman";Times New Roman"">完整代码Q?/span></p> <p style="text-align: left;" align="left"> </p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; "><br />#include </span><span style="color: #000000; "><</span><span style="color: #000000; ">climits</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />#include </span><span style="color: #000000; "><</span><span style="color: #000000; ">cassert</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /><br /></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> SMALL_ARRAY 0</span><span style="color: #000000; "><br /><br /></span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> Pair {<br />  </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> zero;<br />  </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> twice;<br />};<br /><br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">32位CPUq_Q长度n一定小?^16ơ方Ӟ表示一个数的^方|可用32位无W号数类型,效率很高?br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">长度n若在[2^16, 2^31]区间Q就必须用到64位无W号数类型,效率较高?br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">长度n若在[2^31, 2^32)Ӟ表示 所有数的和sumQ就必须改用64位无W号数类型,效率不高?nbsp; </span><span style="color: #008000; "><br /></span><span style="color: #000000; ">Pair find_number(</span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> arr[], unsigned len)<br />{<br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned bits </span><span style="color: #000000; ">=</span><span style="color: #000000; "> CHAR_BIT </span><span style="color: #000000; ">*</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(unsigned);<br /></span><span style="color: #0000FF; ">#if</span><span style="color: #000000; "> SMALL_ARRAY</span><span style="color: #000000; "><br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned max_len </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">1u</span><span style="color: #000000; "> </span><span style="color: #000000; "><<</span><span style="color: #000000; "> (bits </span><span style="color: #000000; ">/</span><span style="color: #000000; "> </span><span style="color: #000000; ">2u</span><span style="color: #000000; ">);<br />  typedef unsigned </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">uint</span><span style="color: #000000; ">;<br /></span><span style="color: #0000FF; ">#else</span><span style="color: #000000; "><br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned max_len </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">1u</span><span style="color: #000000; "> </span><span style="color: #000000; "><<</span><span style="color: #000000; "> (bits </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br />  typedef unsigned </span><span style="color: #0000FF; ">long</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">long</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">uint</span><span style="color: #000000; ">;<br /></span><span style="color: #0000FF; ">#endif</span><span style="color: #000000; "><br /><br />  assert(arr </span><span style="color: #000000; ">&&</span><span style="color: #000000; "> len </span><span style="color: #000000; ">>=</span><span style="color: #000000; "> </span><span style="color: #000000; ">2</span><span style="color: #000000; "> </span><span style="color: #000000; ">&&</span><span style="color: #000000; "> len </span><span style="color: #000000; "><</span><span style="color: #000000; "> max_len);<br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned</span><span style="color: #000000; ">*</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> data </span><span style="color: #000000; ">=</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)arr;<br />  unsigned sum </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">uint</span><span style="color: #000000; "> square_sum </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (unsigned i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">; i </span><span style="color: #000000; "><</span><span style="color: #000000; "> len; </span><span style="color: #000000; ">++</span><span style="color: #000000; ">i)  {<br />    </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned value </span><span style="color: #000000; ">=</span><span style="color: #000000; "> data[i];<br />    sum </span><span style="color: #000000; ">+=</span><span style="color: #000000; "> value;<br />    square_sum </span><span style="color: #000000; ">+=</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">uint</span><span style="color: #000000; ">)value </span><span style="color: #000000; ">*</span><span style="color: #000000; "> value;     </span><span style="color: #008000; ">//</span><span style="color: #008000; ">注意两个数的乘积是否会溢?nbsp; </span><span style="color: #008000; "><br /></span><span style="color: #000000; ">  }<br />  <br />  </span><span style="color: #008000; ">//</span><span style="color: #008000; ">1 + 2 + 3 + <img src="http://www.shnenglu.com/Images/dot.gif" alt="" /> + len = len * (len + 1) / 2</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">uint</span><span style="color: #000000; "> sum_all </span><span style="color: #000000; ">=</span><span style="color: #000000; "> (len </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">) </span><span style="color: #000000; ">/</span><span style="color: #000000; "> </span><span style="color: #000000; ">2u</span><span style="color: #000000; "> </span><span style="color: #000000; ">*</span><span style="color: #000000; "> (</span><span style="color: #0000FF; ">uint</span><span style="color: #000000; ">)(len </span><span style="color: #000000; ">+</span><span style="color: #000000; "> (len </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">) </span><span style="color: #000000; ">%</span><span style="color: #000000; "> </span><span style="color: #000000; ">2u</span><span style="color: #000000; ">);<br />  <br />  </span><span style="color: #008000; ">//</span><span style="color: #008000; ">1^2 + 2^2 + 3^2 + <img src="http://www.shnenglu.com/Images/dot.gif" alt="" /> + len^2 = len * (len + 1) * (2 * len + 1) / 6</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned len2 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">2u</span><span style="color: #000000; "> </span><span style="color: #000000; ">*</span><span style="color: #000000; "> len </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">uint</span><span style="color: #000000; "> square_sum_all </span><span style="color: #000000; ">=</span><span style="color: #000000; "> len2 </span><span style="color: #000000; ">%</span><span style="color: #000000; "> </span><span style="color: #000000; ">3u</span><span style="color: #000000; "> </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; "> </span><span style="color: #000000; ">?</span><span style="color: #000000; "> len2 </span><span style="color: #000000; ">/</span><span style="color: #000000; "> </span><span style="color: #000000; ">3u</span><span style="color: #000000; "> </span><span style="color: #000000; ">*</span><span style="color: #000000; "> sum_all : sum_all </span><span style="color: #000000; ">/</span><span style="color: #000000; "> </span><span style="color: #000000; ">3u</span><span style="color: #000000; "> </span><span style="color: #000000; ">*</span><span style="color: #000000; "> len2;<br />  <br />  unsigned difference </span><span style="color: #000000; ">=</span><span style="color: #000000; "> (unsigned)sum_all </span><span style="color: #000000; ">-</span><span style="color: #000000; "> sum;<br />  </span><span style="color: #0000FF; ">uint</span><span style="color: #000000; "> square_difference </span><span style="color: #000000; ">=</span><span style="color: #000000; "> square_sum_all </span><span style="color: #000000; ">-</span><span style="color: #000000; "> square_sum;<br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> is_negative </span><span style="color: #000000; ">=</span><span style="color: #000000; "> difference </span><span style="color: #000000; ">></span><span style="color: #000000; "> INT_MAX;<br /><br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (is_negative) {<br />    difference </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">difference;<br />    square_difference </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">square_difference;<br />  } <br />   <br />  assert(difference </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; "> </span><span style="color: #000000; ">&&</span><span style="color: #000000; "> square_difference </span><span style="color: #000000; ">%</span><span style="color: #000000; "> difference </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">);<br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned sum_two </span><span style="color: #000000; ">=</span><span style="color: #000000; "> square_difference </span><span style="color: #000000; ">/</span><span style="color: #000000; "> difference;<br />  <br />  assert((sum_two </span><span style="color: #000000; ">+</span><span style="color: #000000; "> difference) </span><span style="color: #000000; ">%</span><span style="color: #000000; "> </span><span style="color: #000000; ">2u</span><span style="color: #000000; "> </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">);<br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned larger  </span><span style="color: #000000; ">=</span><span style="color: #000000; "> (sum_two </span><span style="color: #000000; ">+</span><span style="color: #000000; "> difference) </span><span style="color: #000000; ">/</span><span style="color: #000000; "> </span><span style="color: #000000; ">2u</span><span style="color: #000000; ">;<br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> unsigned smaller </span><span style="color: #000000; ">=</span><span style="color: #000000; "> (sum_two </span><span style="color: #000000; ">-</span><span style="color: #000000; "> difference) </span><span style="color: #000000; ">/</span><span style="color: #000000; "> </span><span style="color: #000000; ">2u</span><span style="color: #000000; ">;<br />  <br />  </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (is_negative) {<br />    </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> Pair result </span><span style="color: #000000; ">=</span><span style="color: #000000; "> { smaller, larger};<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> result;<br />  }<br />  </span><span style="color: #0000FF; ">const</span><span style="color: #000000; "> Pair result </span><span style="color: #000000; ">=</span><span style="color: #000000; "> { larger, smaller};<br />  </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> result;<br />}<br /><br /><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> main()<br />{<br /><br />}<br /><br /><br /></span></div></div><img src ="http://www.shnenglu.com/flyinghearts/aggbug/168267.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2012-03-18 21:16 <a href="http://www.shnenglu.com/flyinghearts/archive/2012/03/18/168267.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> Fibonacci数计中的两个思维盲点及其扩展数列的通用高效解法http://www.shnenglu.com/flyinghearts/archive/2012/02/28/166717.htmlflyingheartsflyingheartsTue, 28 Feb 2012 14:14:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2012/02/28/166717.htmlhttp://www.shnenglu.com/flyinghearts/comments/166717.htmlhttp://www.shnenglu.com/flyinghearts/archive/2012/02/28/166717.html#Feedback11http://www.shnenglu.com/flyinghearts/comments/commentRss/166717.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/166717.html阅读全文

flyinghearts 2012-02-28 22:14 发表评论
]]>
面试题精解之一Q?二叉?/title><link>http://www.shnenglu.com/flyinghearts/archive/2012/02/28/166713.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Tue, 28 Feb 2012 12:54:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2012/02/28/166713.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/166713.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2012/02/28/166713.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/166713.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/166713.html</trackback:ping><description><![CDATA[     摘要: 面试题精解之一Q?二叉?  本篇文章发表在下面三个博客中Q如果出现排版问题,L步到另一个博客?http://www.shnenglu.com/flyinghearts http://www.cnblogs.com/flyinghearts http://blog.csdn.net/flyinghearts   1     求二叉树中相...  <a href='http://www.shnenglu.com/flyinghearts/archive/2012/02/28/166713.html'>阅读全文</a><img src ="http://www.shnenglu.com/flyinghearts/aggbug/166713.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2012-02-28 20:54 <a href="http://www.shnenglu.com/flyinghearts/archive/2012/02/28/166713.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>喝汽水问?/title><link>http://www.shnenglu.com/flyinghearts/archive/2011/09/23/156635.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Fri, 23 Sep 2011 11:45:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2011/09/23/156635.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/156635.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2011/09/23/156635.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/156635.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/156635.html</trackback:ping><description><![CDATA[<div> <p style="text-align: center; text-indent: 23.6pt;" align="center"><strong><span style="font-size: 12pt; font-family: 宋体;">喝汽水问?/span></strong></p> <p style="text-align: center; text-indent: 23.6pt;" align="center"><strong><span style="font-size: 12pt;"><span>                                                  </span></span></strong><span style="font-size: 12pt;">by flyinghearts</span></p> <p style="text-indent: 23.6pt;"><strong> </strong></p> <p style="text-indent: 23.6pt;"><strong><span style="font-size: 12pt; font-family: 宋体;">?/span></strong><strong><span style="font-size: 12pt;">1000</span></strong><strong><span style="font-size: 12pt; font-family: 宋体;">瓶汽_喝完后每</span></strong><strong><span style="font-size: 12pt;">3</span></strong><strong><span style="font-size: 12pt; font-family: 宋体;">个空瓶能?/span></strong><strong><span style="font-size: 12pt;">1</span></strong><strong><span style="font-size: 12pt; font-family: 宋体;">瓶汽_问最后最多可以喝几瓶汽水Q此时剩余几个空Ӟ</span></strong></p> <p> </p> <p> </p> <p style="text-indent: 20.65pt;"><strong><span style="font-family: 宋体;">不妨假设Q共?/span>n</strong><strong><span style="font-family: 宋体;">瓶汽_?/span>a</strong><strong><span style="font-family: 宋体;">个空瓶能?/span>b</strong><strong><span style="font-family: 宋体;">瓶汽_</span>a > b</strong><strong><span style="font-family: 宋体;">Q?/span></strong><span style="font-family: 宋体;">刚开始有</span>n<span style="font-family: 宋体;">瓶汽_喝完后就?/span>n<span style="font-family: 宋体;">个空Ӟ多喝的汽水是靠空瓶换来的Q?strong><span style="color: blue;">每进行一ơ空瓶换汽水Q就能多?/span></strong></span><strong><span style="color: blue;">b</span></strong><strong><span style="font-family: 宋体; color: blue;">瓶汽水、空瓶数目就减少?/span><span style="color: blue;">a-b</span></strong><strong><span style="font-family: 宋体; color: blue;">?/span></strong><span style="font-family: 宋体;">Q?/span>a<span style="font-family: 宋体;">个空瓶换?/span>b<span style="font-family: 宋体;">瓶汽_喝完后得?/span>b<span style="font-family: 宋体;">个空Ӟ?/span></p> <p> </p> <p style="text-indent: 10.5pt;">(<span style="font-family: 宋体;">下面<strong>?/strong></span><strong> [x] </strong><strong><span style="font-family: 宋体;">表示</span>x</strong><strong><span style="font-family: 宋体;">的整数部?/span></strong>)</p> <p><strong><span style="color: #993300;">1 </span></strong><strong><span style="font-family: 宋体; color: #993300;">如果允许从别处(老板或其他顾客处Q借空Ӟ当然Q有借有q)</span></strong></p> <p style="text-indent: 10.55pt;"><strong><span style="font-family: 宋体; color: blue;">I瓶换汽水次敎ͼ</span><span style="color: blue;">   [n / (a - b)]</span></strong></p> <p style="text-indent: 10.55pt;"><strong><span style="font-family: 宋体; color: blue;">最后剩余空Ӟ</span><span style="color: blue;">     n % (a - b)</span></strong></p> <p style="text-indent: 10.55pt;"><strong><span style="font-family: 宋体; color: blue;">d可以喝到汽水Q?/span><span style="color: blue;"> n + [n / (a - b)] * b</span></strong></p> <p> </p> <p><strong><span style="color: #993300;">2 </span></strong><strong><span style="font-family: 宋体; color: #993300;">不允许借空?/span></strong></p> <p> <span style="font-family: 宋体;">I瓶换汽水过E中Q?strong><span style="color: blue;">一但空瓶数于</span></strong></span><strong><span style="color: blue;">a</span></strong><strong><span style="font-family: 宋体; color: blue;">Q则停止交换?/span></strong></p> <p> <span style="font-family: 宋体;">?/span> n < a<span style="font-family: 宋体;">Q显ӞI瓶换汽水次Cؓ</span>0<span style="font-family: 宋体;">Qd可以喝到汽水</span>n<span style="font-family: 宋体;">Ӟ最后剩余空?/span>n<span style="font-family: 宋体;">?/span></p> <p> <span style="font-family: 宋体;">?/span> n >= a<span style="font-family: 宋体;">Q(下面提供三种解法Q?/span></p> <p>  <span style="font-family: 宋体;">解法一</span><span>    </span><span style="font-family: 宋体;">I瓶换汽水次?/span>k<span style="font-family: 宋体;">{于满</span>n – (a-b)*t < a<span style="font-family: 宋体;">的最非负整?/span>t:</p> <p><span><span>        </span>      t > (n-a)/(a-b)</span><span style="font-family: 宋体;">Q最的</span>t<span style="font-family: 宋体;">?/span> [(n-a)/(a-b)] + 1 = [(n-b)/(a-b)]</p> <p><span><span>            </span>  </span><span style="font-family: 宋体;">剩余的空瓶数Q?/span>n – (a-b)*t </p> <p style="text-indent: 5cm;">= n – (a-b)*([(n-b)/(a-b)]) </p> <p><span><span>         </span>                  = b + (n-b) - (a-b)*([(n-b)/(a-b)])</span></p> <p style="text-indent: 5cm;">= b + (n-b)%(a-b)</p> <p style="text-indent: 136.5pt;"> </p> <p style="margin-left: 68.25pt; text-indent: -52.5pt;"><span style="font-family: 宋体;">解法?/span> <span>  </span><span style="font-family: 宋体;">先预?/span>a<span style="font-family: 宋体;">个空Ӟ剩余的</span>n-a<span style="font-family: 宋体;">个空瓶进行换汽水Q换的过E中Q若I瓶不够</span>a<span style="font-family: 宋体;">个,则从预留?/span>a<span style="font-family: 宋体;">个空瓶中“?#8221;Q因而,</span></p> <p style="margin-left: 57.75pt; text-indent: 10.5pt;"><span style="font-family: 宋体;">I瓶换汽水次敎ͼ</span>[(n-a)/(a-b)] + 1 = [(n-b)/(a-b)]<span style="font-family: 宋体;">Q预留的</span>a<span style="font-family: 宋体;">个空瓶也能换一ơ)</span></p> <p style="text-indent: 68.25pt;"><span style="font-family: 宋体;">最后剩余空Ӟ</span> (n-b) % (a-b) + b<span style="font-family: 宋体;">Q预留的</span>a<span style="font-family: 宋体;">个空瓶换?/span>b<span style="font-family: 宋体;">瓶汽_</span></p> <p style="text-indent: 78.75pt;"> </p> <p style="margin-left: 63pt; text-indent: -47.25pt;"><span style="font-family: 宋体;">解法?/span><span>   </span><span style="font-family: 宋体;">最后一ơ空瓶换汽水得到?/span>b<span style="font-family: 宋体;">瓶汽_喝完后得?/span>b<span style="font-family: 宋体;">个空Ӟ因而最后剩余空瓶数必然大于</span>b<span style="font-family: 宋体;">个,先预?/span>b<span style="font-family: 宋体;">个空Ӟ剩余的</span>n-b<span style="font-family: 宋体;">个空瓶进行换汽水Q若I瓶不够</span>a<span style="font-family: 宋体;">个,则从预留?/span>b<span style="font-family: 宋体;">个空瓶中“?#8221;Q由于能q行I瓶换汽_I瓶?/span>>= a – b<span style="font-family: 宋体;">Q因?#8220;?#8221;完后Q可以保证空瓶数大等?/span>a<span style="font-family: 宋体;">Q,</span></p> <p style="text-indent: 10.5pt;"><span>           </span><span style="font-family: 宋体;">I瓶换汽水次敎ͼ</span>[(n-b)/(a-b)]<span style="font-family: 宋体;">Q?/span> </p> <p style="text-indent: 68.25pt;"><span style="font-family: 宋体;">最l剩余空瓶数Q?/span>b + (n-b) % (a-b)</p> <p> </p> <p><span style="font-family: 宋体;">Q对解法三,</span>n>=b<span style="font-family: 宋体;">时结论成立,对解法一、二Q可以验?/span>n >=b<span style="font-family: 宋体;">Ӟl论也成立)</span></p> <p> </p> <p><span style="font-family: 宋体;">因而,<strong><span style="color: blue;">?/span></strong></span><strong><span style="color: blue;"> n >= b </span></strong></p> <p style="text-indent: 10.55pt;"><strong><span style="font-family: 宋体; color: blue;">I瓶换汽水次敎ͼ</span><span style="color: blue;">   [(n-b)/(a-b)] </span></strong></p> <p style="text-indent: 10.55pt;"><strong><span style="font-family: 宋体; color: blue;">最后剩余空Ӟ</span><span style="color: blue;">     b + (n-b) % (a-b)</span></strong></p> <p style="text-indent: 10.55pt;"><strong><span style="font-family: 宋体; color: blue;">d可以喝到汽水Q?/span><span style="color: blue;"> n + [(n-b)/(a-b)] * b</span></strong></p> <p> </p> <p><span style="font-family: 宋体;">对原题:</span></p> <p><span> n = 1000</span><span style="font-family: 宋体;">Q?/span>a = 3, b = 1<span style="font-family: 宋体;">Q?/span> a – b = 2</p> <p> <strong><span style="font-family: 宋体;">若允许借空?/span> <span>   </span></strong></p> <p style="text-indent: 25.7pt;"><span style="font-family: 宋体;">可以喝到汽水Q?/span> 1000 + 1000 / 2 = 1500</p> <p style="text-indent: 25.7pt;"><span style="font-family: 宋体;">剩余I瓶Q?/span>1000 % 2 = 0</p> <p style="text-indent: 25.7pt;"> </p> <p style="text-indent: 10.35pt;"><strong><span style="font-family: 宋体;">若不允许借空?/span> </strong></p> <p style="text-indent: 25.7pt;"><span style="font-family: 宋体;">可以喝到汽水Q?/span> 1000 + (1000 - 1) / 2 = 1499<span style="font-family: 宋体;">Q?/span> </p> <p style="text-indent: 25.7pt;"><span style="font-family: 宋体;">剩余I瓶Q?/span><span>     1 + (1000 - 1) % 2 = 2</span></p> <p> </p> </div><img src ="http://www.shnenglu.com/flyinghearts/aggbug/156635.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2011-09-23 19:45 <a href="http://www.shnenglu.com/flyinghearts/archive/2011/09/23/156635.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>三国?剧本修改?1.1http://www.shnenglu.com/flyinghearts/archive/2011/08/07/152739.htmlflyingheartsflyingheartsSun, 07 Aug 2011 15:24:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2011/08/07/152739.htmlhttp://www.shnenglu.com/flyinghearts/comments/152739.htmlhttp://www.shnenglu.com/flyinghearts/archive/2011/08/07/152739.html#Feedback1http://www.shnenglu.com/flyinghearts/comments/commentRss/152739.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/152739.html  l以前写的剧本修改器Q加了个界面Q本想用wtlQ但是代码中用到不少“初始化列?#8221;{c++11新特性,只好让VC2010靠边站了。对mingwQ好用的gui库还真没有,x十几个消息Q干脆直接调用windows api得了?br />


flyinghearts 2011-08-07 23:24 发表评论
]]>
对环状数l求q箋子数l的最大和http://www.shnenglu.com/flyinghearts/archive/2011/07/20/151513.htmlflyingheartsflyingheartsWed, 20 Jul 2011 15:49:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2011/07/20/151513.htmlhttp://www.shnenglu.com/flyinghearts/comments/151513.htmlhttp://www.shnenglu.com/flyinghearts/archive/2011/07/20/151513.html#Feedback2http://www.shnenglu.com/flyinghearts/comments/commentRss/151513.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/151513.html

 

思\Q由于数l元素d是固定|因而跨头、尾的连l子数组和最大,{h于求 中间那段和最。因此,问题转ؓ计算 q箋子数l的最大和 ?q箋子数l的最和


import std.algorithm;

 

int ring_max_continue_sum(in int[] src)

{

 int min_sum = src[0], cur_min_sum = min_sum;

 int max_sum = src[0], cur_max_sum = max_sum;

 int total_sum = src[0];

 

 foreach (value; src[1 .. $]) {

    cur_min_sum = min(value, cur_min_sum + value);

    min_sum = min(min_sum, cur_min_sum);

  

    cur_max_sum = max(value, cur_max_sum + value);

    max_sum = max(max_sum, cur_max_sum);

  

    total_sum += value;

 }

 return max(max_sum, total_sum - min_sum);

}

 

unittest {

 auto ta = [3, -2, 3];

 auto tb = [3, 4, -2, 3, -7, 1, -3, 8];

 assert(ta.ring_max_continue_sum == 6);

 assert(tb.ring_max_continue_sum == 16);

}

 

void main()

{

}

 



flyinghearts 2011-07-20 23:49 发表评论
]]>
最短摘要的生成Q补充)http://www.shnenglu.com/flyinghearts/archive/2011/07/20/151511.htmlflyingheartsflyingheartsWed, 20 Jul 2011 15:39:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2011/07/20/151511.htmlhttp://www.shnenglu.com/flyinghearts/comments/151511.htmlhttp://www.shnenglu.com/flyinghearts/archive/2011/07/20/151511.html#Feedback0http://www.shnenglu.com/flyinghearts/comments/commentRss/151511.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/151511.html阅读全文

flyinghearts 2011-07-20 23:39 发表评论
]]>
Fibonacci数列的两UO(lgn)解法http://www.shnenglu.com/flyinghearts/archive/2011/07/20/151510.htmlflyingheartsflyingheartsWed, 20 Jul 2011 15:30:00 GMThttp://www.shnenglu.com/flyinghearts/archive/2011/07/20/151510.htmlhttp://www.shnenglu.com/flyinghearts/comments/151510.htmlhttp://www.shnenglu.com/flyinghearts/archive/2011/07/20/151510.html#Feedback0http://www.shnenglu.com/flyinghearts/comments/commentRss/151510.htmlhttp://www.shnenglu.com/flyinghearts/services/trackbacks/151510.html 

import std.stdio : writef;

 

uint fib_matrix(uint num) // 矩阵法求fibonacci?/span>

{

 if (num <= 1) return num;

 --num;

 uint ret = 1, next = 1;

 for (uint a = 1, b = 0; num != 0; num >>= 1) {

    if (num & 1) {

       auto tmp = next;

       next = (a + b) * next + a * ret;

       ret = a * tmp + b * ret;

      }

      auto tmp = a;

      a = a * (a + 2 * b);

      b = b * b + tmp * tmp;

 }

 return ret;

}

 

uint fib(uint num)

{

 if (num <= 1) return num;

 

 uint left_most_one = num; //等于num的最?的kơ幂

 for (uint value = num; value &= (value - 1); ) left_most_one = value;

 

 uint ret = 1, next = 1;

 while (left_most_one >>= 1) {

    if (num & left_most_one) {

       auto tmp = ret;

      ret = ret * ret + next * next;

      next = (tmp * 2 + next) * next;

    } else {

       auto tmp = ret;

      ret = (next * 2 - ret) * ret;

      next = tmp * tmp + next * next;

    }

 }

 return ret;

}

 

uint fib_basic(uint num)

{

 if (num <= 1) return num;

 uint prev = 0, current = 1;

 for (uint i = 2; i <= num; ++i) {

    auto tmp = prev;

    prev = current;

    current += tmp;

 }

 return current;

}

 

unittest {

 foreach (i; 0 .. 48) {

    auto a = fib_basic(i), b = fib_matrix(i), c = fib(i);

    if (a != b || a != c)

      writef("%s %s %s %s\n", i, a, b, c);

 } 

}

 

void main()

{

 

}



flyinghearts 2011-07-20 23:30 发表评论
]]>
VC 2010 error D8027Q无法执行c1xx.dll的解x?/title><link>http://www.shnenglu.com/flyinghearts/archive/2011/07/19/151417.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Tue, 19 Jul 2011 14:07:00 GMT</pubDate><guid>http://www.shnenglu.com/flyinghearts/archive/2011/07/19/151417.html</guid><wfw:comment>http://www.shnenglu.com/flyinghearts/comments/151417.html</wfw:comment><comments>http://www.shnenglu.com/flyinghearts/archive/2011/07/19/151417.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.shnenglu.com/flyinghearts/comments/commentRss/151417.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/flyinghearts/services/trackbacks/151417.html</trackback:ping><description><![CDATA[<br /><div>  晚上惌个程序,l果VC 2010头一ơŞ工: <br />            cl: 命o?error D8027 :无法执行“E:\prog\vc2010\bin\c1xx.dll”<br />     上网搜烦了下Q也没找C个解x法(<a >官方说明</a>Q。正郁闷着Q突焉惛_cl.exe可以正常q行Q而c1xx.dll不能正常q行Q会不会是c1xx.dll的依赖库出问题了Q于是调出dependency一查,果然了个msvcp100.dllQ估计是某个卸蝲软g造成的)Q从别处拯一个到bin目录Q问题解冟?br /></div><img src ="http://www.shnenglu.com/flyinghearts/aggbug/151417.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/flyinghearts/" target="_blank">flyinghearts</a> 2011-07-19 22:07 <a href="http://www.shnenglu.com/flyinghearts/archive/2011/07/19/151417.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.shnenglu.com/" title="精品视频久久久久">精品视频久久久久</a> <div class="friend-links"> </div> </div> </footer> <a href="http://www.c6t9.cn" target="_blank">þۺ༤</a>| <a href="http://www.kinbay.cn" target="_blank">þþþƷһ </a>| <a href="http://www.lnsmgg.cn" target="_blank">þþƷˬӰ</a>| <a href="http://www.rytaoshumiao.cn" target="_blank">þ91Ʒ91þû</a>| <a href="http://www.iido.org.cn" target="_blank">˾Ʒþ</a>| <a href="http://www.ad2500.cn" target="_blank">þAV</a>| <a href="http://www.yangyongfu.com.cn" target="_blank">þùƷ99þþþþ</a>| <a href="http://www.lovebuyhome.com.cn" target="_blank">þþþþþۺ </a>| <a href="http://www.021-10000.cn" target="_blank">þҹҹݺ2022</a>| <a href="http://www.rszgrh2.cn" target="_blank">þó鱬Ļ</a>| <a href="http://www.sooppp.cn" target="_blank">ľþۺĻ</a>| <a href="http://www.haolook.cn" target="_blank">þ99Ʒþþþþhb</a>| <a href="http://www.jianluanwang.cn" target="_blank">aaþ</a>| <a href="http://www.zhongxiangka.cn" target="_blank">7777þþùƷ</a>| <a href="http://www.ipingpong.cn" target="_blank">AëƬþþƷ</a>| <a href="http://www.zzcjw.cn" target="_blank">ҹƷþþþþ</a>| <a href="http://www.baby-photo.com.cn" target="_blank">þ99Ʒþþþþ</a>| <a href="http://www.zambak.cn" target="_blank">һaƬþëƬ</a>| <a href="http://www.fmlw.net.cn" target="_blank">þøԴƷ999</a>| <a href="http://www.lureng.cn" target="_blank">þþƷ5555</a>| <a href="http://www.fzlqb.cn" target="_blank"> ŷ þ</a>| <a href="http://www.hyattzhuzhou.cn" target="_blank">ŷ糱þþXXXXx</a>| <a href="http://www.tv81.cn" target="_blank">avɫۺϾþavɫۺ</a>| <a href="http://www.taiguolu.com.cn" target="_blank">Ʒþþþû</a>| <a href="http://www.hohong.com.cn" target="_blank">Ʒ99þ99þþ</a>| <a href="http://www.gh688.cn" target="_blank">þþƷAvӰƬ </a>| <a href="http://www.pcsaver.cn" target="_blank">66ƷۺϾþþþþþ</a>| <a href="http://www.vnxz.cn" target="_blank">vĻþ þһ ľþþþר </a>| <a href="http://www.4neq.cn" target="_blank">þþþ</a>| <a href="http://www.xahjqc.cn" target="_blank">þþþþþۺ</a>| <a href="http://www.mail-lists.cn" target="_blank">þŷƷ</a>| <a href="http://www.07ww.cn" target="_blank">뾫Ʒþþþ..</a>| <a href="http://www.fc117.cn" target="_blank">99þ㽶߿</a>| <a href="http://www.zmlxs.cn" target="_blank">˾þ</a>| <a href="http://www.guwz.cn" target="_blank">ҹþþ</a>| <a href="http://www.h21jc.cn" target="_blank">Ʒþþþþ³</a>| <a href="http://www.k8uvo.cn" target="_blank">ϵרþ</a>| <a href="http://www.tyo8.cn" target="_blank">þ޾Ʒ벥</a>| <a href="http://www.shaikr.cn" target="_blank">þþƷ7777</a>| <a href="http://www.hifistudio.cn" target="_blank">þþþùƷ</a>| <a href="http://www.snryw.cn" target="_blank">þþþƷҰ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>