﻿<?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++博客-一些事╰一些情^-文章分类-我的程序设计之路</title><link>http://www.cppblog.com/ice-fish/category/8636.html</link><description>专注于C++技术</description><language>zh-cn</language><lastBuildDate>Sun, 26 Oct 2008 09:30:43 GMT</lastBuildDate><pubDate>Sun, 26 Oct 2008 09:30:43 GMT</pubDate><ttl>60</ttl><item><title>判断是否为闰年</title><link>http://www.cppblog.com/ice-fish/articles/65111.html</link><dc:creator>ice-fish</dc:creator><author>ice-fish</author><pubDate>Sun, 26 Oct 2008 08:58:00 GMT</pubDate><guid>http://www.cppblog.com/ice-fish/articles/65111.html</guid><wfw:comment>http://www.cppblog.com/ice-fish/comments/65111.html</wfw:comment><comments>http://www.cppblog.com/ice-fish/articles/65111.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ice-fish/comments/commentRss/65111.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ice-fish/services/trackbacks/65111.html</trackback:ping><description><![CDATA[<blockquote dir=ltr style="MARGIN-RIGHT: 0px">
<p>程序：<br><strong>#include &lt;iostream.h&gt;<br>void main()<br>{<br>int year;<br>cout&lt;&lt;"please input the year"&lt;&lt;endl;<br>cin&gt;&gt;year;<br>if(year%100==0)<br>{if(year%400==0)<br>cout&lt;&lt;"It is a leap year!"&lt;&lt;endl;<br>else<br>cout&lt;&lt;"It is not a leap year!"&lt;&lt;endl;<br>}<br>else<br>{if(year%4==0)<br>cout&lt;&lt;"It is a leap year!"&lt;&lt;endl;<br>else<br>cout&lt;&lt;"It is not a leap year"&lt;&lt;endl;<br>}<br>}<br></strong><br><br><br>好像这是个入门级作品哦，一开始把判断条件搞错，还一直在怀疑1900年为什么是闰年，呵呵。</p>
</blockquote>
<img src ="http://www.cppblog.com/ice-fish/aggbug/65111.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ice-fish/" target="_blank">ice-fish</a> 2008-10-26 16:58 <a href="http://www.cppblog.com/ice-fish/articles/65111.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>求100-999中的水仙花数</title><link>http://www.cppblog.com/ice-fish/articles/65072.html</link><dc:creator>ice-fish</dc:creator><author>ice-fish</author><pubDate>Sun, 26 Oct 2008 03:46:00 GMT</pubDate><guid>http://www.cppblog.com/ice-fish/articles/65072.html</guid><wfw:comment>http://www.cppblog.com/ice-fish/comments/65072.html</wfw:comment><comments>http://www.cppblog.com/ice-fish/articles/65072.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ice-fish/comments/commentRss/65072.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ice-fish/services/trackbacks/65072.html</trackback:ping><description><![CDATA[<blockquote dir=ltr style="MARGIN-RIGHT: 0px">
<p>呵呵，可能这个问题比较简单。但开始刚刚接触，虽然这个问题很经典，但我想了比较久才把它弄出来。因为在pow函数那里卡住了，但后来慢慢看书，终于把它给弄出来啦，后来在网上找了找解法，发现和我的大同小异，还是想想是否有更简单的解法吧。</p>
<p style="COLOR: #800000">#include&lt;iostream.h&gt;<o:p></o:p></p>
<p style="COLOR: #800000">#include&lt;math.h&gt;<o:p></o:p></p>
<p style="COLOR: #800000">void main()<o:p></o:p></p>
<p style="COLOR: #800000">{<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp; int m,a,b,c,tmp;<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp; cout&lt;&lt;"The Narcissistic Number is "&lt;&lt;endl;<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp; for(m=100;m&lt;=999;m++)<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp;&nbsp; {a=m/100;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <o:p></o:p></p>
<p style="COLOR: #800000">&nbsp;&nbsp;&nbsp; b=(m%100)/10;<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp;&nbsp;&nbsp; c=m%10;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //把数分成三位，存在不同的地方<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp;&nbsp;&nbsp; tmp=pow(a,3)+pow(b,3)+pow(c,3);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //求出各位数的三次方之和<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp;&nbsp; if(tmp==m)<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp;&nbsp; cout&lt;&lt;m&lt;&lt;endl; //输出该水仙花数<o:p></o:p></p>
<p style="COLOR: #800000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <o:p></o:p></p>
<p style="COLOR: #800000">&nbsp;&nbsp;&nbsp; }<o:p></o:p></p>
</blockquote>
<img src ="http://www.cppblog.com/ice-fish/aggbug/65072.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ice-fish/" target="_blank">ice-fish</a> 2008-10-26 11:46 <a href="http://www.cppblog.com/ice-fish/articles/65072.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用迭代公式编程求某一正整数a的平方根</title><link>http://www.cppblog.com/ice-fish/articles/65070.html</link><dc:creator>ice-fish</dc:creator><author>ice-fish</author><pubDate>Sun, 26 Oct 2008 03:34:00 GMT</pubDate><guid>http://www.cppblog.com/ice-fish/articles/65070.html</guid><wfw:comment>http://www.cppblog.com/ice-fish/comments/65070.html</wfw:comment><comments>http://www.cppblog.com/ice-fish/articles/65070.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ice-fish/comments/commentRss/65070.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ice-fish/services/trackbacks/65070.html</trackback:ping><description><![CDATA[<blockquote dir=ltr style="MARGIN-RIGHT: 0px">
<p>迭代公式具体为Xn+1=(Xn+a/xn)/2&nbsp; (n=0,1,2,3&#8230;.;X0=a/2)，这是我的一次作业。<br><br></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">#include&lt;iostream.h&gt;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">#include&lt;math.h&gt;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">void main()<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">{<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">float a,m,n;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">cout&lt;&lt;"please input a positive integer"&lt;&lt;endl;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">cout&lt;&lt;"a="&lt;&lt;endl;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">cin&gt;&gt;a;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">n=a/2;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">m=(n+a/n)/2;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">while(fabs(n-m)&gt;1e-6)<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">{n=m;<o:p></o:p></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US style="FONT-SIZE: 14pt">m=(n+a/n)/2;<o:p></o:p></span></p>
<p class=MsoNormal dir=ltr style="MARGIN: 0cm 0cm 0pt">}<br>cout&lt;&lt;"The square root of a is"&lt;&lt;m&lt;&lt;endl;<br>}<o:p></o:p></p>
<p>&nbsp;</p>
<p>呵呵，关于牛顿迭代法，众人研究甚多。<br>TIP<sub>1</sub>:<br>牛顿迭代法（Newton's method）又称为牛顿-拉夫逊方法（Newton-Raphson method），它是牛顿在17世纪提出的一种在实数域和复数域上近似求解方程的方法。多数方程不存在求根公式，因此求精确根非常困难，甚至不可能，从而寻找方程的近似根就显得特别重要。方法使用函数f(x)的泰勒级数的前面几项来寻找方程f(x) = 0的根。牛顿迭代法是求方程根的重要方法之一，其最大优点是在方程f(x) = 0的单根附近具有平方收敛，而且该法还可以用来求方程的重根、复根。 <br><br>设r是f(x) = 0的根，选取x0作为r初始近似值，过点（x0,f(x0)）做曲线y = f(x)的切线L，L的方程为y = f(x0)+f'(x0)(x-x0)，求出L与x轴交点的横坐标 x1 = x0-f(x0)/f'(x0)，称x1为r的一次近似值。过点（x1,f(x1)）做曲线y = f(x)的切线，并求该切线与x轴的横坐标 x2 = x1-f(x1)/f'(x1)，称x2为r的二次近似值。重复以上过程，得r的近似值序列，其中x(n+1)=x(n)－f(x(n))/f'(x(n))，称为r的n+1次近似值，上式称为牛顿迭代公式。 <br><br>解非线性方程f(x)=0的牛顿法是把非线性方程线性化的一种近似方法。把f(x)在x0点附近展开成泰勒级数 f(x) = f(x0)+(x－x0)f'(x0)+(x－x0)^2*f''(x0)/2! +&#8230; 取其线性部分，作为非线性方程f(x) = 0的近似方程，即泰勒展开的前两项，则有f(x0)+f'(x0)(x－x0)=f(x)=0 设f'(x0)&#8800;0则其解为x1=x0－f(x0)/f'(x0) 这样，得到牛顿法的一个迭代序列：x(n+1)=x(n)－f(x(n))/f'(x(n))。</cn> <br><br></p>
<p>TIP<sub>2</sub>: </p>
<p>相关：迭代算法是用计算机解决问题的一种基本方法。它利用计算机运算速度快、适合做重复性操作的特点，让计算机对一组指令（或一定步骤）进行重复执行，在每次执行这组指令（或这些步骤）时，都从变量的原值推出它的一个新值。<br>例如，当我们要解决一个相关递增问题时，利用计算机的优势可以设计出相关的程序来快速求得解。<br>如题， 一个饲养场引进一只刚出生的新品种兔子，这种兔子从出生的下一个月开始，每月新生一只兔子，新生的兔子也如此繁殖。如果所有的兔子都不死去，问到第 12 个月时，该饲养场共有兔子多少只？ <br>呵呵，又是兔子问题，这是个思路比较清楚，解法比较简单的一道题！<br>这是一个典型的递推问题。我们不妨假设第 1 个月时兔子的只数为 u1 ，第2个月时兔子的只数为u2,第3个月时兔子的只数为u3，&#8230;&#8230;根据题意，&#8220;这种兔子从出生的下一个月开始，每月新生一只兔子&#8221;，则有 <br>U1＝1,U 2＝U1＋U1&#215;1＝2,U3＝U2+U2&#215;1＝4,&#8230;&#8230; <br>根据这个规律，可以归纳出下面的递推公式：&nbsp;<br>U n ＝&nbsp;U (n-1)&#215; 2 (n &#8805; 2) ,对应 Un 和&nbsp;U n － 1 ，定义两个迭代变量 y 和 x ，可将上面的递推公式转换成如下迭代关系： <br>y=x*2 <br>x=y <br>让计算机对这个迭代关系重复执行 11 次，就可以算出第 12 个月时的兔子数。</p>
</blockquote>
<img src ="http://www.cppblog.com/ice-fish/aggbug/65070.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ice-fish/" target="_blank">ice-fish</a> 2008-10-26 11:34 <a href="http://www.cppblog.com/ice-fish/articles/65070.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>