﻿<?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</title><link>http://www.cppblog.com/zhenglinbo/category/17752.html</link><description>享受编程的乐趣。</description><language>zh-cn</language><lastBuildDate>Fri, 27 Sep 2013 12:21:58 GMT</lastBuildDate><pubDate>Fri, 27 Sep 2013 12:21:58 GMT</pubDate><ttl>60</ttl><item><title>栈和堆（转）</title><link>http://www.cppblog.com/zhenglinbo/archive/2013/09/27/203451.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Fri, 27 Sep 2013 01:13:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2013/09/27/203451.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/203451.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2013/09/27/203451.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/203451.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/203451.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: 'courier new', courier; line-height: 25px; background-color: #ffffff;">摘抄自：</span><span style="color: #333333; font-family: 'courier new', courier; line-height: 25px; background-color: #ffffff;">http://www.cnblogs.com/vamei&nbsp;</span><br /><span style="color: #333333; font-family: 'courier new', courier; line-height: 25px; background-color: #ffffff;"><br />当程序文件运行为进程的时候，进程在内存中得到空间(进程自己的小房间)。每个进程空间按照如下方式分为不同区域:<br /></span><img src="http://www.cppblog.com/images/cppblog_com/zhenglinbo/5.jpg" width="402" height="878" alt="" /><br /><span style="color: #333333; font-family: 'courier new', courier; line-height: 25px; text-align: center; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 内存空间<br /><br /></span><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: 'courier new', courier;">Text区域用来储存指令(instruction)，来告诉程序每一步的操作。Global Data用于存放全局变量，stack用于存放局部变量，heap用于存放动态变量 (dynamic variable. 程序利用malloc系统调用，直接从内存中为dynamic variable开辟空间)。<span style="margin: 0px; padding: 0px; color: #ff0000;">Text</span>和<span style="margin: 0px; padding: 0px; color: #ff0000;">Global data</span>在进程一开始的时候就确定了，并在整个进程中保持<span style="margin: 0px; padding: 0px; color: #ff0000;">固定大小</span>。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: 'courier new', courier;">&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: 'courier new', courier;"><span style="margin: 0px; padding: 0px; color: #ff0000;">Stack<span style="margin: 0px; padding: 0px; color: #000000;">(</span>栈<span style="margin: 0px; padding: 0px; color: #000000;">)</span></span>以<span style="margin: 0px; padding: 0px; color: #ff0000;">stack frame</span>为单位。当程序调用函数的时候，比如main()函数中调用inner()函数，stack会向下增长一个stack frame。Stack frame中存储该函数的<span style="margin: 0px; padding: 0px; color: #ff0000;">参数</span>和<span style="margin: 0px; padding: 0px; color: #ff0000;">局部变量</span>，以及该函数的<span style="margin: 0px; padding: 0px; color: #ff0000;">返回地址<span style="margin: 0px; padding: 0px; color: #000000;">(return address)</span></span>。此时，计算机将控制权从main()转移到inner()，inner()函数处于<span style="margin: 0px; padding: 0px; color: #ff0000;">激活</span>(active)状态。位于Stack最下方的frame和Global Data就构成了当前的环境(context)。激活函数可以从中调用需要的变量。典型的编程语言都只允许你使用位于stack最下方的frame ，而不允许你调用其它的frame (这也符合stack结构&#8220;先进后出&#8221;的特征。但也有一些语言允许你调用stack的其它部分，相当于允许你在运行inner()函数的时候调用main()中声明的局部变量，比如Pascal)。当函数又进一步调用另一个函数的时候，一个新的frame会继续增加到stack下方，控制权转移到新的函数中。当激活函数<span style="margin: 0px; padding: 0px; color: #ff0000;">返回</span>的时候，会从stack中<span style="margin: 0px; padding: 0px; color: #ff0000;">弹出</span>(<span style="margin: 0px; padding: 0px; color: #ff0000;">pop</span>，就是读取并删除)该frame，并根据frame中记录的返回地址，将控制权交给返回地址<span style="margin: 0px; padding: 0px; color: #ff0000;">所指向的指令</span>(比如从inner()函数中返回，继续执行main()中赋值给main2的操作)。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: 'courier new', courier;">下图是stack在运行过程中的变化，箭头表示stack增长的方向，每个方块代表一个stack frame。开始的时候我们有一个为main()服务的frame，随着调用inner()，我们为inner()增加一个frame。在inner()返回时，我们再次只有main()的frame，直到最后main()返回，其返回地址为空，所以进程结束。<br /><br /></span></p><span style="color: #333333; font-family: 'courier new', courier; line-height: 25px; text-align: center; background-color: #ffffff;"><img src="http://www.cppblog.com/images/cppblog_com/zhenglinbo/6.jpg" border="0" alt="" width="808" height="338" /><br /></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: #333333; font-family: 'courier new', courier; line-height: 25px; text-align: center; background-color: #ffffff;">stack变化<br /><br /><br /></span><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: 'courier new', courier;">在进程运行的过程中，通过调用和返回函数，<span style="margin: 0px; padding: 0px; color: #ff0000;">控制权</span>不断在函数间转移。进程可以在调用函数的时候，原函数的stack frame中保存有在我们离开时的状态，并为新的函数开辟所需的stack frame空间。在调用函数返回时，该函数的stack frame所占据的空间随着stack frame的弹出而清空。进程再次回到原函数的stack frame中保存的状态，并根据返回地址所指向的指令继续执行。上面过程不断继续，stack不断增长或减小，直到main()返回的时候，stack完全清空，进程结束。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: 'courier new', courier;">&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: 'courier new', courier;">&nbsp;当程序中使用malloc的时候，<span style="margin: 0px; padding: 0px; color: #ff0000;">heap<span style="margin: 0px; padding: 0px; color: #000000;">(</span>堆<span style="margin: 0px; padding: 0px; color: #000000;">)</span></span><span style="margin: 0px; padding: 0px; color: #000000;">会</span>向上增长，其增长的部分就成为malloc从内存中分配的空间。malloc开辟的空间会一直存在，直到我们用free系统调用来释放，或者进程结束。一个经典的错误是<span style="margin: 0px; padding: 0px; color: #ff0000;">内存泄漏</span>(<span style="margin: 0px; padding: 0px; color: #ff0000;">memory leakage</span>), 就是指我们没有释放不再使用的heap空间，导致heap不断增长，而内存可用空间不断减少。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-family: 'courier new', courier;">由于stack和heap的大小则会随着进程的运行增大或者变小，当stack和heap增长到两者相遇时候，也就是内存空间图中的<span style="margin: 0px; padding: 0px; color: #ff0000;">蓝色区域</span>(unused area)完全消失的时候，进程会出现<span style="margin: 0px; padding: 0px; color: #ff0000;">栈溢出</span>(<span style="margin: 0px; padding: 0px; color: #ff0000;">stack overflow</span>)的错误，导致进程终止。在现代计算机中，内核一般都会为进程分配足够多的蓝色区域，如果我们即时清理的话，stack overflow是可以避免的。但是，在进行一些矩阵运算的时候，由于所需的内存很大，依然可能出现stack overflow的情况。一种解决方式是增大内核分配给每个进程的内存空间。如果依然不能解决问题的话，我们就需要增加物理内存。<br /><br /></span></p><span style="color: #333333; font-family: 'courier new', courier; line-height: 25px; text-align: center; background-color: #ffffff;"><br />堆和栈的区别：<br /><br /><br /></span><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">2.1申请方式&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; stack:&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 由系统自动分配。&nbsp;&nbsp; 例如，声明在函数中一个局部变量&nbsp;&nbsp; int&nbsp;&nbsp; b;&nbsp;&nbsp; 系统自动在栈中为b开辟空&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 间&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; heap:&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 需要程序员自己申请，并指明大小，在c中malloc函数&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 如p1&nbsp;&nbsp; =&nbsp;&nbsp; (char&nbsp;&nbsp; *)malloc(10);&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 在C++中用new运算符&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 如p2&nbsp;&nbsp; =&nbsp;&nbsp; new&nbsp;&nbsp; char[10];&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 但是注意p1、p2本身是在栈中的。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 2.2&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 申请后系统的响应&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 栈：只要栈的剩余空间大于所申请空间，系统将为程序提供内存，否则将报异常提示栈溢&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 出。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 堆：首先应该知道操作系统有一个记录空闲内存地址的链表，当系统收到程序的申请时，&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 会遍历该链表，寻找第一个空间大于所申请空间的堆结点，然后将该结点从空闲结点链表&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 中删除，并将该结点的空间分配给程序，另外，对于大多数系统，会在这块内存空间中的&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 首地址处记录本次分配的大小，这样，代码中的delete语句才能正确的释放本内存空间。&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 另外，由于找到的堆结点的大小不一定正好等于申请的大小，系统会自动的将多余的那部&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 分重新放入空闲链表中。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 2.3申请大小的限制&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 栈：在Windows下,栈是向低地址扩展的数据结构，是一块连续的内存的区域。这句话的意&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 思是栈顶的地址和栈的最大容量是系统预先规定好的，在WINDOWS下，栈的大小是2M（也有&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 的说是1M，总之是一个编译时就确定的常数），如果申请的空间超过栈的剩余空间时，将&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 提示overflow。因此，能从栈获得的空间较小。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 堆：堆是向高地址扩展的数据结构，是不连续的内存区域。这是由于系统是用链表来存储&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 的空闲内存地址的，自然是不连续的，而链表的遍历方向是由低地址向高地址。堆的大小&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 受限于计算机系统中有效的虚拟内存。由此可见，堆获得的空间比较灵活，也比较大。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 2.4申请效率的比较：&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 栈由系统自动分配，速度较快。但程序员是无法控制的。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 堆是由new分配的内存，一般速度比较慢，而且容易产生内存碎片,不过用起来最方便.&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 另外，在WINDOWS下，最好的方式是用VirtualAlloc分配内存，他不是在堆，也不是在栈是&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 直接在进程的地址空间中保留一块内存，虽然用起来最不方便。但是速度快，也最灵活。&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 2.5堆和栈中的存储内容&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 栈：&nbsp;&nbsp; 在函数调用时，第一个进栈的是主函数中后的下一条指令（函数调用语句的下一条可&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 执行语句）的地址，然后是函数的各个参数，在大多数的C编译器中，参数是由右往左入栈&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 的，然后是函数中的局部变量。注意静态变量是不入栈的。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 当本次函数调用结束后，局部变量先出栈，然后是参数，最后栈顶指针指向最开始存的地&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 址，也就是主函数中的下一条指令，程序由该点继续运行。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 堆：一般是在堆的头部用一个字节存放堆的大小。堆中的具体内容由程序员安排。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 2.6存取效率的比较&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; char&nbsp;&nbsp; s1[]&nbsp;&nbsp; =&nbsp;&nbsp; "aaaaaaaaaaaaaaa";&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; char&nbsp;&nbsp; *s2&nbsp;&nbsp; =&nbsp;&nbsp; "bbbbbbbbbbbbbbbbb";&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; aaaaaaaaaaa是在运行时刻赋值的；&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 而bbbbbbbbbbb是在编译时就确定的；&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 但是，在以后的存取中，在栈上的数组比指针所指向的字符串(例如堆)快。 &nbsp;&nbsp;<br /><br /></span><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 2.7小结：&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 堆和栈的区别可以用如下的比喻来看出：&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 使用栈就象我们去饭馆里吃饭，只管点菜（发出申请）、付钱、和吃（使用），吃饱了就&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 走，不必理会切菜、洗菜等准备工作和洗碗、刷锅等扫尾工作，他的好处是快捷，但是自&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 由度小。&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 使用堆就象是自己动手做喜欢吃的菜肴，比较麻烦，但是比较符合自己的口味，而且自由&nbsp;&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp; 度大。&nbsp;&nbsp; (经典！) &nbsp;</span><br /><img src ="http://www.cppblog.com/zhenglinbo/aggbug/203451.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2013-09-27 09:13 <a href="http://www.cppblog.com/zhenglinbo/archive/2013/09/27/203451.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> printf("%.*s\n"，int,str) 的含义</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/12/05/196021.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Wed, 05 Dec 2012 14:10:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/12/05/196021.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/196021.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/12/05/196021.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/196021.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/196021.html</trackback:ping><description><![CDATA[<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 />-->转。<br /><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;%.*s&nbsp;其中的.*表示显示的精度&nbsp;对字符串输出(s)类型来说就是宽度<br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;这个*代表的值由后面的参数列表中的整数型(int)值给出<br /><br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;例如：</span><span style="color: #008000; "><br /></span>printf("%.*s\n",&nbsp;1,&nbsp;"abc");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;输出a</span><span style="color: #008000; "><br /></span>printf("%.*s\n",&nbsp;2,&nbsp;"abc");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;输出ab</span><span style="color: #008000; "><br /></span>printf("%.*s\n",&nbsp;3,&nbsp;"abc");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;输出abc&nbsp;&gt;3是一样的效果&nbsp;因为输出类型type&nbsp;=&nbsp;s，遇到'\0'会结束<br /></span></div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/196021.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-12-05 22:10 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/12/05/196021.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>十进制转二进制（代码）</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/09/08/189923.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sat, 08 Sep 2012 04:41:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/09/08/189923.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/189923.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/09/08/189923.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/189923.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/189923.html</trackback:ping><description><![CDATA[如下：<br /><span style="background-color: #eeeeee; font-size: 13px; ">#include</span><span style="background-color: #eeeeee; font-size: 13px; ">&lt;</span><span style="background-color: #eeeeee; font-size: 13px; ">stdio.h</span><span style="background-color: #eeeeee; font-size: 13px; ">&gt;</span><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"><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;M,b;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;N[100],c=0;<br />&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d%d",&amp;b,&amp;M);<span style="color: #008000; ">//</span><span style="color: #008000; ">输入进制b(2~10),要转化为b进制的正整数M(十进制)</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(M)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N[c++]=M%b;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;M=M/b;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;i=c-1;i&gt;=0;i--)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d",N[i]);<br />&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}</div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/189923.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-09-08 12:41 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/09/08/189923.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>二进制转十进制（代码）</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/09/08/189922.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sat, 08 Sep 2012 04:37:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/09/08/189922.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/189922.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/09/08/189922.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/189922.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/189922.html</trackback:ping><description><![CDATA[如下：<br /><span style="background-color: #eeeeee; font-size: 13px; ">#include</span><span style="background-color: #eeeeee; font-size: 13px; ">&lt;</span><span style="background-color: #eeeeee; font-size: 13px; ">stdio.h</span><span style="background-color: #eeeeee; font-size: 13px; ">&gt;</span><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">#include&lt;<span style="color: #0000FF; ">string</span>.h&gt;<br />#include&lt;math.h&gt;<br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>&nbsp;M[100]={0};<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;N=0;<br />&nbsp;&nbsp;&nbsp;&nbsp;gets(M);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;len=strlen(M);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;i=0;i&lt;len;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N+=(M[i]-'0')&nbsp;*&nbsp;pow(2.0,len-i-1);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n",N);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}</div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/189922.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-09-08 12:37 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/09/08/189922.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>填充二维数组</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187679.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sun, 19 Aug 2012 08:22:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187679.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/187679.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187679.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/187679.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/187679.html</trackback:ping><description><![CDATA[编写一程序，用0或1填充一个二维数组，如果i 和j 的最大公因子为1，则设a[i][j]为1；否则设为0。<br /><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"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include&lt;stdio.h&gt;<br /><span style="color: #0000FF; ">#define</span>&nbsp;N&nbsp;10<br /><span style="color: #0000FF; ">int</span>&nbsp;Maxcom(<span style="color: #0000FF; ">int</span>&nbsp;a,&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;b)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(a!=b)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(a&gt;b)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a=a-b;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #0000FF; ">if</span>(b&gt;a)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b=b-a;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;a;<br />}<br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;a[N][N];<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;i,j,max;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(i=0;i&lt;5;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(j=0;j&lt;5;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(i==1&nbsp;&amp;&amp;&nbsp;j==1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i][j]=1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #0000FF; ">if</span>(&nbsp;i&gt;0&nbsp;&amp;&amp;&nbsp;j&gt;0)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max&nbsp;=&nbsp;Maxcom(i,j);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(max&nbsp;==&nbsp;1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i][j]=1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i][j]=0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i][j]=0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(i=0;i&lt;5;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(j=0;j&lt;5;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d&nbsp;",a[i][j]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}</div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/187679.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-08-19 16:22 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187679.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于二维数组</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187675.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sun, 19 Aug 2012 07:40:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187675.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/187675.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187675.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/187675.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/187675.html</trackback:ping><description><![CDATA[二维数组的乘积计算：<br /><br /><div style="padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; background-color: #eeeeee; font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; width: 1081px; word-break: break-all; "><span style="color: #0000ff; ">for</span>(i=0;i&lt;N;i++)<br />&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(j=0;j&lt;N;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(k=0,c[i][j]&nbsp;=&nbsp;0.0;k&lt;N;k++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[i][j]&nbsp;+=a[i][k]&nbsp;*&nbsp;b[k][j];<br /><br /><br /></div><br />二维数组的分配：<br /><br /><div style="padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; background-color: #eeeeee; font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; width: 1081px; word-break: break-all; ">#include&lt;stdio.h&gt;<br />#include&lt;stdlib.h&gt;<br /><br /><span style="color: #0000ff; ">int</span>&nbsp;**malloc2d(<span style="color: #0000ff; ">int</span>&nbsp;r,&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;c)&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">二维数组分配函数</span><span style="color: #008000; "><br /></span>{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;i;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;**t&nbsp;=&nbsp;(<span style="color: #0000ff; ">int</span>&nbsp;**)malloc(r&nbsp;*&nbsp;<span style="color: #0000ff; ">sizeof</span>(<span style="color: #0000ff; ">int</span>&nbsp;*));<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(i=0;i&lt;r;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t[i]&nbsp;=&nbsp;(<span style="color: #0000ff; ">int</span>&nbsp;*)malloc(c&nbsp;*&nbsp;<span style="color: #0000ff; ">sizeof</span>(<span style="color: #0000ff; ">int</span>));<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">return</span>&nbsp;t;<br />}<br /><br /><span style="color: #0000ff; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;i,j;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;**a&nbsp;=malloc2d(3,3);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(i=0;i&lt;3;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(j=0;j&lt;3;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i][j]=i+j;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(i=0;i&lt;3;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(j=0;j&lt;3;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d&nbsp;",a[i][j]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">return</span>&nbsp;0;<br />}</div><br />综合计算二维数组相乘的乘积。<br />代码如下：<br /><br /><div style="padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; background-color: #eeeeee; font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; width: 1081px; word-break: break-all; ">#include&lt;stdio.h&gt;<br />#include&lt;stdlib.h&gt;<br /><br /><span style="color: #0000ff; ">int</span>&nbsp;**malloc2d(<span style="color: #0000ff; ">int</span>&nbsp;r,&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;c)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;i;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;**t&nbsp;=&nbsp;(<span style="color: #0000ff; ">int</span>&nbsp;**)malloc(r&nbsp;*&nbsp;<span style="color: #0000ff; ">sizeof</span>(<span style="color: #0000ff; ">int</span>&nbsp;*));<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(i=0;i&lt;r;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t[i]&nbsp;=&nbsp;(<span style="color: #0000ff; ">int</span>&nbsp;*)malloc(c&nbsp;*&nbsp;<span style="color: #0000ff; ">sizeof</span>(<span style="color: #0000ff; ">int</span>));<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">return</span>&nbsp;t;<br />}<br /><br /><span style="color: #0000ff; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;i,j,k;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;**a&nbsp;=malloc2d(3,3);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;**b&nbsp;=malloc2d(3,3);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">int</span>&nbsp;**c&nbsp;=malloc2d(3,3);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(i=0;i&lt;3;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(j=0;j&lt;3;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i][j]=i+j;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b[i][j]=i+j;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(i=0;i&lt;3;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(j=0;j&lt;3;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d&nbsp;",a[i][j]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(i=0;i&lt;3;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(j=0;j&lt;3;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(k=0,c[i][j]&nbsp;=&nbsp;0.0;k&lt;3;k++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[i][j]&nbsp;+=a[i][k]&nbsp;*&nbsp;b[k][j];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(i=0;i&lt;3;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">for</span>(j=0;j&lt;3;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d&nbsp;",c[i][j]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff; ">return</span>&nbsp;0;<br />}<br />&nbsp; &nbsp;&nbsp;</div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/187675.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-08-19 15:40 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187675.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>库函数qsort()的用法</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187656.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sun, 19 Aug 2012 03:11:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187656.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/187656.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187656.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/187656.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/187656.html</trackback:ping><description><![CDATA[<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: #008000; ">//</span><span style="color: #008000; ">整数排序，利用qsort函数</span><span style="color: #008000; "><br /></span>#include&lt;stdio.h&gt;<br />#include&lt;stdlib.h&gt;<br /><span style="color: #0000FF; ">int</span>&nbsp;compare(<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;*&nbsp;a,<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;*&nbsp;b)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;*(<span style="color: #0000FF; ">int</span>*)a&nbsp;-&nbsp;*(<span style="color: #0000FF; ">int</span>*)b;<br />}<br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;a[20],i;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(i=0;i&lt;10;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&amp;a[i]);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;qsort(a,10,<span style="color: #0000FF; ">sizeof</span>(<span style="color: #0000FF; ">int</span>),compare);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(i=0;i&lt;10;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d&nbsp;",a[i]);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}</div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/187656.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-08-19 11:11 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187656.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串排序</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187655.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sun, 19 Aug 2012 02:49:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187655.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/187655.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187655.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/187655.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/187655.html</trackback:ping><description><![CDATA[<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: #008000; ">//</span><span style="color: #008000; ">字符串排序，利用qsort函数</span><span style="color: #008000; "><br /></span>#include&lt;stdio.h&gt;<br />#include&lt;<span style="color: #0000FF; ">string</span>.h&gt;<br />#include&lt;stdlib.h&gt;<br /><span style="color: #0000FF; ">#define</span>&nbsp;Nmax&nbsp;1000<br /><span style="color: #0000FF; ">#define</span>&nbsp;Mmax&nbsp;10000<br /><span style="color: #0000FF; ">char</span>&nbsp;buf[Mmax];&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;M=0;<br /><span style="color: #0000FF; ">int</span>&nbsp;compare(<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;*i,&nbsp;<span style="color: #0000FF; ">const</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;*j)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;strcmp(*(<span style="color: #0000FF; ">char</span>&nbsp;**)i,&nbsp;*(<span style="color: #0000FF; ">char</span>&nbsp;**)j);<br />}<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;i,&nbsp;N;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">char</span>&nbsp;*a[Nmax];<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(N=0;&nbsp;N&lt;Nmax;&nbsp;N++)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[N]=&nbsp;&amp;buf[M];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(scanf("%s",&nbsp;a[N])&nbsp;==&nbsp;EOF)&nbsp;<span style="color: #0000FF; ">break</span>;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;M+=strlen(a[N])+1;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;qsort(a,N,<span style="color: #0000FF; ">sizeof</span>(<span style="color: #0000FF; ">char</span>*),compare);&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">四个参数分别代表：待排序的数组首地址，数组中待排序的元素数量，各元素占用的空间，排序函数（确定排序顺序）</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(i=0;i&lt;N;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s\n",a[i]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}</div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/187655.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-08-19 10:49 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/08/19/187655.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>文件的读写</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/03/12/167721.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Mon, 12 Mar 2012 08:53:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/03/12/167721.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/167721.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/03/12/167721.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/167721.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/167721.html</trackback:ping><description><![CDATA[创建一个后缀名为txt的文件，并向该文件写入一个字符串，保存起来，再打开文件，读出文件中的内容。<br />代码如下：
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">string</span><span style="color: #000000">.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_48_544_Open_Image" onclick="this.style.display='none'; Codehighlighter1_48_544_Open_Text.style.display='none'; Codehighlighter1_48_544_Closed_Image.style.display='inline'; Codehighlighter1_48_544_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_48_544_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_48_544_Closed_Text.style.display='none'; Codehighlighter1_48_544_Open_Image.style.display='inline'; Codehighlighter1_48_544_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_48_544_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_48_544_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">fp;<br /><img id="Codehighlighter1_90_96_Open_Image" onclick="this.style.display='none'; Codehighlighter1_90_96_Open_Text.style.display='none'; Codehighlighter1_90_96_Closed_Image.style.display='inline'; Codehighlighter1_90_96_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_90_96_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_90_96_Closed_Text.style.display='none'; Codehighlighter1_90_96_Open_Image.style.display='inline'; Codehighlighter1_90_96_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;pathName[</span><span style="color: #000000">20</span><span style="color: #000000">],txt1[</span><span style="color: #000000">100</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_90_96_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_90_96_Open_Text"><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><span style="color: #000000">,txt2[</span><span style="color: #000000">20</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_107_113_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_107_113_Open_Text"><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><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;fileLen;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">Please&nbsp;type&nbsp;the&nbsp;path&nbsp;name&nbsp;of&nbsp;the&nbsp;file\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,pathName);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;fp</span><span style="color: #000000">=</span><span style="color: #000000">fopen(pathName,</span><span style="color: #000000">"</span><span style="color: #000000">w</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">Please&nbsp;input&nbsp;a&nbsp;string&nbsp;to&nbsp;this&nbsp;file\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,txt1);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;fileLen</span><span style="color: #000000">=</span><span style="color: #000000">strlen(txt1);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;fwrite(txt1,fileLen,</span><span style="color: #000000">1</span><span style="color: #000000">,fp);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;fclose(fp);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">The&nbsp;file&nbsp;has&nbsp;been&nbsp;saved\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">The&nbsp;content&nbsp;of&nbsp;teh&nbsp;file:&nbsp;%s&nbsp;is\n</span><span style="color: #000000">"</span><span style="color: #000000">,pathName);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;fp</span><span style="color: #000000">=</span><span style="color: #000000">fopen(pathName,</span><span style="color: #000000">"</span><span style="color: #000000">r</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;fread(txt2,fileLen,</span><span style="color: #000000">1</span><span style="color: #000000">,fp);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%s\n</span><span style="color: #000000">"</span><span style="color: #000000">,txt2);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div><br /><br /><img src ="http://www.cppblog.com/zhenglinbo/aggbug/167721.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-03-12 16:53 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/03/12/167721.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>贪心算法之最优装船问题</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/03/12/167713.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Mon, 12 Mar 2012 06:10:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/03/12/167713.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/167713.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/03/12/167713.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/167713.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/167713.html</trackback:ping><description><![CDATA[题目：<br />有一批集装箱要装入一个载质量为C的货船中，每个集装箱的质量由用户自己输入指定，在货船的装载体积不限的前提下，如何装载集装箱才能尽可能多地的将集装箱装入货船中。<br /><br />每次选取质量最小的箱子装入船（用冒泡排序算法解决），代码如下： 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdlib.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;sort(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;w[],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;t[],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n)</span><span style="color: #008000">//</span><span style="color: #008000">w[]存放每个集装箱的质量，t[]存放w[]的下标</span><span style="color: #008000"><br /><img id="Codehighlighter1_97_448_Open_Image" onclick="this.style.display='none'; Codehighlighter1_97_448_Open_Text.style.display='none'; Codehighlighter1_97_448_Closed_Image.style.display='inline'; Codehighlighter1_97_448_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_97_448_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_97_448_Closed_Text.style.display='none'; Codehighlighter1_97_448_Open_Image.style.display='inline'; Codehighlighter1_97_448_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_97_448_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_97_448_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,tmp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">w_tmp</span><span style="color: #000000">=</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">)malloc(</span><span style="color: #0000ff">sizeof</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: #000000">n);</span><span style="color: #008000">//</span><span style="color: #008000">开辟一个临时数组，存放w[]的内容，用于排序</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t[i]</span><span style="color: #000000">=</span><span style="color: #000000">i;</span><span style="color: #008000">//</span><span style="color: #008000">初始化数组t</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w_tmp[i]</span><span style="color: #000000">=</span><span style="color: #000000">w[i];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="color: #008000">//</span><span style="color: #008000">冒泡排序实现</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(w_tmp[j]</span><span style="color: #000000">&gt;</span><span style="color: #000000">w_tmp[j</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">])<br /><img id="Codehighlighter1_331_446_Open_Image" onclick="this.style.display='none'; Codehighlighter1_331_446_Open_Text.style.display='none'; Codehighlighter1_331_446_Closed_Image.style.display='inline'; Codehighlighter1_331_446_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_331_446_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_331_446_Closed_Text.style.display='none'; Codehighlighter1_331_446_Open_Image.style.display='inline'; Codehighlighter1_331_446_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_331_446_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_331_446_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000">=</span><span style="color: #000000">w_tmp[j];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w_tmp[j]</span><span style="color: #000000">=</span><span style="color: #000000">w_tmp[j</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w_tmp[j</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">tmp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000">=</span><span style="color: #000000">t[j];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t[j]</span><span style="color: #000000">=</span><span style="color: #000000">t[j</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t[j</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">tmp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;loading(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x[],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;w[],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;c,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n)<br /><img id="Codehighlighter1_493_706_Open_Image" onclick="this.style.display='none'; Codehighlighter1_493_706_Open_Text.style.display='none'; Codehighlighter1_493_706_Closed_Image.style.display='inline'; Codehighlighter1_493_706_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_493_706_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_493_706_Closed_Text.style.display='none'; Codehighlighter1_493_706_Open_Image.style.display='inline'; Codehighlighter1_493_706_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_493_706_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_493_706_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,s</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">t</span><span style="color: #000000">=</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">)malloc(</span><span style="color: #0000ff">sizeof</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: #000000">n);</span><span style="color: #008000">//</span><span style="color: #008000">动态开辟一个临时数组，存放w[]的下标</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;sort(w,t,n);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x[i]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img id="Codehighlighter1_638_704_Open_Image" onclick="this.style.display='none'; Codehighlighter1_638_704_Open_Text.style.display='none'; Codehighlighter1_638_704_Closed_Image.style.display='inline'; Codehighlighter1_638_704_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_638_704_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_638_704_Closed_Text.style.display='none'; Codehighlighter1_638_704_Open_Image.style.display='inline'; Codehighlighter1_638_704_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;w[t[i]]</span><span style="color: #000000">&lt;=</span><span style="color: #000000">c;i</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_638_704_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_638_704_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x[t[i]]</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">将第t[i]个集装箱装入货船中</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="color: #000000">=</span><span style="color: #000000">c</span><span style="color: #000000">-</span><span style="color: #000000">w[t[i]];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">变量c中存放货船的剩余载质量</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_720_1035_Open_Image" onclick="this.style.display='none'; Codehighlighter1_720_1035_Open_Text.style.display='none'; Codehighlighter1_720_1035_Closed_Image.style.display='inline'; Codehighlighter1_720_1035_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_720_1035_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_720_1035_Closed_Text.style.display='none'; Codehighlighter1_720_1035_Open_Image.style.display='inline'; Codehighlighter1_720_1035_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_720_1035_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_720_1035_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x[</span><span style="color: #000000">5</span><span style="color: #000000">],w[</span><span style="color: #000000">5</span><span style="color: #000000">],c,i;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">Iput&nbsp;the&nbsp;maximum&nbsp;loading&nbsp;of&nbsp;the&nbsp;sheep\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">c);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">Iput&nbsp;the&nbsp;weight&nbsp;of&nbsp;Five&nbsp;box\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">5</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">w[i]);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;loading(x,w,c,</span><span style="color: #000000">5</span><span style="color: #000000">);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">The&nbsp;following&nbsp;boxes&nbsp;will&nbsp;be&nbsp;loaded\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">5</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_982_1022_Open_Image" onclick="this.style.display='none'; Codehighlighter1_982_1022_Open_Text.style.display='none'; Codehighlighter1_982_1022_Closed_Image.style.display='inline'; Codehighlighter1_982_1022_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_982_1022_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_982_1022_Closed_Text.style.display='none'; Codehighlighter1_982_1022_Open_Image.style.display='inline'; Codehighlighter1_982_1022_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_982_1022_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_982_1022_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(x[i]</span><span style="color: #000000">==</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">BOX:%d&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,i);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span></div><br /><br /><br /><img src ="http://www.cppblog.com/zhenglinbo/aggbug/167713.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-03-12 14:10 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/03/12/167713.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串的删除</title><link>http://www.cppblog.com/zhenglinbo/archive/2012/02/01/164760.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Tue, 31 Jan 2012 17:13:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2012/02/01/164760.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/164760.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2012/02/01/164760.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/164760.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/164760.html</trackback:ping><description><![CDATA[<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; ">#include</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdio.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />#include</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">string</span><span style="color: #000000; ">.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;strins(</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;str1,</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;str2,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;pos)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;s_len;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;d_len;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i,j;<br />&nbsp;&nbsp;&nbsp;&nbsp;pos</span><span style="color: #000000; ">--</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;s_len</span><span style="color: #000000; ">=</span><span style="color: #000000; ">strlen(str1);<br />&nbsp;&nbsp;&nbsp;&nbsp;d_len</span><span style="color: #000000; ">=</span><span style="color: #000000; ">strlen(str2);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(i</span><span style="color: #000000; ">=</span><span style="color: #000000; ">s_len</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;i</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">pos;i</span><span style="color: #000000; ">--</span><span style="color: #000000; ">)&nbsp;</span><span style="color: #008000; ">/*</span><span style="color: #008000; ">空出str2的空间</span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str1[i</span><span style="color: #000000; ">+</span><span style="color: #000000; ">d_len]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">str1[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(j</span><span style="color: #000000; ">=</span><span style="color: #000000; ">pos;str2[j</span><span style="color: #000000; ">-</span><span style="color: #000000; ">pos]</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; ">;j</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)&nbsp;</span><span style="color: #008000; ">/*</span><span style="color: #008000; ">将字符串str2插入str1中的特定位置</span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str1[j]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">str2[j</span><span style="color: #000000; ">-</span><span style="color: #000000; ">pos];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;str1;<br />}<br /><br /><br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;string1[</span><span style="color: #000000; ">200</span><span style="color: #000000; ">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;string2[</span><span style="color: #000000; ">100</span><span style="color: #000000; ">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;pos;<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">请输入初始字符串:</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;gets(string1);<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">请输入插入字符串:</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;gets(string2);<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">请输入插入位置:</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">pos);<br />&nbsp;&nbsp;&nbsp;&nbsp;strins(string1,string2,pos);<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">插入后的字符串是%s\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,string1);<br />}<br /></span></div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/164760.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2012-02-01 01:13 <a href="http://www.cppblog.com/zhenglinbo/archive/2012/02/01/164760.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>递归函数的使用</title><link>http://www.cppblog.com/zhenglinbo/archive/2011/09/18/156128.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sun, 18 Sep 2011 13:03:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2011/09/18/156128.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/156128.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2011/09/18/156128.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/156128.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/156128.html</trackback:ping><description><![CDATA[要求：编写一个递归函数，将一个整数n转换为字符串，例如输入483，应输出字符串&#8220;483&#8221;。<br />设计思想：设置一个字符型数组str，用于存放转换后的字符串，因为递归函数中要反复使用它，所以将其设置为全局数组变量。要实现整数n转换成字符串的递归转换，首先要将n的个位数n%10转换为字符，插入到str字符串的最前面，然后将除个位后的高位部分n/10按照相同的方式来转换，直到要转换的数为0结束。
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />#include</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">string</span><span style="color: #000000">.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;IntToStr(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n);<br /></span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;str[</span><span style="color: #000000">80</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">void</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;num;<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">input&nbsp;an&nbsp;integer&nbsp;number:</span><span style="color: #000000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">num);<br />&nbsp;&nbsp;&nbsp;&nbsp;IntToStr(num);<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">The&nbsp;string&nbsp;is:%s\n</span><span style="color: #000000">"</span><span style="color: #000000">,str);<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;IntToStr(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(n</span><span style="color: #000000">==</span><span style="color: #000000">0</span><span style="color: #000000">)</span><span style="color: #008000">//</span><span style="color: #008000">整数为0时，递归结束</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">strlen(str)</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">将字符串整个往后移一个字符</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str[i</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">str[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;str[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">n</span><span style="color: #000000">%</span><span style="color: #000000">10</span><span style="color: #000000">+</span><span style="color: #000000">0x30</span><span style="color: #000000">;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">将该数的个位数转换成字符放在字符数组的第一个元素的位置，+0x30表示十六进制的0，其实可以不加</span><span style="color: #008000"><br /></span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;IntToStr(n</span><span style="color: #000000">/</span><span style="color: #000000">10</span><span style="color: #000000">);</span><span style="color: #008000">//</span><span style="color: #008000">将该数的商，即除个位外的其他数通过递归转换</span><span style="color: #008000"><br /></span><span style="color: #000000">}<br /><br /></span><span style="color: #008000">//</span><span style="color: #008000">递归过程：例如输入n=148,那么第一次递归时，str[0]存放的数是8,n/10=14，得到第二次递归的数是14；<br /></span><span style="color: #008000">//</span><span style="color: #008000">8原先存储在str[0]，但第二次递归向后移动一个字符，变成存在str[1]内，而4存在str[0]内；<br /></span><span style="color: #008000">//</span><span style="color: #008000">第三次递归时，同理可知8放在str[2],4放在str[1],而1放在str[0]，这样就将148转换为字符串。</span></div><br /><br /><img src ="http://www.cppblog.com/zhenglinbo/aggbug/156128.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2011-09-18 21:03 <a href="http://www.cppblog.com/zhenglinbo/archive/2011/09/18/156128.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>编写一递归函数求斐波纳契数列的前40项</title><link>http://www.cppblog.com/zhenglinbo/archive/2011/09/18/156118.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sun, 18 Sep 2011 07:27:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2011/09/18/156118.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/156118.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2011/09/18/156118.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/156118.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/156118.html</trackback:ping><description><![CDATA[注：斐波纳契数列的第一项和第二项的值都为1，以后各项的值为其前两项值之和。所以要计算第n项的值F(n)，可以列出递归式F(n)=F(n-1)+F(n-2)，当n=1或n=2时，其值为1；<br /><br />用C语言代码表示如下：<br /><br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; height: 450px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">long</span><span style="color: #000000">&nbsp;F(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n);<br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">40</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">F(%2d)=%-9ld</span><span style="color: #000000">"</span><span style="color: #000000">,i,F(i));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(i</span><span style="color: #000000">%</span><span style="color: #000000">4</span><span style="color: #000000">==</span><span style="color: #000000">0</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000ff">long</span><span style="color: #000000">&nbsp;F(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n)</span><span style="color: #008000">//</span><span style="color: #008000">求第n项的值</span><span style="color: #008000"><br /></span><span style="color: #000000">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(n</span><span style="color: #000000">&lt;=</span><span style="color: #000000">2</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">(F(n</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">F(n</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">));<br />}<br /><br /></span></div><br /><br /><img src ="http://www.cppblog.com/zhenglinbo/aggbug/156118.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2011-09-18 15:27 <a href="http://www.cppblog.com/zhenglinbo/archive/2011/09/18/156118.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>冒泡排序与选择排序学习总结</title><link>http://www.cppblog.com/zhenglinbo/archive/2011/09/17/156034.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Sat, 17 Sep 2011 08:53:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2011/09/17/156034.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/156034.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2011/09/17/156034.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/156034.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/156034.html</trackback:ping><description><![CDATA[<div>&nbsp;冒泡排序的基本概念：<br />依次比较相邻的两个数，将小数放在前面，大数放在后面。即在第一趟：首先比较第1个和第2个数，将小数放前，大数放后。然后比较第2个数和第3个数，将小数放前，大数放后，如此继续，直至比较最后两个数，将小数放前，大数放后。至此第一趟结束，将最大的数放到了最后。在第二趟：仍从第一对数开始比较（因为可能由于第2个数和第3个数的交换，使得第1个数不再小于第2个数），将小数放前，大数放后，一直比较到倒数第二个数（倒数第一的位置上已经是最大的），第二趟结束，在倒数第二的位置上得到一个新的最大数（其实在整个数列中是第二大的数）。如此下去，重复以上过程，直至最终完成排序。需要用二重循环排序。<br />Example: 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;<br /><img id="Codehighlighter1_30_490_Open_Image" onclick="this.style.display='none'; Codehighlighter1_30_490_Open_Text.style.display='none'; Codehighlighter1_30_490_Closed_Image.style.display='inline'; Codehighlighter1_30_490_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img style="display: none" id="Codehighlighter1_30_490_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_30_490_Closed_Text.style.display='none'; Codehighlighter1_30_490_Open_Image.style.display='inline'; Codehighlighter1_30_490_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_30_490_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_30_490_Open_Text"><span style="color: #000000">{&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,temp,tag;&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a[</span><span style="color: #000000">11</span><span style="color: #000000">];&nbsp; //数组第0位空出<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">=10</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;scanf&nbsp;(</span><span style="color: #000000">"</span><span style="color: #000000">%d,</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">a[i]);&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">10</span><span style="color: #000000">;j</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;<br /><img id="Codehighlighter1_142_420_Open_Image" onclick="this.style.display='none'; Codehighlighter1_142_420_Open_Text.style.display='none'; Codehighlighter1_142_420_Closed_Image.style.display='inline'; Codehighlighter1_142_420_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img style="display: none" id="Codehighlighter1_142_420_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_142_420_Closed_Text.style.display='none'; Codehighlighter1_142_420_Open_Image.style.display='inline'; Codehighlighter1_142_420_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_142_420_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_142_420_Open_Text"><span style="color: #000000">{&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tag</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">=10</span><span style="color: #000000">-</span><span style="color: #000000">j;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;<br /><img id="Codehighlighter1_189_319_Open_Image" onclick="this.style.display='none'; Codehighlighter1_189_319_Open_Text.style.display='none'; Codehighlighter1_189_319_Closed_Image.style.display='inline'; Codehighlighter1_189_319_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img style="display: none" id="Codehighlighter1_189_319_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_189_319_Closed_Text.style.display='none'; Codehighlighter1_189_319_Open_Image.style.display='inline'; Codehighlighter1_189_319_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_189_319_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_189_319_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(a[i]</span><span style="color: #000000">&gt;</span><span style="color: #000000">a[i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">])&nbsp;<br /><img id="Codehighlighter1_221_312_Open_Image" onclick="this.style.display='none'; Codehighlighter1_221_312_Open_Text.style.display='none'; Codehighlighter1_221_312_Closed_Image.style.display='inline'; Codehighlighter1_221_312_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img style="display: none" id="Codehighlighter1_221_312_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_221_312_Closed_Text.style.display='none'; Codehighlighter1_221_312_Open_Image.style.display='inline'; Codehighlighter1_221_312_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_221_312_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_221_312_Open_Text"><span style="color: #000000">{&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp</span><span style="color: #000000">=</span><span style="color: #000000">a[i];&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i]</span><span style="color: #000000">=</span><span style="color: #000000">a[i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i</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">temp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tag</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">&nbsp;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000">==</span><span style="color: #000000">tag)<br /><img id="Codehighlighter1_341_417_Open_Image" onclick="this.style.display='none'; Codehighlighter1_341_417_Open_Text.style.display='none'; Codehighlighter1_341_417_Closed_Image.style.display='inline'; Codehighlighter1_341_417_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img style="display: none" id="Codehighlighter1_341_417_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_341_417_Closed_Text.style.display='none'; Codehighlighter1_341_417_Open_Image.style.display='inline'; Codehighlighter1_341_417_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_341_417_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_341_417_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;</span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">10</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%5d</span><span style="color: #000000">"</span><span style="color: #000000">,a[i]);<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span></div><br /><br />以下是选择排序法：<br /><br />每次外循环先将定位元素的小标i值记录到K，认为a[k]是最小值，第一轮比较时，若遇到比a[k]更小的数，则交换两数的下标，由下面的if语句进行交换处理。<br />这样第一轮就选出了最小的数，第二轮，同理选出次小的数排在最小的数后面。如果是输入10个数，那么进行9轮排序后就可完成整个排序过程。<br /><br /><br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #008000">//</span><span style="color: #008000">选择排序法</span><span style="color: #008000"><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,t,a[</span><span style="color: #000000">10</span><span style="color: #000000">],k;<br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">input&nbsp;10&nbsp;numbers:\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">10</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">a[i]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">9</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="color: #008000">//</span><span style="color: #008000">这里也要注意i=0;i&lt;9;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="color: #000000">=</span><span style="color: #000000">i;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">10</span><span style="color: #000000">;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(a[k]</span><span style="color: #000000">&gt;</span><span style="color: #000000">a[j])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="color: #000000">=</span><span style="color: #000000">j;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(k</span><span style="color: #000000">!=</span><span style="color: #000000">i)</span><span style="color: #008000">//</span><span style="color: #008000">如果k不等于i，改变了，则交换两个数的位置</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="color: #000000">=</span><span style="color: #000000">a[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i]</span><span style="color: #000000">=</span><span style="color: #000000">a[k];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[k]</span><span style="color: #000000">=</span><span style="color: #000000">t;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">10</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="color: #008000">//</span><span style="color: #008000">最后输出已经排好序的数</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%5d</span><span style="color: #000000">"</span><span style="color: #000000">,a[i]);<br />}<br /><br /><br /></span></div><br /><br />PS：大一刚开始接触这两个排序算法时，感觉有点乱，现在回过头来仔细看，思路清晰了不少。时刻回顾过去的知识，进行整理，再认识，很重要呀。:-D<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></div><img src ="http://www.cppblog.com/zhenglinbo/aggbug/156034.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2011-09-17 16:53 <a href="http://www.cppblog.com/zhenglinbo/archive/2011/09/17/156034.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>欧几里得算法</title><link>http://www.cppblog.com/zhenglinbo/archive/2011/09/17/155976.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Fri, 16 Sep 2011 18:00:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2011/09/17/155976.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/155976.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2011/09/17/155976.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/155976.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/155976.html</trackback:ping><description><![CDATA[辗转相除法，又名欧几里得算法，是求最大公约数的算法。<br />
<h3 class="headline-2 bk-sidecatalog-title"><a name="2_2"></a><span class="headline-content">原理及其详细证明</span></h3>　　设两数为a、b(b&lt;a)，用gcd(a,b)表示a，b的最大公约数，r=a mod b 为a除以b以后的余数，辗转相除法即是要证明gcd(a,b)=gcd(b,r)。 
<div class="spctrl"></div>　　第一步：令c=gcd(a,b)，则设a=mc，b=nc 
<div class="spctrl"></div>　　第二步：根据前提可知r =a-kb=mc-knc=(m-kn)c 
<div class="spctrl"></div>　　第三步：根据第二步结果可知c也是r的因数 
<div class="spctrl"></div>　　第四步：可以断定m-kn与n互素【否则，可设m-kn=xd，n=yd，(d&gt;1)，则m=kn+xd=kyd+xd=(ky+x)d，则a=mc=(ky+x)dc，b=nc=ycd，故a与b最大公约数成为cd，而非c】 
<div class="spctrl"></div>　　从而可知gcd(b,r)=c，继而gcd(a,b)=gcd(b,r)。 
<div class="spctrl"></div>　　证毕。<br /><br />用C表示则：<br /><br />&nbsp;&nbsp;&nbsp;&nbsp; int gcd(int a,int b) 
<div class="spctrl"></div>　　{ 
<div class="spctrl"></div>　　int temp; 
<div class="spctrl"></div>　　if(a&lt;b)/*交换两个数，使大数放在a上*/ 
<div class="spctrl"></div>　　{ 
<div class="spctrl"></div>　　temp=a; 
<div class="spctrl"></div>　　a=b; 
<div class="spctrl"></div>　　b=temp; 
<div class="spctrl"></div>　　} 
<div class="spctrl"></div>　　while(b!=0)/*利用辗除法，直到b为0为止*/ 
<div class="spctrl"></div>　　{ 
<div class="spctrl"></div>　　temp=a%b; 
<div class="spctrl"></div>　　a=b; 
<div class="spctrl"></div>　　b=temp; 
<div class="spctrl"></div>　　} 
<div class="spctrl"></div>　　return a; 
<div class="spctrl"></div>　　}<br /><br /><br /><br /><br /> <img src ="http://www.cppblog.com/zhenglinbo/aggbug/155976.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2011-09-17 02:00 <a href="http://www.cppblog.com/zhenglinbo/archive/2011/09/17/155976.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>计算两个正整数的最大公约数*</title><link>http://www.cppblog.com/zhenglinbo/archive/2011/09/16/155961.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Fri, 16 Sep 2011 10:35:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2011/09/16/155961.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/155961.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2011/09/16/155961.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/155961.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/155961.html</trackback:ping><description><![CDATA[算法分析：因为对于a与b两个数，当a&gt;b时，如果a中含有与b相同的公约数，则a-b也含有与b相同的公约数，反复循环，直到a和b相等为止，这时a或b就是它们的最大公约数。<br /><br />
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;Maxcommonfactor(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;b);<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a,b,c;<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">input&nbsp;two&nbsp;integer&nbsp;number:</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">a,</span><span style="color: #000000">&amp;</span><span style="color: #000000">b);<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="color: #000000">=</span><span style="color: #000000">Maxcommonfactor(a,b);<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(c</span><span style="color: #000000">!=-</span><span style="color: #000000">1</span><span style="color: #000000">)<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">The&nbsp;biggest&nbsp;common&nbsp;factor&nbsp;of&nbsp;%d&nbsp;and&nbsp;%d&nbsp;is&nbsp;%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,a,b,c);<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">The&nbsp;biggest&nbsp;common&nbsp;factor&nbsp;of&nbsp;%d&nbsp;and&nbsp;%d&nbsp;isn't&nbsp;exist\n</span><span style="color: #000000">"</span><span style="color: #000000">,a,b);<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;Maxcommonfactor(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;b)<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(a</span><span style="color: #000000">&lt;=</span><span style="color: #000000">0</span><span style="color: #000000">||</span><span style="color: #000000">b</span><span style="color: #000000">&lt;=</span><span style="color: #000000">0</span><span style="color: #000000">)<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(a</span><span style="color: #000000">!=</span><span style="color: #000000">b)<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(a</span><span style="color: #000000">&gt;</span><span style="color: #000000">b)<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a</span><span style="color: #000000">=</span><span style="color: #000000">a</span><span style="color: #000000">-</span><span style="color: #000000">b;<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(b</span><span style="color: #000000">&gt;</span><span style="color: #000000">a)<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b</span><span style="color: #000000">=</span><span style="color: #000000">b</span><span style="color: #000000">-</span><span style="color: #000000">a;<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">(a);</span><span style="color: #008000">//</span><span style="color: #008000">返回的值为最大公约数</span><span style="color: #008000"><br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">}<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"></span></div><br /><br />  <img src ="http://www.cppblog.com/zhenglinbo/aggbug/155961.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2011-09-16 18:35 <a href="http://www.cppblog.com/zhenglinbo/archive/2011/09/16/155961.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>判断一个数是否为素数</title><link>http://www.cppblog.com/zhenglinbo/archive/2011/09/16/155941.html</link><dc:creator>hoshelly</dc:creator><author>hoshelly</author><pubDate>Fri, 16 Sep 2011 06:31:00 GMT</pubDate><guid>http://www.cppblog.com/zhenglinbo/archive/2011/09/16/155941.html</guid><wfw:comment>http://www.cppblog.com/zhenglinbo/comments/155941.html</wfw:comment><comments>http://www.cppblog.com/zhenglinbo/archive/2011/09/16/155941.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenglinbo/comments/commentRss/155941.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenglinbo/services/trackbacks/155941.html</trackback:ping><description><![CDATA[<p>&nbsp;</p>
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">math.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;pdss(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;number);</span><span style="color: #008000">//</span><span style="color: #008000">定义一个函数，判断一个数是&nbsp;否为素数</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n;<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">input&nbsp;the&nbsp;integer:\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n);<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(pdss(n))</span><span style="color: #008000">//</span><span style="color: #008000">如果是1，输出素数</span><span style="color: #008000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d&nbsp;是素数.</span><span style="color: #000000">"</span><span style="color: #000000">,n);<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d&nbsp;不是素数.</span><span style="color: #000000">"</span><span style="color: #000000">,n);<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;pdss(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;number)<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i;<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(number</span><span style="color: #000000">&lt;=</span><span style="color: #000000">1</span><span style="color: #000000">)<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">2</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">sqrt(number);i</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="color: #008000">//</span><span style="color: #008000">检验一个数为素数时，用其平方根小或等于的数去整除所输入的数，即可判断。为什么？</span><span style="color: #008000"><br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(number</span><span style="color: #000000">%</span><span style="color: #000000">i</span><span style="color: #000000">==</span><span style="color: #000000">0</span><span style="color: #000000">)<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"></span></div>
<p>&nbsp;</p> <img src ="http://www.cppblog.com/zhenglinbo/aggbug/155941.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenglinbo/" target="_blank">hoshelly</a> 2011-09-16 14:31 <a href="http://www.cppblog.com/zhenglinbo/archive/2011/09/16/155941.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>