﻿<?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++博客-GLORY | 学习·记录-随笔分类-Algorithms</title><link>http://www.cppblog.com/meglory/category/13415.html</link><description>coding for life</description><language>zh-cn</language><lastBuildDate>Tue, 26 Apr 2011 16:22:42 GMT</lastBuildDate><pubDate>Tue, 26 Apr 2011 16:22:42 GMT</pubDate><ttl>60</ttl><item><title>UVA 10815</title><link>http://www.cppblog.com/meglory/archive/2011/04/03/143349.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Sun, 03 Apr 2011 09:05:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/04/03/143349.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/143349.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/04/03/143349.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/143349.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/143349.html</trackback:ping><description><![CDATA[<span style="FONT-FAMILY: Verdana">I hate this type of problems...<br><br>The key to this problem is the definition of a "word":<br><br>a word is defined as a consecutive sequence of alphabets, in upper and/or lower case<br><br>so, "won't" consists of two words, "won" and "t"......<br></span><br>
<img src ="http://www.cppblog.com/meglory/aggbug/143349.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-04-03 17:05 <a href="http://www.cppblog.com/meglory/archive/2011/04/03/143349.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 10878</title><link>http://www.cppblog.com/meglory/archive/2011/03/24/142673.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Thu, 24 Mar 2011 13:11:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/03/24/142673.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/142673.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/03/24/142673.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/142673.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/142673.html</trackback:ping><description><![CDATA[水题。<br><br>看到网上一个代码挺有意思的，要我肯定写不了这么简洁。exp的使用很精髓。<br><br>原文：<a href="http://www.cppblog.com/syhd142/articles/130347.html">http://www.cppblog.com/syhd142/articles/130347.html</a><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"><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</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&nbsp;</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: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;N&nbsp;20</span><span style="COLOR: #000000"><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"></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;exp[</span><span style="COLOR: #000000">11</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">128</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">64</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">32</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">freopen("out.txt",&nbsp;"w",&nbsp;stdout);</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;data[N];<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(gets(data))<br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;l&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;strlen(data);<br></span><span style="COLOR: #008080">16</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">(data[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">continue</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">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;l&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">18</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="COLOR: #008080">19</span>&nbsp;<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">(data[i]&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">.</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">continue</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;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(data[i]&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">o</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">)&nbsp;sum&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;exp[i];<br></span><span style="COLOR: #008080">21</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">22</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">%c</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;sum);<br></span><span style="COLOR: #008080">23</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #008080">24</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">25</span>&nbsp;<span style="COLOR: #000000">}</span></div>
<img src ="http://www.cppblog.com/meglory/aggbug/142673.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-03-24 21:11 <a href="http://www.cppblog.com/meglory/archive/2011/03/24/142673.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 10010</title><link>http://www.cppblog.com/meglory/archive/2011/03/15/141922.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Tue, 15 Mar 2011 15:16:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/03/15/141922.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/141922.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/03/15/141922.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/141922.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/141922.html</trackback:ping><description><![CDATA[I got a YES for the first submission.<br><span style="font-weight: bold;">I learned that devising and testing program on a paper is quite effective. </span>It's easier to edit and modify. Once you've finished coding, it's harder to modify your program.<br>And devising at least several test cases before submitting your program. It's quite often that the Sample is somewhat misleading, there used to be long way between YES and merely making your program pass the Sample test.<br><br>Most importantly, I can feel some improvement on my coding skills no matter how easy the problem I'm trying to solve seems to be. Just write your code, once day you will feel the improvements.<br> <img src ="http://www.cppblog.com/meglory/aggbug/141922.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-03-15 23:16 <a href="http://www.cppblog.com/meglory/archive/2011/03/15/141922.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 401</title><link>http://www.cppblog.com/meglory/archive/2011/03/14/141829.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Mon, 14 Mar 2011 15:08:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/03/14/141829.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/141829.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/03/14/141829.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/141829.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/141829.html</trackback:ping><description><![CDATA[<p>这个题目花了一些时间，总结一下因为下面两个原因：<br><br>1. <strong>还是读题的问题。</strong>题目有两个地方没有注意到：a.每个输出后面必须在输出一个空行；b. 题目关于0(zero)和O(letter)的问题我完全没有看懂就去写代码了。<br><br>为啥总是出现读题的问题呢？总结一条还是自己太急于去写代码了，不愿意把长长的说明读仔细。其实你在读题目时候因为几秒钟的疏忽可能在最后调式代码的时候要花去几个小时的时间，得不偿失啊。切记切记。<br><br>2.代码习惯。 我的一处出问题的代码 <strong>while(s[i++]==t[j--] &amp;&amp; j&gt;=i)</strong>，乍一看这么代码没啥问题，还省了循环里面的代码。但是，在同一行上面同时使用i++和i是非常差的代码习惯，而这个坏习惯到了我这里就成了一个bug。</p>
<img src ="http://www.cppblog.com/meglory/aggbug/141829.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-03-14 23:08 <a href="http://www.cppblog.com/meglory/archive/2011/03/14/141829.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 457 | 什么是Core Dump？</title><link>http://www.cppblog.com/meglory/archive/2011/03/14/141755.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Mon, 14 Mar 2011 00:24:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/03/14/141755.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/141755.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/03/14/141755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/141755.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/141755.html</trackback:ping><description><![CDATA[<p>UVA457是个水题，但是我还是碰到了一些小问题。<br><br>1.开了一个state[50][42]数组在main函数里面，结果提交之后发现runtime error，觉着是堆栈溢出了。突然想起来50*42&gt;2000，这种数组一定是开在main外面的，不然必然堆栈溢出。<br>2.是读题不仔细，当成了多重输入，搞了一个while(scanf(...))，结果超时了。第一次在UVA OJ上面超时。<br><br>但是这篇文章重点不是讲这个，而是讲我在运行过程中经常遇到的core dumped现象。我在网上找到一点资料，贴在下面：<br><br>原帖：<a href="http://blogold.chinaunix.net/u3/98822/showart_2093542.html">http://blogold.chinaunix.net/u3/98822/showart_2093542.html</a><br><br><br><strong>什么是Core Dump?<br></strong>Core的意思是内存, Dump的意思是扔出来, 堆出来.<br>开发和使用Unix程序时, 有时程序莫名其妙的down了, 却没有任何的提示(有时候会提示core dumped). 这时候可以查看一下有没有形如core.进程号的文件生成, 这个文件便是操作系统把程序down掉时的内存内容扔出来生成的, 它可以做为调试程序的参考.<br>core dump又叫核心转储, 当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump.<br><br><strong>如何使用core文件?</strong><br>gdb -c core文件路径 [应用程序的路径]<br>进去后输入where回车, 就可以显示程序在哪一行当掉的, 在哪个函数中.<br><br><strong>为什么没有core文件生成呢?</strong><br>有时候程序down了, 但是core文件却没有生成. core文件的生成跟你当前系统的环境设置有关系, 可以用下面的语句设置一下, 然后再运行程序便成生成core文件.<br>ulimit -c unlimited<br>【<span style="TEXT-DECORATION: underline">没有找到core文件，我们改改ulimit的设置，让它产生。1024是随便取的，要是core文件大于1024个块，就产生不出来了。）</span><br style="TEXT-DECORATION: underline"><span style="TEXT-DECORATION: underline">$ ulimit -c 1024 （转者注: 使用-c unlimited不限制core文件大小</span>】<br><br>core文件生成的位置一般于运行程序的路径相同, 文件名一般为core.进程号<br><br>4. 用gdb查看core文件:<br>下面我们可以在发生运行时信号引起的错误时发生core dump了.<br>发生core dump之后, 用gdb进行查看core文件的内容, 以定位文件中引发core dump的行.<br><span class=Code>gdb [exec file] [core file]</span><br>如:<br><span class=Code>gdb ./test test.core</span><br>在进入gdb后, 用bt命令查看backtrace以检查发生程序运行到哪里, 来定位core dump的文件-&gt;行.<br><br>===========================================================================<br></p>
<p align=left>造成程序core dump的原因很多，这里根据以往的经验总结一下：</p>
<p align=left>1 内存访问越界</p>
<p align=left>&nbsp; a) 由于使用错误的下标，导致数组访问越界</p>
<p align=left>&nbsp; b) 搜索字符串时，依靠字符串结束符来判断字符串是否结束，但是字符串没有正常的使用结束符</p>
<p align=left>&nbsp; c) 使用strcpy, strcat, sprintf, strcmp, strcasecmp等字符串操作函数，将目标字符串读/写爆。应该使用strncpy, strlcpy, strncat, strlcat, snprintf, strncmp, strncasecmp等函数防止读写越界。</p>
<p align=left>2 多线程程序使用了线程不安全的函数。</p>
<p align=left>应该使用下面这些可重入的函数，尤其注意红色标示出来的函数，它们很容易被用错：</p>
<p align=left>asctime_r(3c) gethostbyname_r(3n) getservbyname_r(3n) ctermid_r(3s) gethostent_r(3n) getservbyport_r(3n) ctime_r(3c) getlogin_r(3c) getservent_r(3n) fgetgrent_r(3c) getnetbyaddr_r(3n) getspent_r(3c) fgetpwent_r(3c) getnetbyname_r(3n) getspnam_r(3c) fgetspent_r(3c) getnetent_r(3n) gmtime_r(3c) gamma_r(3m) getnetgrent_r(3n) lgamma_r(3m) getauclassent_r(3) getprotobyname_r(3n) localtime_r(3c) getauclassnam_r(3) etprotobynumber_r(3n) nis_sperror_r(3n) getauevent_r(3) getprotoent_r(3n) rand_r(3c) getauevnam_r(3) getpwent_r(3c) readdir_r(3c) getauevnum_r(3) getpwnam_r(3c) strtok_r(3c) getgrent_r(3c) getpwuid_r(3c) tmpnam_r(3s) getgrgid_r(3c) getrpcbyname_r(3n) ttyname_r(3c) getgrnam_r(3c) getrpcbynumber_r(3n) gethostbyaddr_r(3n) getrpcent_r(3n)</p>
<p align=left>3 多线程读写的数据未加锁保护。</p>
<p align=left>对于会被多个线程同时访问的全局数据，应该注意加锁保护，否则很容易造成core dump</p>
<p align=left>4 非法指针</p>
<p align=left>&nbsp; a) 使用空指针</p>
<p align=left>&nbsp; b) 随意使用指针转换。一个指向一段内存的指针，除非确定这段内存原先就分配为某种结构或类型，或者这种结构或类型的数组，否则不要将它转换为这种结构或类型的指针，而应该将这段内存拷贝到一个这种结构或类型中，再访问这个结构或类型。这是因为如果这段内存的开始地址不是按照这种结构或类型对齐的，那么访问它时就很容易因为bus error而core dump.</p>
<p align=left>5 堆栈溢出</p>
<p>不要使用大的局部变量（因为局部变量都分配在栈上），这样容易造成堆栈溢出，破坏系统的栈和堆结构，导致出现莫名其妙的错误。 <br><br>-------<br>我自己程序core dumped就是因为第5个原因，堆栈溢出。我的局部数组开的过大，而局部变量分配在栈上，导致堆栈溢出。</p>
<img src ="http://www.cppblog.com/meglory/aggbug/141755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-03-14 08:24 <a href="http://www.cppblog.com/meglory/archive/2011/03/14/141755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 489</title><link>http://www.cppblog.com/meglory/archive/2011/03/12/141679.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Sat, 12 Mar 2011 15:07:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/03/12/141679.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/141679.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/03/12/141679.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/141679.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/141679.html</trackback:ping><description><![CDATA[<p>发帖前再次抱怨一下C++博客不支持最新的Firefox发帖的情况，让我辛辛苦苦码的字又找不到了。<br><br>这个题目折腾了我一段时间，是因为下面的情况自己没有考虑：<br><br>被猜测的字符串为：ab<br><br>1. 猜测abcdefghijklmn，结果为win<br>2.猜测cdefghijklmnab，结果为lose<br><br>这就要求处理的时候，<br><br>a. 如果被猜测串中所有字符都被猜中，应即时停止统计，跳出循环；<br>b.如果错误猜测的次数已经到达了7次，应该即时停止统计，跳出循环。<br><br>我之前的程序没有对上述情况处理，所以导致1情况输出失败，因为我把后面的错误次数都统计完之后，才跳出循环看是否错误超过7次，这么算当然就是lose了；而对于2情况，因为确实猜到了ab，在里面把字符统计之后再跳出来看被猜测穿，反而是所有字符到猜到了，所以win了。<br><br>被折腾了一下还是挺高兴，即时水题也有需要留心的地方。谨记。<br><br></p>
<img src ="http://www.cppblog.com/meglory/aggbug/141679.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-03-12 23:07 <a href="http://www.cppblog.com/meglory/archive/2011/03/12/141679.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 445 </title><link>http://www.cppblog.com/meglory/archive/2011/03/07/141315.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Mon, 07 Mar 2011 15:23:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2011/03/07/141315.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/141315.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2011/03/07/141315.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/141315.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/141315.html</trackback:ping><description><![CDATA[<p>考察<br><br>1a3x3<br>1a3a<br><br>就ok。</p>
<img src ="http://www.cppblog.com/meglory/aggbug/141315.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2011-03-07 23:23 <a href="http://www.cppblog.com/meglory/archive/2011/03/07/141315.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>养成自己的做题习惯 ｜ 随感</title><link>http://www.cppblog.com/meglory/archive/2010/07/16/120596.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Fri, 16 Jul 2010 15:07:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/07/16/120596.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/120596.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/07/16/120596.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/120596.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/120596.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;"><span style="font-size: 10pt;">2010-7-16<br>--------------------<br>在设计算法的时候，拿出一张纸自己列出来思路，并且实现算法。如果只是一味的空想容易作无用功，很久想不明白的东西画个图列表就可能清楚了。实现完成之后，我之前的习惯是过了Sample立马Submit，完全就是撞运气。这个时候应该继续在纸上跑一遍自己的程序，看是否每个边界条件都按照预期达到了，程序是不是走了之前预想的路径。</span><br></span><img src ="http://www.cppblog.com/meglory/aggbug/120596.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-07-16 23:07 <a href="http://www.cppblog.com/meglory/archive/2010/07/16/120596.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>刷题第一波</title><link>http://www.cppblog.com/meglory/archive/2010/07/16/120594.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Fri, 16 Jul 2010 15:01:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/07/16/120594.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/120594.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/07/16/120594.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/120594.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/120594.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">uva 490 转动字符串：Linux下面的Ctrl-D和Windows下面的Ctrl-Z一样可以模拟EOF.<br></span><img src ="http://www.cppblog.com/meglory/aggbug/120594.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-07-16 23:01 <a href="http://www.cppblog.com/meglory/archive/2010/07/16/120594.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法竞赛入门经典》学习笔记－枚举</title><link>http://www.cppblog.com/meglory/archive/2010/07/15/120483.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Thu, 15 Jul 2010 14:02:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/07/15/120483.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/120483.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/07/15/120483.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/120483.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/120483.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">除法<br><br>输入正整数n,按从小到大的顺序输出所有形如 abcde/fghij=n的表达式，其中a~j恰好为数字0~9的一个排列，2&lt;=n&lt;=79。<br>样例输入：<br>62<br>样例输出：<br>79546/01283=62<br>94736/01528=62<br><br>如何解决这个问题？<br><br>2010-7-16更新<br>上面的问题让我一下子想到了如何求一个序列的全排列的问题，于是顺着这个思路摸索了一下。Google到了一个算法。<br><br><span style="font-weight: bold;">递归</span><br></span>
<pre><span style="color: #008000;"><span style="color: #000000; font-size: 10pt;">例说明如何编写全排列的递归算法。<br>1、首先看最后两个数4, 5。 它们的全排列为4 5和5 4, 即以4开头的5的全排列和以5开头的4的全排列。由于一个数的全排列就是其本身，从而得到以上结果。<br>2、再看后三个数3, 4, 5。它们的全排列为3 4 5、3 5 4、 4 3 5、 4 5 3、 5 3 4、 5 4 3 六组数。<br>即以3开头的和4,5的全排列的组合、以4开头的和3,5的全排列的组合和以5开头的和3,4的全排列的组合.<br>从而可以推断，设一组数p = {r1, r2, r3, ... ,rn}, 全排列为perm(p)，pn = p - {rn}。<br>因此perm(p) = r1perm(p1), r2perm(p2), r3perm(p3), ... , rnperm(pn)。当n = 1时perm(p} = r1。<br>为了更容易理解，将整组数中的所有的数分别与第一个数交换，这样就总是在处理后n-1个数的全排列。</span></span></pre><img src ="http://www.cppblog.com/meglory/aggbug/120483.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-07-15 22:02 <a href="http://www.cppblog.com/meglory/archive/2010/07/15/120483.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>素数筛选法实现的问题</title><link>http://www.cppblog.com/meglory/archive/2010/07/14/120320.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Wed, 14 Jul 2010 05:22:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/07/14/120320.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/120320.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/07/14/120320.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/120320.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/120320.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">我实现了一个原生的版本，后来在Wikipedia看到欧拉的优化版本，但是不大知道如何实现。<br>原来的版本是直接判断是否是素数，所以空间占的比较少，时间比较多。打表法以后，应该是空间换时间的办法，但是为啥时间和空间都变大了呢？<br>应该是我实现的有问题，今天思考一下。<br><br><br></span>
<table class="a" width="100%" border="1" bordercolor="#ffffff" cellpadding="0" cellspacing="0">
    <tbody>
        <tr align="center">
            <td>7152474</td>
            <td><a href="http://acm.pku.edu.cn/JudgeOnline/userstatus?user_id=meGLORY">meGLORY</a></td>
            <td><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3006">3006</a></td>
            <td><font color="blue">Accepted</font></td>
            <td>4056K</td>
            <td>641MS</td>
            <td>C</td>
            <td>551B</td>
            <td>2010-07-14 11:35:03</td>
        </tr>
        <tr align="center">
            <td>7150446</td>
            <td><a href="http://acm.pku.edu.cn/JudgeOnline/userstatus?user_id=meGLORY">meGLORY</a></td>
            <td><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3006">3006</a></td>
            <td><font color="blue">Accepted</font></td>
            <td>156K</td>
            <td>250MS</td>
            <td>C</td>
            <td>405B</td>
            <td>2010-07-14 00:15:01</td>
        </tr>
    </tbody>
</table>
<br><span style="font-size: 10pt;">大概思考了一下，加了几个判断语句，时间成功的降到了100多一点了，内存还是4056K.</span><br>
<table class="a" width="100%" border="1" bordercolor="#ffffff" cellpadding="0" cellspacing="0">
    <tbody>
        <tr align="center">
            <td><a href="http://acm.pku.edu.cn/JudgeOnline/userstatus?user_id=meGLORY">meGLORY</a></td>
            <td><a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=3006">3006</a></td>
            <td><font color="blue">Accepted</font></td>
            <td>4056K</td>
            <td>125MS</td>
        </tr>
    </tbody>
</table>
<br>  <img src ="http://www.cppblog.com/meglory/aggbug/120320.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-07-14 13:22 <a href="http://www.cppblog.com/meglory/archive/2010/07/14/120320.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ 3006 Dirichlet's Theorem on Arithmetic Progressions</title><link>http://www.cppblog.com/meglory/archive/2010/07/12/120116.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Mon, 12 Jul 2010 05:07:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/07/12/120116.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/120116.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/07/12/120116.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/120116.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/120116.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">这两天都是水题，没有什么想法而言，就是为了熟悉一下感觉。我又给互联网上增加了不少垃圾日志。<br>昨晚出了点突发事件，原定的写程序事件被用来处理事情了。不过后来我倒是有点想明白了，题目只是很少一点时间在看题目，很忙的时候可以先花几分钟把题目弄明白了，然后在去忙别的事情，这种件的空隙还能想一想思路。不知POJ有什么办法可以把题目批量导出的，我就不用天天费劲上网读题目了，在单位也能抽空瞅两眼。<br><br>我坚持两件事情：1。决不直接贴</span><span style="font-size: 10pt;">题目</span><span style="font-size: 10pt;">的</span><span style="font-size: 10pt;">代码&nbsp; 2。每天都写题目<br><br>POJ 3006<br>计划打个表，然后直接搞，看了一下数据很小，应该没有什么问题。<br><br>在这里顺便题一下素数筛选法。<br>以下内容来自Wikipedia:</span><br><span style="font-size: 10pt;">
<p>To find all the prime numbers less than or equal to a given integer <em>n</em> by Eratosthenes' method:</p>
<ol>
    <li>Create a list of consecutive integers from two to <em>n</em>: (2, 3, 4, ..., <em>n</em>).</li>
    <li>Initially, let <em>p</em> equal 2, the first prime number.</li>
    <li>Strike from the list all multiples of <em>p</em> less than or equal to <em>n</em>. (<em>2p, 3p, 4p, etc.</em>)</li>
    <li>Find the first number remaining on the list after <em>p</em> (this number is the next prime); replace <em>p</em> with this number.</li>
    <li>Repeat steps 3 and 4 until <em>p</em><sup>2</sup> is greater than <em>n</em>.</li>
    <li>All the remaining numbers in the list are prime</li>
</ol>
</span><span style="font-size: 10pt;">后来，欧拉同学提出了另外一个办法，让每个数只要标记一次就ok<br><br></span>
<pre><span style="font-size: 8pt;">A) Start with all the natural numbers except '1' which is not a prime:<br><br>  2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...<br>  ^<br><br>B) The leftmost number is prime. Multiply each number in the list by this prime and<br>   then discard the products:<br><br> (4 6 8 10 12 14 16 18 20 22 24 26 28 30 ... )<br><br>  These are removed:<br>      4   6   8   10    12    14    16    18    20    22    24    26    28    30 <br><br>  These are left:<br>  2 3   5   7   9    11    13    15    17    19    21    23    25    27    29 ...<br>    ^<br><br>C) The number after the previous prime is also a prime. Multiply by it each number <br>   in the new list starting from this prime and then discard the products:<br><br>   (9   15  21  27   33    39    45    51    57    63    69    75    81    87 ...)<br><br>  These are removed:<br>                9                15                21                27 <br><br>  These are left:<br>  2 3   5   7        11    13          17    19          23    25          29 ...<br>        ^</span><br></pre>
<br><span style="font-size: 10pt;"><br><br><br>=================================================<br>2010-7-13 23:47<br>拆掉了自己的惠普V3159笔记本，心情一片大好，竟然装上以后运行正常了。出来混，迟早要还，之前一直对于拆机器的事情不感兴趣，现在可真是被逼得。<br>我终于可以去中关村赚取那90块的拆机费了: )<br>PS:今天中午修好了屋子里面的抽水马桶，心情一片大好。<br></span>      <img src ="http://www.cppblog.com/meglory/aggbug/120116.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-07-12 13:07 <a href="http://www.cppblog.com/meglory/archive/2010/07/12/120116.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ 1153 Integer Inquiry</title><link>http://www.cppblog.com/meglory/archive/2010/07/11/120056.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Sat, 10 Jul 2010 17:47:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/07/11/120056.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/120056.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/07/11/120056.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/120056.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/120056.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">非常简单的题目，没有方法可言。<br>1。之前忘记拿char读入的，所以直接把int和char相加所以结果一直很奇怪。<br>2。题目的数字会出现以0开头的非零数字，这个当初读题完全没有想到。但是看到discuss之后回去读题，发现题目确实题目没有说不可以。这个还是自己没有考虑周全。<br><br><br></span><img src ="http://www.cppblog.com/meglory/aggbug/120056.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-07-11 01:47 <a href="http://www.cppblog.com/meglory/archive/2010/07/11/120056.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ 2262 Goldbach's Conjecture</title><link>http://www.cppblog.com/meglory/archive/2010/07/09/119806.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Thu, 08 Jul 2010 16:26:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/07/09/119806.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/119806.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/07/09/119806.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/119806.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/119806.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;"><span style="font-size: 10pt;">1.WA数次，因为边界的问题，把2也当成候选数了，没有留心two odd primes.<br>2.gcc编译math.h时，需要-lm.<br>3.如何从vim里面把代码拷出来提交到POJ的那个框框里面？我搞了半天没有解决<br><br>附：<br></span></span>
<p>gcc -l参数 <br>
</p>
<p>-l参数就是用来指定程序要链接的库，-l参数紧接着就是库名，那么库名跟真正的库文件名有什么关系呢？就拿数学库来说，他的库名是m，他的库文件名是libm.so，很容易看出，把库文件名的头lib和尾.so去掉就是库名了</p>
<br>   <img src ="http://www.cppblog.com/meglory/aggbug/119806.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-07-09 00:26 <a href="http://www.cppblog.com/meglory/archive/2010/07/09/119806.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 494的读题错误</title><link>http://www.cppblog.com/meglory/archive/2010/04/04/111600.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Sun, 04 Apr 2010 10:21:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/04/04/111600.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/111600.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/04/04/111600.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/111600.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/111600.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">
<p style="font-family: 宋体;">这道题是很简单的一个题目。我的题目理解稍微有点问题，所以花了一些时间才搞清楚。</p>
<p style="font-family: 宋体;">还碰巧发现了一个讨论uva的论坛，很好很强大。就是里面一位老大的一句话让我豁然开朗。<br>关键在于题目里面关于word的定义。</p>
<p style="font-family: 宋体;">A &#8220;word&#8221; is defined as a consecutive sequence of letters (upper and/or lower case).
</p>
<p style="font-family: 宋体;">Your program should output a word count for each line of input. Each word count should be printed on a separate line.
</p>
<p style="font-family: 宋体;">一开始我以为直接判断scanf(&#8220;%s&#8221;,s)的次数就ok。后来不停地wa。发现题目里面怎么连did!也算作一个单词，不是连续的字母吗？
</p>
<p style="font-family: 宋体;">I did! I did! I did taw a putty tat.</p>
<p style="font-family: 宋体;">比如这个字符串用我的理解就是应该是八个word。后来想到，是一开始用读入字符传的思维导致了我的错误。这个题目是一个字符一个字符的处理的，并不是一下读进来一个字符串然后一个个的判断。</p>
<p style="font-family: 宋体;">想到这里，我豁然开朗。</p>
</span><img src ="http://www.cppblog.com/meglory/aggbug/111600.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-04-04 18:21 <a href="http://www.cppblog.com/meglory/archive/2010/04/04/111600.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法竞赛入门经典》-读书笔记-蛇形填数</title><link>http://www.cppblog.com/meglory/archive/2010/03/29/110937.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Mon, 29 Mar 2010 14:53:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/03/29/110937.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/110937.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/03/29/110937.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/110937.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/110937.html</trackback:ping><description><![CDATA[<p>一开始在家里面有了一点思路，用两重循环。后来真正开始在纸上写代码的时候发现只要一重循环即可。&nbsp;</p>
<div class=cnblogs_code>
<div><!--<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><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a[</span><span style="COLOR: #800080">10</span><span style="COLOR: #000000">][</span><span style="COLOR: #800080">10</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dx[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;dy[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">-</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;value</span><span style="COLOR: #000000">=</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,n,x,y;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">%d</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<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: #800080">1</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>&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: #800080">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">初始位置为(1,n),初始值为1</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;x</span><span style="COLOR: #000000">=</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;a[x][y]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">循环结束的条件为n*n个值已经赋完</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(value</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">n)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;x</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">dx[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">dy[i];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;x</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;y</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;y</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;a[x][y]</span><span style="COLOR: #000000">==</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[x][y]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">dx[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">dy[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">+</span><span style="COLOR: #800080">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">%</span><span style="COLOR: #800080">4</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">\nvalue=%d\n</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">,value);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<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: #800080">1</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>&nbsp;&nbsp;&nbsp;&nbsp;{<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: #800080">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">n;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">%d&nbsp;</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">,a[i][j]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">;<br><br>}</span></div>
</div>
<p>&nbsp;后来看到书上的sample代码，我觉着我的代码显然要好很多:-)</p>
<img src ="http://www.cppblog.com/meglory/aggbug/110937.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-03-29 22:53 <a href="http://www.cppblog.com/meglory/archive/2010/03/29/110937.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《算法竞赛入门经典》-读书笔记-排列&amp;amp;分数化小数</title><link>http://www.cppblog.com/meglory/archive/2010/03/29/110938.html</link><dc:creator>meglory</dc:creator><author>meglory</author><pubDate>Mon, 29 Mar 2010 14:53:00 GMT</pubDate><guid>http://www.cppblog.com/meglory/archive/2010/03/29/110938.html</guid><wfw:comment>http://www.cppblog.com/meglory/comments/110938.html</wfw:comment><comments>http://www.cppblog.com/meglory/archive/2010/03/29/110938.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/meglory/comments/commentRss/110938.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/meglory/services/trackbacks/110938.html</trackback:ping><description><![CDATA[<p><font color="#000000"><font face="宋体"><span style="font-family: verdana; color: #0000ff; font-size: 10pt">用1,2,3,···,9 组成 3 个三位数 </span><span style="font-family: verdana; color: #0000ff; font-size: 10pt">abc</span><span style="font-family: verdana; color: #0000ff; font-size: 10pt">,</span><span style="font-family: verdana; color: #0000ff; font-size: 10pt">def</span><span style="font-family: verdana; color: #0000ff; font-size: 10pt">和</span><span style="font-family: verdana; color: #0000ff; font-size: 10pt">ghi,</span><span style="font-family: verdana; color: #0000ff; font-size: 10pt">每个数字恰好使用一次，要求abc:def:ghi=1:2:3。输出所有解。</span></font></font></p> <p><span style="font-family: verdana; color: #0000ff; font-size: 10pt"><font color="#000000" face="宋体">题目特别写出了提示：不必太动脑筋。(其实这句话让我现在都不清楚我的解法是不是符合要求......) </font></span></p> <div class="cnblogs_code"> <div><!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><font size="2"><font face="宋体"><span style="color: #008080">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></font></font><span style="color: #000000"><br></span><font size="2"><font face="宋体"><span style="color: #008080">2</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdlib.h</span><span style="color: #000000">&gt;</span></font></font><span style="color: #000000"><br></span><font size="2"><font face="宋体"><span style="color: #008080">3</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span></font></font><font size="2"><font face="宋体"><span style="color: #000000"> main()<br></span><span style="color: #008080">4</span>&nbsp;</font></font><font size="2"><font face="宋体"><span style="color: #000000">{<br></span><span style="color: #008080">5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">int</span></font></font><font size="2"><font face="宋体"><span style="color: #000000"> num;<br></span><span style="color: #008080">6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">char</span></font></font><font size="2"><font face="宋体"><span style="color: #000000"> chr;<br></span><span style="color: #008080">7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">char</span><span style="color: #000000"> a[</span><span style="color: #800080">10</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">];<br></span><span style="color: #008080">8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">char</span><span style="color: #000000"> temp[</span><span style="color: #800080">3</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">];<br></span><span style="color: #008080">9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">char</span></font></font><font size="2"><font face="宋体"><span style="color: #000000"> pos;<br></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">//</span><span style="color: #008000">num*3&lt;=987 所以 num&lt;=329</span></font></font><span style="color: #008000"><br></span><font size="2"><font face="宋体"><span style="color: #008080">11</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">for</span><span style="color: #000000">(num</span><span style="color: #000000">=</span><span style="color: #800080">123</span><span style="color: #000000">;num</span><span style="color: #000000">&lt;=</span><span style="color: #800080">329</span><span style="color: #000000">;num</span><span style="color: #000000">++</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">)<br></span><span style="color: #008080">12</span>&nbsp;</font></font><font size="2"><font face="宋体"><span style="color: #000000">&nbsp;&nbsp;&nbsp; {<br></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; itoa(num,temp,</span><span style="color: #800080">10</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">);<br></span><span style="color: #008080">14</span>&nbsp;</font></font><font size="2"><font face="宋体"><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcpy(a,temp);<br></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; itoa(num</span><span style="color: #000000">*</span><span style="color: #800080">2</span><span style="color: #000000">,temp,</span><span style="color: #800080">10</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">);<br></span><span style="color: #008080">16</span>&nbsp;</font></font><font size="2"><font face="宋体"><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcat(a,temp);<br></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; itoa(num</span><span style="color: #000000">*</span><span style="color: #800080">3</span><span style="color: #000000">,temp,</span><span style="color: #800080">10</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">);<br></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcat(a,temp);</span><span style="color: #008000">//</span><span style="color: #008000">a保存了num以及num*2和num*3组成的字符串</span></font></font><span style="color: #008000"><br></span><font size="2"><font face="宋体"><span style="color: #008080">19</span>&nbsp;<span style="color: #008000"></span></font></font><span style="color: #000000"><br></span><font size="2"><font face="宋体"><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">for</span><span style="color: #000000">(chr</span><span style="color: #000000">=</span><span style="color: #800000">'</span><span style="color: #800000">1</span><span style="color: #800000">'</span><span style="color: #000000">;chr</span><span style="color: #000000">&lt;=</span><span style="color: #800000">'</span><span style="color: #800000">9</span><span style="color: #800000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000"> (pos</span><span style="color: #000000">=</span><span style="color: #000000">strchr(a,chr))</span><span style="color: #000000">!=</span><span style="color: #000000">NULL;chr</span><span style="color: #000000">++</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">)<br></span><span style="color: #008080">21</span>&nbsp;</font></font><font size="2"><font face="宋体"><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;<br></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">if</span><span style="color: #000000">(pos</span><span style="color: #000000">!=</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">NULL)<br></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(</span><span style="color: #800000">"</span><span style="color: #800000">%d:%d:%d=1:2:3\n</span><span style="color: #800000">"</span><span style="color: #000000">,num,num</span><span style="color: #000000">*</span><span style="color: #800080">2</span><span style="color: #000000">,num</span><span style="color: #000000">*</span><span style="color: #800080">3</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br></span><span style="color: #008080">24</span>&nbsp;</font></font><font size="2"><font face="宋体"><span style="color: #000000">&nbsp;&nbsp;&nbsp; }<br></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">0</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">;<br></span><span style="color: #008080">26</span>&nbsp;</font></font><font size="2"><font face="宋体"><span style="color: #000000">}<br></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000"></span></font></font></div></div> <p><font face="宋体"><span style="font-size: 10pt">&nbsp;</span><span style="font-size: 10pt">今天继续读到了分数化小数题目，感觉挺简单的一个问题。实际藏着一个我并不知道的printf妙用。</span></font></p> <p><font face="宋体"><span style="font-size: 10pt">　　</span><span style="color: #0000ff; font-size: 10pt">输入正整数a,b,c,输出a/b的小数形式，精确到小数点后c位。a,b&lt;=10^6,c&lt;=100.例如a=1,b=6,c=4时应输出0.1667.</span></font></p> <p><span style="font-size: 10pt"><font face="宋体">printf的特殊用法，<span style="line-height: 26px; font-family: arial, helvetica, georgia, sans-serif; color: #212121">对于m.n的格式可以用如下方法表示</span></font></span></p><span style="line-height: 26px; font-family: arial, helvetica, georgia, sans-serif; color: #212121; font-size: 10pt"><font face="宋体">&nbsp;&nbsp;&nbsp; char ch[20];<br></font></span><font face="宋体"><span style="line-height: 26px; font-family: arial, helvetica, georgia, sans-serif; color: #212121; font-size: 10pt">&nbsp;&nbsp;&nbsp; printf("%*.*s\n",m,n,ch);<br></span><span style="line-height: 26px; font-family: arial, helvetica, georgia, sans-serif; color: #212121; font-size: 10pt">&nbsp;&nbsp;&nbsp; 前边的*定义的是总的宽度，后边的定义的是输出的个数。分别对应外面的参数m和n 。 这种方法的好处是可以在语句之外对参数m和n赋值，从而控制输出格式。</span><font size="2"> </font></font> <div class="cnblogs_code"> <div><!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><font size="2"><font face="宋体"><span style="color: #008080">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></font></font><span style="color: #000000"><br></span><font size="2"><font face="宋体"><span style="color: #008080">2</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span></font></font><font size="2"><font face="宋体"><span style="color: #000000"> main()<br></span><span style="color: #008080">3</span>&nbsp;</font></font><font size="2"><font face="宋体"><span style="color: #000000">{<br></span><span style="color: #008080">4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">int</span></font></font><font size="2"><font face="宋体"><span style="color: #000000"> a,b,c;<br></span><span style="color: #008080">5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; scanf(</span><span style="color: #800000">"</span><span style="color: #800000">%d %d %d</span><span style="color: #800000">"</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,</span><span style="color: #000000">&amp;</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">c);<br></span><span style="color: #008080">6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; printf(</span><span style="color: #800000">"</span><span style="color: #800000">%.*lf</span><span style="color: #800000">"</span><span style="color: #000000">,c,(</span><span style="color: #0000ff">double</span><span style="color: #000000">)a</span><span style="color: #000000">/</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">b);<br></span><span style="color: #008080">7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">0</span></font></font><font size="2"><font face="宋体"><span style="color: #000000">;<br></span><span style="color: #008080">8</span>&nbsp;<span style="color: #000000">} </span></font></font></div></div> <p><font size="2" face="宋体"></font></p> <p><font size="2" face="宋体"></font></p><img src ="http://www.cppblog.com/meglory/aggbug/110938.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/meglory/" target="_blank">meglory</a> 2010-03-29 22:53 <a href="http://www.cppblog.com/meglory/archive/2010/03/29/110938.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>