﻿<?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++博客-编程每日C-文章分类-编程 C C++</title><link>http://www.cppblog.com/hijay/category/5536.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 29 May 2008 05:58:23 GMT</lastBuildDate><pubDate>Thu, 29 May 2008 05:58:23 GMT</pubDate><ttl>60</ttl><item><title>一道N取到超级无穷大的超级复杂C 程序的超级优解法</title><link>http://www.cppblog.com/hijay/articles/36410.html</link><dc:creator>很笨的蛋</dc:creator><author>很笨的蛋</author><pubDate>Sun, 11 Nov 2007 14:15:00 GMT</pubDate><guid>http://www.cppblog.com/hijay/articles/36410.html</guid><wfw:comment>http://www.cppblog.com/hijay/comments/36410.html</wfw:comment><comments>http://www.cppblog.com/hijay/articles/36410.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/hijay/comments/commentRss/36410.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/hijay/services/trackbacks/36410.html</trackback:ping><description><![CDATA[<p><strong>写一个函数计算当参数为n(n很大)时的值 1-2+3-4+5-6+7......+n<br></strong></p>
<p><strong>方法一:依次循环</strong></p>
<p><strong><font color=#009999 size=4>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br>long fn(long n);<br>int main(void)<br>{&nbsp;&nbsp; long n=0;<br>&nbsp;&nbsp;&nbsp; printf("input n\n");<br>&nbsp;scanf("%ld",&amp;n);<br>printf("result=:%ld",fn(n));<br>system("pause");<br>&nbsp;return 0;<br>}<br>long fn(long n)<br>{<br>&nbsp;&nbsp;&nbsp; long temp=0;<br>&nbsp;&nbsp;&nbsp; int i,flag=1;//用FLAG控制正负号的交替 ,I 从1循环到N <br>&nbsp;&nbsp;&nbsp; if(n&lt;=0)<br>&nbsp;&nbsp;&nbsp; printf("error: n must &gt; 0");<br>&nbsp;&nbsp; <font color=#ff00ff>for(i=1;i&lt;=n;i++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp=temp+flag*i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flag=(-1)*flag;}<br></font>&nbsp;&nbsp;&nbsp;&nbsp; return temp;<br>}</font></strong></p>
<p><strong></strong>&nbsp;</p>
<p><strong>方法二:比起上一个程序，第二个涉及到乘法指令的语句改为执行加法指令，既达到要题目的要求而且运算时间上缩短了很多！而代价仅仅是增加了一个整型变量！</strong></p>
<p><strong>实现全码:</strong></p>
<p><strong><font color=#009999 size=4>long fn(long n)</font></strong></p>
<p><strong><font color=#009999 size=4>　　{</font></strong></p>
<p><strong><font color=#009999 size=4>　　long temp=0;</font></strong></p>
<p><strong><font color=#009999 size=4>　　int j=1,i=1,flag=1;</font></strong></p>
<p><strong><font color=#009999 size=4>　　if(n&lt;=0)</font></strong></p>
<p><strong><font color=#009999 size=4>　　{</font></strong></p>
<p><strong><font color=#009999 size=4>　　printf("error: n must &gt; 0);</font></strong></p>
<p><strong><font color=#009999 size=4>　　exit(1);</font></strong></p>
<p><strong><font color=#009999 size=4>　　}</font></strong></p>
<p><strong><font color=#009999 size=4>　　<font color=#ff00ff>while(j&lt;=n)</font></font></strong></p>
<p><strong><font color=#ff00ff size=4>　　{</font></strong></p>
<p><strong><font color=#ff00ff size=4>　　temp=temp+i;</font></strong></p>
<p><strong><font color=#ff00ff size=4>　　i=-i;</font></strong></p>
<p><strong><font color=#ff00ff size=4>　　i&gt;0?i++:i--;</font></strong></p>
<p><strong><font color=#ff00ff size=4>　　j++;</font></strong></p>
<p><strong><font color=#ff00ff size=4>　　}</font></strong></p>
<p><strong><font color=#009999 size=4>　　return temp;</font></strong></p>
<p><strong><font size=4><font color=#009999>　　}</font><br></font></strong></p>
<p><strong></strong>&nbsp;</p>
<p><strong>方法三:直接跳过循环!!!整体上看,考虑到1-2+3-4+.....n的规律..&nbsp;其值等于中间数..</strong></p>
<p><strong>实现代码如下:</strong></p>
<p><strong><font color=#009999 size=4>long fn(long n)</font></strong></p>
<p><strong><font color=#009999 size=4>{</font></strong></p>
<p><strong><font color=#009999 size=4>　　if(n&lt;=0)</font></strong></p>
<p><strong><font color=#009999 size=4>　　{</font></strong></p>
<p><strong><font color=#009999 size=4>　　printf("error: n must &gt; 0");</font></strong></p>
<p><strong><font color=#009999 size=4>　　exit(1);</font></strong></p>
<p><strong><font color=#009999 size=4>　　}</font></strong></p>
<p><strong><font color=#009999 size=4>　　<font color=#ff00ff>if(0==n%2)</font></font></strong></p>
<p><strong><font color=#ff00ff size=4>　　return (n/2)*(-1);</font></strong></p>
<p><strong><font color=#ff00ff size=4>　　else</font></strong></p>
<p><strong><font color=#ff00ff size=4>　　return (n/2)*(-1)+n;</font></strong></p>
<p><strong><font color=#009999 size=4>} </font></strong></p>
<br><br><strong><font face=幼圆 color=#ff0000 size=4>是很强悍吧?我说过了嘛,是越来越优噻~</font></strong>
<img src ="http://www.cppblog.com/hijay/aggbug/36410.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/hijay/" target="_blank">很笨的蛋</a> 2007-11-11 22:15 <a href="http://www.cppblog.com/hijay/articles/36410.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>