﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-beautykingdom-随笔分类-C++_BASIS</title><link>http://www.cppblog.com/beautykingdom/category/7610.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 08 Jul 2012 15:07:11 GMT</lastBuildDate><pubDate>Sun, 08 Jul 2012 15:07:11 GMT</pubDate><ttl>60</ttl><item><title>程序员面试题精选100题(08)－求1+2+...+n[C/C++/C#] &lt;转&gt;</title><link>http://www.cppblog.com/beautykingdom/archive/2012/07/05/181446.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Thu, 05 Jul 2012 02:04:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2012/07/05/181446.html</guid><wfw:comment>http://www.cppblog.com/beautykingdom/comments/181446.html</wfw:comment><comments>http://www.cppblog.com/beautykingdom/archive/2012/07/05/181446.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/beautykingdom/comments/commentRss/181446.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/beautykingdom/services/trackbacks/181446.html</trackback:ping><description><![CDATA[<p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><a href="http://zhedahht.blog.163.com/blog/static/2541117420072915131422/">http://zhedahht.blog.163.com/blog/static/2541117420072915131422/</a><span style="font-family: SimSun; ">题目：求</span>1+2+&#8230;+n<span style="font-family: SimSun; ">，要求不能使用乘除法、</span>for<span style="font-family: SimSun; ">、</span>while<span style="font-family: SimSun; ">、</span>if<span style="font-family: SimSun; ">、</span>else<span style="font-family: SimSun; ">、</span>switch<span style="font-family: SimSun; ">、</span>case<span style="font-family: SimSun; ">等关键字以及条件判断语句（</span>A?B:C<span style="font-family: SimSun; ">）。</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="font-family: SimSun; ">分析：这道题没有多少实际意义，因为在软件开发中不会有这么变态的限制。但这道题却能有效地考查发散思维能力，而发散思维能力能反映出对编程相关技术理解的深刻程度。</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="font-family: SimSun; ">通常求</span>1+2+&#8230;+n<span style="font-family: SimSun; ">除了用公式</span>n(n+1)/2<span style="font-family: SimSun; ">之外，无外乎循环和递归两种思路。由于已经明确限制</span>for<span style="font-family: SimSun; ">和</span>while<span style="font-family: SimSun; ">的使用，循环已经不能再用了。同样，递归函数也需要用</span>if<span style="font-family: SimSun; ">语句或者条件判断语句来判断是继续递归下去还是终止递归，但现在题目已经不允许使用这两种语句了。</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="font-family: SimSun; ">我们仍然围绕循环做文章。循环只是让相同的代码执行</span>n<span style="font-family: SimSun; ">遍而已，我们完全可以不用</span>for<span style="font-family: SimSun; ">和</span>while<span style="font-family: SimSun; ">达到这个效果。比如定义一个类，我们</span>new<span style="font-family: SimSun; ">一含有</span>n<span style="font-family: SimSun; ">个这种类型元素的数组，那么该类的构造函数将确定会被调用</span>n<span style="font-family: SimSun; ">次。我们可以将需要执行的代码放到构造函数里。如下代码正是基于这个思路：</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">class</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;Temp<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">public</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">:<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Temp() { ++ N; Sum += N; }<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; "><br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">static</span>&nbsp;<span style="color: blue; ">void</span>&nbsp;Reset() { N = 0; Sum = 0; }<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">static</span>&nbsp;<span style="color: blue; ">int</span>&nbsp;GetSum() {&nbsp;<span style="color: blue; ">return</span>&nbsp;Sum; }<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; "><br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">private</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">:<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">static</span>&nbsp;<span style="color: blue; ">int</span>&nbsp;N;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">static</span>&nbsp;<span style="color: blue; ">int</span>&nbsp;Sum;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">};<br /><br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">int</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;Temp::N = 0;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">int</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;Temp::Sum = 0;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; "><br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">int</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;solution1_Sum(<span style="color: blue; ">int</span>&nbsp;n)<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /><span style="font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Temp::Reset();<br /></span><br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Temp *a =&nbsp;<span style="color: blue; ">new</span>&nbsp;Temp[n];<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">delete</span>&nbsp;[]a;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = 0;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp;Temp::GetSum();<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">}</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="font-family: SimSun; ">我们同样也可以围绕递归做文章。既然不能判断是不是应该终止递归，我们不妨定义两个函数。一个函数充当递归函数的角色，另一个函数处理终止递归的情况，我们需要做的就是在两个函数里二选一。从二选一我们很自然的想到布尔变量，比如</span>ture<span style="font-family: SimSun; ">（</span>1<span style="font-family: SimSun; ">）的时候调用第一个函数，</span>false<span style="font-family: SimSun; ">（</span>0<span style="font-family: SimSun; ">）的时候调用第二个函数。那现在的问题是如和把数值变量</span>n<span style="font-family: SimSun; ">转换成布尔值。如果对</span>n<span style="font-family: SimSun; ">连续做两次反运算，即</span>!!n<span style="font-family: SimSun; ">，那么非零的</span>n<span style="font-family: SimSun; ">转换为</span>true<span style="font-family: SimSun; ">，</span>0<span style="font-family: SimSun; ">转换为</span>false<span style="font-family: SimSun; ">。有了上述分析，我们再来看下面的代码：</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">class</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;A;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">A* Array[2];<br /><br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">class</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;A<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">public</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">:<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">virtual</span>&nbsp;<span style="color: blue; ">int</span>&nbsp;Sum (<span style="color: blue; ">int</span>&nbsp;n) {&nbsp;<span style="color: blue; ">return</span>&nbsp;0; }<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">};<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; "><br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">class</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;B:&nbsp;<span style="color: blue; ">public</span>&nbsp;A<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">public</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">:<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">virtual</span>&nbsp;<span style="color: blue; ">int</span>&nbsp;Sum (<span style="color: blue; ">int</span>&nbsp;n) {&nbsp;<span style="color: blue; ">return</span>&nbsp;Array[!!n]-&gt;Sum(n-1)+n; }<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">};<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; "><br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">int</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;solution2_Sum(<span style="color: blue; ">int</span>&nbsp;n)<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A a;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B b;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array[0] = &amp;a;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array[1] = &amp;b;<br /><br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">int</span>&nbsp;value = Array[1]-&gt;Sum(n);<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; "><br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">return</span>&nbsp;value;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">}</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="font-family: SimSun; ">这种方法是用虚函数来实现函数的选择。当</span>n<span style="font-family: SimSun; ">不为零时，执行函数</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">B::Sum</span><span style="font-family: SimSun; ">；当</span>n<span style="font-family: SimSun; ">为</span>0<span style="font-family: SimSun; ">时，执行</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">A::Sum</span><span style="font-family: SimSun; ">。我们也可以直接用函数指针数组，这样可能还更直接一些：</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">typedef</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;<span style="color: blue; ">int</span>&nbsp;(*fun)(<span style="color: blue; ">int</span>);<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; "><br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">int</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;solution3_f1(<span style="color: blue; ">int</span>&nbsp;i)&nbsp;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">return</span>&nbsp;0;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">}<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; "><br /></span><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">int</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;solution3_f2(<span style="color: blue; ">int</span>&nbsp;i)<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fun f[2]={solution3_f1, solution3_f2};&nbsp;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">return</span>&nbsp;i+f[!!i](i-1);<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">}</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="line-height: 23px; font-family: SimSun; font-size: 10pt; ">另外我们还可以让编译器帮我们来完成类似于递归的运算，比如如下代码：</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">template</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&lt;<span style="color: blue; ">int</span>&nbsp;n&gt;&nbsp;<span style="color: blue; ">struct</span>&nbsp;solution4_Sum<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">enum</span>&nbsp;Value { N = solution4_Sum&lt;n - 1&gt;::N + n};<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">};</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="line-height: 23px; font-family: 'Courier New'; color: blue; font-size: 10pt; ">template</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&lt;&gt;&nbsp;<span style="color: blue; ">struct</span>&nbsp;solution4_Sum&lt;1&gt;<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">{<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue; ">enum</span>&nbsp;Value { N = 1};<br /></span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">};</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">solution4_Sum&lt;100&gt;::N</span><span style="font-family: SimSun; ">就是</span>1+2+...+100<span style="font-family: SimSun; ">的结果。当编译器看到</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">solution4_Sum&lt;100&gt;</span><span style="font-family: SimSun; ">时，就是为模板类</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">solution4_Sum</span><span style="font-family: SimSun; ">以参数</span>100<span style="font-family: SimSun; ">生成该类型的代码。但以</span>100<span style="font-family: SimSun; ">为参数的类型需要得到以</span>99<span style="font-family: SimSun; ">为参数的类型，因为</span><span style="line-height: 23px; font-family: 'Courier New'; font-size: 10pt; ">solution4_Sum&lt;100&gt;::N=solution4_Sum&lt;99&gt;::N+100</span><span style="font-family: SimSun; ">。这个过程会递归一直到参数为</span>1<span style="font-family: SimSun; ">的类型，由于该类型已经显式定义，编译器无需生成，递归编译到此结束。由于这个过程是在编译过程中完成的，因此要求输入</span>n<span style="font-family: SimSun; ">必须是在编译期间就能确定，不能动态输入。这是该方法最大的缺点。而且编译器对递归编译代码的递归深度是有限制的，也就是要求</span>n<span style="font-family: SimSun; ">不能太大。</span></p><p style="line-height: 25px; margin: 0px 0px 10px; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; "><span style="font-family: SimSun; ">大家还有更多、更巧妙的思路吗？欢迎讨论</span>^_^</p><span style="line-height: 25px; text-align: left; background-color: #ffffff; font-family: 宋体; "><p style="margin: 0in 0in 10pt; padding: 0px; text-indent: 0.5in; ">本文已经收录到《<span style="font-family: Calibri, sans-serif; "><a target="_blank" href="http://zhedahht.blog.163.com/blog/static/254111742011101624433132/" style="text-decoration: none; color: #990000; "><span style="font-family: 宋体; ">剑指</span>Offer<span style="font-family: 宋体; ">&#8212;&#8212;名企面试官精讲典型编程题</span></a></span>》一书中，有改动。欢迎关注。我把这篇博客翻译成了英文，感兴趣的朋友可以到</p></span><a target="_blank" rel="nofollow" href="http://codercareer.blogspot.com/2011/10/no-08-calculate-12n.html" style="line-height: 25px; text-decoration: none; color: #990000; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; ">http://codercareer.blogspot.com/2011/10/no-08-calculate-12n.html</a><span style="line-height: 25px; text-align: left; background-color: #ffffff; font-family: 宋体; ">查看。<br /></span><p style="line-height: 25px; margin: 0in 0in 10pt; padding: 0px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; text-align: left; background-color: #ffffff; text-indent: 0.5in; "><span style="font-family: 宋体; ">博主何海涛对本博客文章享有版权。网络转载请注明出处</span><a href="http://zhedahht.blog.163.com/" style="text-decoration: none; color: #990000; ">http://zhedahht.blog.163.com/</a><span style="font-family: 宋体; ">。整理出版物请和作者联系。</span><span style="font-family: 宋体; ">对解题思路有任何建议，欢迎在评论中告知，或者加我微博</span><a rel="nofollow" href="http://t.sina.com.cn/zhedahht" style="text-decoration: none; color: #990000; ">http://weibo.com/zhedahht</a><span style="font-family: 宋体; ">或者</span><a href="http://t.163.com/zhedahht" style="text-decoration: none; color: #990000; ">http://t.163.com/zhedahht</a><span style="font-family: 宋体; ">与我讨论。谢谢。<br />from：<br /></span><a href="http://zhedahht.blog.163.com/blog/static/2541117420072915131422/">http://zhedahht.blog.163.com/blog/static/2541117420072915131422/</a></p><img src ="http://www.cppblog.com/beautykingdom/aggbug/181446.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2012-07-05 10:04 <a href="http://www.cppblog.com/beautykingdom/archive/2012/07/05/181446.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>effective c++</title><link>http://www.cppblog.com/beautykingdom/archive/2012/06/01/177068.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Fri, 01 Jun 2012 07:00:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2012/06/01/177068.html</guid><wfw:comment>http://www.cppblog.com/beautykingdom/comments/177068.html</wfw:comment><comments>http://www.cppblog.com/beautykingdom/archive/2012/06/01/177068.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/beautykingdom/comments/commentRss/177068.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/beautykingdom/services/trackbacks/177068.html</trackback:ping><description><![CDATA[to be continued<br />最近该回顾下这本书了觉得，整本书会写一篇博客。看个差不多了就会开始写的。看完了书总是要留下些东西的。<img src ="http://www.cppblog.com/beautykingdom/aggbug/177068.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2012-06-01 15:00 <a href="http://www.cppblog.com/beautykingdom/archive/2012/06/01/177068.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>函数指针和指针函数&lt;forward&gt;</title><link>http://www.cppblog.com/beautykingdom/archive/2010/08/18/123887.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Wed, 18 Aug 2010 14:20:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2010/08/18/123887.html</guid><wfw:comment>http://www.cppblog.com/beautykingdom/comments/123887.html</wfw:comment><comments>http://www.cppblog.com/beautykingdom/archive/2010/08/18/123887.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/beautykingdom/comments/commentRss/123887.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/beautykingdom/services/trackbacks/123887.html</trackback:ping><description><![CDATA[<table style="table-layout: fixed; font-family: Arial; font-size: 12px; line-height: 18px; "><tbody><tr><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 18px; "><div class="cnt" style="font-family: Arial; word-wrap: break-word; word-break: normal; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 14px; line-height: 20px; color: rgb(51, 51, 51); overflow-x: hidden; overflow-y: hidden; position: static; "><p style="line-height: normal; "><font size="2" style="line-height: normal; "><font color="#0000ff" style="line-height: normal; ">【函数指针】</font><br style="line-height: normal; "><br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在程序运行中，函数代码是程序的算法指令部分，它们和数组一样也占用存储空间，都有相应的地址。可以使用指针变量指向数组的首地址，也可以使用指针变量指向函数代码的首地址，指向函数代码首地址的指针变量称为函数指针。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">1．函数指针定义</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">函数类型 （*指针变量名）(形参列表)；</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&#8220;函数类型&#8221;说明函数的返回类型，由于&#8220;()&#8221;的优先级高于&#8220;*&#8221;,所以指针变量名外的括号必不可少，后面的&#8220;形参列表&#8221;表示指针变量指向的函数所带的参数列表。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">例如：</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">int (*f)(int x);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">double (*ptr)(double x);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">在定义函数指针时请注意：<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; ">函数指针和它指向的函数的参数个数和类型都应该是—致的；</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">函数指针的类型和函数的返回值类型也必须是一致的。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">2．函数指针的赋值</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">函数名和数组名一样代表了函数代码的首地址，因此在赋值时，直接将函数指针指向函数名就行了。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">例如，</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">int func(int x);&nbsp;&nbsp; /* 声明一个函数 */</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">int (*f) (int x);&nbsp;&nbsp;&nbsp; /* 声明一个函数指针 */</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">f=func;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* 将func函数的首地址赋给指针f */</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">赋值时函数func不带括号，也不带参数，由于func代表函数的首地址，因此经过赋值以后，指针f就指向函数func(x)的代码的首地址。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">3．通过函数指针调用函数</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">函数指针是通过函数名及有关参数进行调用的。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">与其他指针变量相类似，如果指针变量pi是指向某整型变量i的指针，则*p等于它所指的变量i；如果pf是指向某浮点型变量f的指针，则*pf就等价于它所指的变量f。同样地，*f是指向函数func(x)的指针，则*f就代表它所指向的函数func。所以在执行了f=func;之后，(*f)和func代表同一函数。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">由于函数指针指向存储区中的某个函数，因此可以通过函数指针调用相应的函数。现在我们就讨论如何用函数指针调用函数，它应执行下面三步：</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">首先，要说明函数指针变量。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">例如：int (*f)(int x);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">其次，要对函数指针变量赋值。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">例如： f=func;&nbsp;&nbsp;&nbsp; (func(x)必须先要有定义)</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">最后，要用 (*指针变量)(参数表);调用函数。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">例如：&nbsp;&nbsp;&nbsp; (*f)(x);(x必须先赋值)</font></p><p style="line-height: normal; "><br style="line-height: normal; "><font size="2" style="line-height: normal; ">【例】任意输入n个数，找出其中最大数，并且输出最大数值。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">main()</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">{</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int f();</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i，a，b;</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int (*p)();&nbsp;&nbsp;&nbsp; /* 定义函数指针 */</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf("%d"，&amp;a);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p=f;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* 给函数指针p赋值，使它指向函数f */</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(i＝1;i&lt;9;i++)</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf("%d"，&amp;b);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a＝(*p)(a，b);&nbsp;&nbsp;&nbsp; /* 通过指针p调用函数f */</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("The Max Number is:%d"，a)</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">}</font></p><p style="line-height: normal; "><br style="line-height: normal; "><font size="2" style="line-height: normal; ">f(int x，int y)</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">{</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; int z;</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; z＝(x&gt;y)?x:y;</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; return(z);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">}</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">运行结果为：</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">343 -45 4389 4235 1 -534 988 555 789↙</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">The Max Number is：4389</font></p><p style="line-height: normal; "><br style="line-height: normal; "><font color="#0000ff" size="2" style="line-height: normal; ">【指针函数】</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">一个函数不仅可以带回一个整型数据的值，字符类型值和实型类型的值，还可以带回指针类型的数据，使其指向某个地址单元。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 返回指针的函数，一般定义格式为：</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 类型标识符&nbsp;&nbsp;&nbsp; *函数名(参数表)</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">int *f(x，y);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">其中x，y是形式参数，f是函数名，调用后返回一个指向整型数据的地址指针。f(x，y)是函数，其值是指针。</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">如：char *ch();表示的就是一个返回字符型指针的函数，请看下面的例题：</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">【例】将字符串1(str1)复制到字符串2(str2)，并输出字符串2.</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">#include "stdio.h"</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">main()</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">{</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; char *ch(char *，char *);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; char str1[]="I am glad to meet you!";</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; char str2[]="Welcom to study C!";</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; printf("%s"，ch(str1，str2));</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">}</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">char *ch(char *str1，char *str2)</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">{</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; int i;</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; char *p;</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; p=str2<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp; if(*str2==NULL) exit(-1);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; do</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; {</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *str2=*str1;</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str1++;</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str2++;</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; }while(*str1!=NULL);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">&nbsp;&nbsp;&nbsp; return(p);</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">}</font></p><p style="line-height: normal; "><br style="line-height: normal; "><font size="2" style="line-height: normal; ">通过分析可得</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">函数指针是一个指向函数的指针，而指针函数只是说明他是一个返回值为指针的函数，</font></p><p style="line-height: normal; "><font size="2" style="line-height: normal; ">函数指针可以用来指向一个函数。<br style="line-height: normal; "></font></p></div></td></tr></tbody></table><span  style="font-family: Arial; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial; font-size: 12px; line-height: 18px; ">本文引用通告地址：</span><span  style="font-family: Arial; font-size: 12px; line-height: 18px; "><a href="http://lionwq.spaces.eepw.com.cn/articles/article/item/18258#" style="color: rgb(0, 51, 204); ">http://lionwq.spaces.eepw.com.cn/articles/trackback/item/18258</a></span>

<div>from:</div><div><a href="http://lionwq.spaces.eepw.com.cn/articles/article/item/18258">http://lionwq.spaces.eepw.com.cn/articles/article/item/18258</a></div><img src ="http://www.cppblog.com/beautykingdom/aggbug/123887.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/beautykingdom/" target="_blank">chatler</a> 2010-08-18 22:20 <a href="http://www.cppblog.com/beautykingdom/archive/2010/08/18/123887.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>几道笔试题</title><link>http://www.cppblog.com/beautykingdom/archive/2010/07/23/121131.html</link><dc:creator>chatler</dc:creator><author>chatler</author><pubDate>Fri, 23 Jul 2010 09:15:00 GMT</pubDate><guid>http://www.cppblog.com/beautykingdom/archive/2010/07/23/121131.html</guid><wfw:comment>http://www.cppblog.com/beautykingdom/comments/121131.html</wfw:comment><comments>http://www.cppblog.com/beautykingdom/archive/2010/07/23/121131.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/beautykingdom/comments/commentRss/121131.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/beautykingdom/services/trackbacks/121131.html</trackback:ping><description><![CDATA[<strong><span lang="EN-US"><span><font face="Arial">1、</font><span style="font: 7pt 'Times New Roman';"> </span></span></span></strong><strong><span lang="EN-US"><font face="Arial">C++ </font></span></strong><strong><span style="font-family: 宋体;">定义了哪些类型转换操作符？分别有什么作用？</span><span lang="EN-US"><o:p></o:p></span></strong>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt 18pt;"><strong><span lang="EN-US"><o:p><font face="Arial">&nbsp;</font></o:p></span></strong></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><strong><span style="font-size: 9pt; color: red; font-family: 宋体;">①</span></strong><strong><span style="font-size: 9pt; color: red;" lang="EN-US"><font face="Arial"> </font></span></strong><strong><span style="font-size: 9pt; color: red; font-family: 宋体;">定义了四个操作符：</span></strong><strong><span style="font-size: 9pt; color: red;" lang="EN-US"><font face="Arial">static_cast</font></span></strong><strong><span style="font-size: 9pt; color: red; font-family: 宋体;">，</span></strong><strong><span style="font-size: 9pt; color: red;" lang="EN-US"><font face="Arial">const_cast</font></span></strong><strong><span style="font-size: 9pt; color: red; font-family: 宋体;">，</span></strong><strong><span style="font-size: 9pt; color: red;" lang="EN-US"><font face="Arial">dynamic_cast</font></span></strong><strong><span style="font-size: 9pt; color: red; font-family: 宋体;">和</span></strong><strong><span style="font-size: 9pt; color: red;" lang="EN-US"><font face="Arial">reinterpret_cast</font></span></strong><strong><span style="font-size: 9pt; color: red; font-family: 宋体;">。</span></strong><strong><span style="font-size: 9pt; color: red;" lang="EN-US"><o:p></o:p></span></strong></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 18pt;"><strong><span style="font-size: 9pt; color: red;" lang="EN-US"><o:p><font face="Arial">&nbsp;</font></o:p></span></strong></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><strong><span style="font-size: 9pt; color: red; font-family: 宋体;">②</span></strong><strong><span style="font-size: 9pt; color: red;" lang="EN-US"><font face="Arial"> static_cast</font></span></strong><strong><span style="font-size: 9pt; color: red; font-family: 宋体;">：</span></strong><span style="font-size: 9pt; color: blue; font-family: 宋体;">可以被用于强制类型转换</span><span style="font-size: 9pt; color: red; font-family: 宋体;">（例如，</span><span style="font-size: 9pt; color: red; font-family: 'calibri','sans-serif';" lang="EN-US">non-const</span><span style="font-size: 9pt; color: red; font-family: 宋体;">对象转换为</span><span style="font-size: 9pt; color: red; font-family: 'calibri','sans-serif';" lang="EN-US">const</span><span style="font-size: 9pt; color: red; font-family: 宋体;">对象，</span><span style="font-size: 9pt; color: red; font-family: 'calibri','sans-serif';" lang="EN-US">int</span><span style="font-size: 9pt; color: red; font-family: 宋体;">转换为</span><span style="font-size: 9pt; color: red; font-family: 'calibri','sans-serif';" lang="EN-US">double</span><span style="font-size: 9pt; color: red; font-family: 宋体;">，</span><span style="font-size: 9pt; color: red; font-family: 宋体;">等等），</span><span style="font-size: 9pt; color: blue; font-family: 宋体;">它还可以用于很多这样的转换的反向转换</span><span style="font-size: 9pt; color: red; font-family: 宋体;">（例如，</span><span style="font-size: 9pt; color: red; font-family: 'calibri','sans-serif';" lang="EN-US">void*</span><span style="font-size: 9pt; color: red; font-family: 宋体;">指针转换为有类型指针，基类指针转换为派生类指针）。</span><span style="font-size: 9pt; color: blue; font-family: 宋体;">但是它不能将一个</span><span style="font-size: 9pt; color: blue; font-family: 'calibri','sans-serif';" lang="EN-US">const</span><span style="font-size: 9pt; color: blue; font-family: 宋体;">对象转换为一个</span><span style="font-size: 9pt; color: blue; font-family: 'calibri','sans-serif';" lang="EN-US">non-const</span><span style="font-size: 9pt; color: blue; font-family: 宋体;">对象</span><span style="font-size: 9pt; color: red; font-family: 宋体;">（只有</span><span st