﻿<?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++博客-ivy-jie-文章分类-arithmetic</title><link>http://www.cppblog.com/ivy-jie/category/10522.html</link><description>progress ...</description><language>zh-cn</language><lastBuildDate>Sat, 23 May 2009 02:59:03 GMT</lastBuildDate><pubDate>Sat, 23 May 2009 02:59:03 GMT</pubDate><ttl>60</ttl><item><title>200511 重叠区间大小</title><link>http://www.cppblog.com/ivy-jie/articles/83630.html</link><dc:creator>ivy-jie</dc:creator><author>ivy-jie</author><pubDate>Thu, 21 May 2009 16:08:00 GMT</pubDate><guid>http://www.cppblog.com/ivy-jie/articles/83630.html</guid><wfw:comment>http://www.cppblog.com/ivy-jie/comments/83630.html</wfw:comment><comments>http://www.cppblog.com/ivy-jie/articles/83630.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ivy-jie/comments/commentRss/83630.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ivy-jie/services/trackbacks/83630.html</trackback:ping><description><![CDATA[1 题目描述：请编写程序，找出下面 &#8220; 输入数据及格式 &#8221; 中所描述的输入数据文件中最大重叠区间的大小。<br><br>&nbsp;&nbsp;&nbsp; 对一个正整数 n ，如果 n 在数据文件中某行的两个正整数（假设为 A 和 B ）之间，即 A&lt;=n&lt;=B 或 A&gt;=n&gt;=B ，则 n 属于该行；如果 n 同时&nbsp;&nbsp; 属于行 i 和 j ，则 i 和 j 有重叠区间；重叠区间的大小是同时属于行 i 和 j 的整数个数。&nbsp;<br>&nbsp;&nbsp;<br>&nbsp; &nbsp;例如，行（ 10 20 ）和（ 12 25 ）的重叠区间为 [12 20] ，其大小为 9 ；行（ 20 10 ）和（ 12 18 ）的重叠区间为 [10 12] ，其大小为 3 ；行 (20 10) 和（ 20 30 ）的重叠区间大小为 1 。<br><br>&nbsp; 输入数据：程序读入已被命名为 input.txt 的输入数据文本文件，该文件的行数在 1 到 1,000,000 之间，每行有用一个空格分隔的 2 个正整数，这 2 个正整数的大小次序随机，每个数都在 1 和 2^32-1 之间。（为便于调试，您可下载测试 input.txt 文件，实际运行时我们会使用不同内容的输入文件。）<br><br>&nbsp; 输出数据：在标准输出上打印出输入数据文件中最大重叠区间的大小，如果所有行都没有重叠区间，则输出 0 。 <br><br>&nbsp; 评分标准：程序输出结果必须正确，内存使用必须不超过 256MB ，程序的执行时间越快越好。 <br>2 #include &lt;iostream&gt;<br>#include &lt;string&gt;<br>#include &lt;cmath&gt;<br>#include &lt;algorithm&gt;<br>using namespace std;<br>typedef struct {int x,y,l;} Sec;<br>const int MAXN= 1000003;<br>int n,l;<br>Sec sec[MAXN];<br>int cmp(const void *a,const void *b)<br>{<br>&nbsp;return (*((Sec *)a)).x-(*((Sec *)b)).x;<br>}<br>int main()<br>{<br>&nbsp; int i,j,k,t;<br>&nbsp; freopen("input.txt","r",stdin);<br>&nbsp; n=0,l=0;<br>&nbsp; while(scanf("%d%d",&amp;sec[n].x,&amp;sec[n].y)!=EOF)<br>&nbsp; {<br>&nbsp;&nbsp; if(sec[n].x&gt;sec[n].y) t=sec[n].x,sec[n].x=sec[n].y,sec[n].y=t;<br>&nbsp;&nbsp; sec[n].l=sec[n].y-sec[n].x+1;<br>&nbsp;&nbsp; n++;<br>&nbsp; <br>&nbsp; }<br>&nbsp; qsort(sec,n,sizeof(Sec),cmp);<br>&nbsp; for(i=0;i&lt;n;i++)<br>&nbsp; {<br>&nbsp;&nbsp; if(sec[i].x == -1) continue;<br>&nbsp;&nbsp; for(j=i+1;j&lt;n;j++)<br>&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; if(sec[j].x &gt; sec[i].y) break; //肯定不会重叠<br>&nbsp;&nbsp;&nbsp; if(sec[j].x == -1 || sec[j].y&gt;sec[i].y) continue; <br>&nbsp;&nbsp;&nbsp; if(sec[j].l&gt;l) l=sec[j].l;//sec[i]完全包括sec[j]<br>&nbsp;&nbsp;&nbsp; sec[j].x = -1; //已经考虑过<br>&nbsp;&nbsp; }<br>&nbsp; }<br>&nbsp; for(i=0;i&lt;n;i++)<br>&nbsp; {<br>&nbsp;&nbsp;&nbsp; if(sec[i].x == -1) continue;<br>&nbsp;for(j=i+1;j&lt;n;j++)<br>&nbsp;{<br>&nbsp;&nbsp; if(sec[j].x &gt; sec[i].y) break;<br>&nbsp;&nbsp; if(sec[j].x == -1) continue;<br>&nbsp;&nbsp; if(sec[i].y - sec[j].x +1 &gt;l) l =sec[i].y-sec[j].x+1;<br>&nbsp;&nbsp; break;<br>&nbsp;}<br>&nbsp; }<br>&nbsp; cout&lt;&lt;l&lt;&lt;endl;<br>&nbsp; return 0;<br>}<br>
<img src ="http://www.cppblog.com/ivy-jie/aggbug/83630.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ivy-jie/" target="_blank">ivy-jie</a> 2009-05-22 00:08 <a href="http://www.cppblog.com/ivy-jie/articles/83630.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于汉字gbk编码</title><link>http://www.cppblog.com/ivy-jie/articles/83624.html</link><dc:creator>ivy-jie</dc:creator><author>ivy-jie</author><pubDate>Thu, 21 May 2009 15:31:00 GMT</pubDate><guid>http://www.cppblog.com/ivy-jie/articles/83624.html</guid><wfw:comment>http://www.cppblog.com/ivy-jie/comments/83624.html</wfw:comment><comments>http://www.cppblog.com/ivy-jie/articles/83624.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ivy-jie/comments/commentRss/83624.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ivy-jie/services/trackbacks/83624.html</trackback:ping><description><![CDATA[<p>汉字的GBK编码也是把一个汉字用两个字节来表示，其首字节对应0x81-0xFE(即129-224)，尾字节对应除掉(0x7F)的0x40-oxFE(即64-126和128-224)</p>
<p>我们一般判断一个字是否是汉字都是根据其首字节来判断，我们来看汉字的GBK编码首字节的起始编码0x81，其二进制即为0000 1000 0000 0000， 可见若一个字符的二进制逻辑与0x81之后为0时，该字符一定小于0x81，此时该字符必定不是汉字，反之，该字符应该是一个汉字的首字节。</p>
<p>结合以上原理就不难理解一个中英文混合字符串的截取的常用代码：</p>
<p>function gb_substr($str, $start, $len)<br>{<br>$s = '';<br>$j = 0;<br>for ($i=0; $i&lt;strlen($str); $i++) {<br>&nbsp;&nbsp; if (ord($str[$i]) &amp; 0x81 != 0) { //或者ord($str[$i]) &gt; '0x80'<br>&nbsp;&nbsp;&nbsp; $t = $str[$i].$str[$i+1];<br>&nbsp;&nbsp;&nbsp; if (($i &gt;= $start) &amp;&amp; ($i+1 &lt; $start+$len)) $s.= $t;<br>&nbsp;&nbsp;&nbsp; $i++;<br>&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp; $t = $str[$i];<br>&nbsp;&nbsp;&nbsp; if (($i &gt;= $start) &amp;&amp; ($i &lt; $start+$len)) $s.= $t;<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; if ($i &gt;= $start+$len) break;<br>}<br>return $s;<br>}</p>
<img src ="http://www.cppblog.com/ivy-jie/aggbug/83624.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ivy-jie/" target="_blank">ivy-jie</a> 2009-05-21 23:31 <a href="http://www.cppblog.com/ivy-jie/articles/83624.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>200813 传输规划</title><link>http://www.cppblog.com/ivy-jie/articles/83440.html</link><dc:creator>ivy-jie</dc:creator><author>ivy-jie</author><pubDate>Wed, 20 May 2009 01:34:00 GMT</pubDate><guid>http://www.cppblog.com/ivy-jie/articles/83440.html</guid><wfw:comment>http://www.cppblog.com/ivy-jie/comments/83440.html</wfw:comment><comments>http://www.cppblog.com/ivy-jie/articles/83440.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ivy-jie/comments/commentRss/83440.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ivy-jie/services/trackbacks/83440.html</trackback:ping><description><![CDATA[问题背景:<br>面对艰巨复杂的技术挑战，百度所崇尚的系统设计哲学是&#8220;简单可依赖&#8221;，而百度的工程师们正在互联网世界中实践着这种理念。这里正好有一个挑战，让作为百度之星的你小试牛刀：<br>在处理数以百亿计的网络信息的过程中，有一个很常见的问题：怎么样将一个集群上的信息以最低的成本传输到另外一个集群上？<br>&#8226; 数据源集群A有n台服务器，编号为1,2,&#8230;,n，i号服务器上待传输的数据量为Ai，单位是GB。 <br>&#8226; 目的地集群B有m台服务器，编号为1,2,&#8230;,m，j号服务器上的空闲容量为Bj，单位为GB。<br>&#8226; A集群的i号服务器上的每GB数据对于B的集群的j号服务器收益为Vi,j，从A集群的i号服务器向B集群的j号服务器传输1GB数据的开销为Ci,j。<br>你的任务是在保证A中的所有数据传输完毕的前提下，性价比V/C尽量高。其中V为所有数据在B集群上的价值之和，C为总开销。换句话说，若A集群的i号服务器向B集群的j号服务器发送了Ti,j个GB的数据，则性价比定义为：<br>输入格式<br>第1行两个整数n,m(1&lt;=n,m&lt;=50)，即集群A和B各自的服务器台数。<br>第2行包含n个不超过100的正整数A1,A2,&#8230;,An，即集群A中每台服务器的待传输数据量（单位：GB）。<br>第3行包含m个不超过100的正整数B1,B2,&#8230;,Bm，即集群B中每台服务器所能接受的最大数据量（单位：GB）。<br>第4~n+3行每行包含m个不超过100的非负整数Vi,j，表示集群A的i号服务器中每GB数据对于集群B中的j号服务器的价值。<br>第n+4~2n+3行每行包含m个不超过100的正整数Ci,j，表示集群A的i号服务器中每GB数据传输到集群B中的j号服务器所需要的开销。<br>输出格式<br>仅一行，为最大性价比。输出保留三位小数（四舍五入）。如果A的数据无法传输完毕，输出-1。<br>样例输入<br>22<br>12<br>21<br>110<br>75<br>61<br>32<br>样例输出<br>2.091<br>样例解释<br>一个方案是：<br>集群A的1号服务器把所有数据传输到集群B的1号服务器，价值11，开销6。<br>集群A的2号服务器把1GB数据传输到集群B的1号服务器，价值7，开销3，然后把剩下的1GB数据传输到集群B的2号服务器，价值5，开销2。<br>性价比：(11+7+5)/(6+3+2)=2.091<br>另一个方案是：<br>集群A的1号服务器把所有数据传输到集群B的2号服务器，价值0，开销1。<br>集群A的2号服务器把所有数据传输到集群B的1号服务器，价值14，开销6。<br>性价比：(0+14)/(1+6)=2。<br>第一种方案更优。<br>
<img src ="http://www.cppblog.com/ivy-jie/aggbug/83440.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ivy-jie/" target="_blank">ivy-jie</a> 2009-05-20 09:34 <a href="http://www.cppblog.com/ivy-jie/articles/83440.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>200812 圆内五角星</title><link>http://www.cppblog.com/ivy-jie/articles/83439.html</link><dc:creator>ivy-jie</dc:creator><author>ivy-jie</author><pubDate>Wed, 20 May 2009 01:22:00 GMT</pubDate><guid>http://www.cppblog.com/ivy-jie/articles/83439.html</guid><wfw:comment>http://www.cppblog.com/ivy-jie/comments/83439.html</wfw:comment><comments>http://www.cppblog.com/ivy-jie/articles/83439.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ivy-jie/comments/commentRss/83439.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ivy-jie/services/trackbacks/83439.html</trackback:ping><description><![CDATA[<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align=left><strong><span style="FONT-SIZE: 13.5pt; FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal">问题背景</span></strong><strong><span lang=EN-US style="FONT-SIZE: 13.5pt; FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></strong></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 12pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align=left><font size=3><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">如图，一个半径为</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">1</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">的圆周上有</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">5</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">个点。按角度制给出</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">5</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">个点的极角</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">A<sub>i</sub> (0&lt;=A<sub>i</sub>&lt;360, i=1..5)</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">。按下图的方法连成一个五角星</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">, </font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">计算圆被切割成的</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">11</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">个部分面积的方差。</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><o:p></o:p></span></font></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: center; mso-pagination: widow-orphan" align=center><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font size=3><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><o:p></o:p></font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><br></span><font size=3><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">具体地说</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">, </font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">假定</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">11</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">个区域的面积分别为</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">S<sub>1</sub>,S<sub>2</sub>, ..., S<sub>11</sub></font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">，那么面积的均值计算方法为：</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><o:p></o:p></span></font></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: center; mso-pagination: widow-orphan" align=center><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font size=3><font face="Times New Roman">M = (S<sub>1</sub>+S<sub>2</sub>+...+S<sub>11</sub> ) / 11<o:p></o:p></font></font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><br></span><font size=3><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">面积的方差计算方法为：</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><o:p></o:p></span></font></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: center; mso-pagination: widow-orphan" align=center><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font size=3><font face="Times New Roman">D = ((S<sub>1</sub>-M)<sup>2</sup> + (S<sub>2</sub>-M)<sup>2</sup> + ... + (S<sub>11</sub>-M)<sup>2</sup>) / 11<o:p></o:p></font></font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-outline-level: 3" align=left><strong><span style="FONT-SIZE: 13.5pt; FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal">输入格式</span></strong><strong><span lang=EN-US style="FONT-SIZE: 13.5pt; FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></strong></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><font size=3><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">输入仅一行，包含</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">5</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">个</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">[0,359]</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">内的互不相等的整数。</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman"> <o:p></o:p></font></span></font></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-outline-level: 3" align=left><strong><span style="FONT-SIZE: 13.5pt; FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal">输出格式</span></strong><strong><span lang=EN-US style="FONT-SIZE: 13.5pt; FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></strong></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><font size=3><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">输出仅一行，包含一个实数，即各部分面积的方差。输出保留小数点后</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">4</font></span><span style="FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal; mso-bidi-font-size: 10.5pt">位。</span><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman"> <o:p></o:p></font></span></font></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-outline-level: 3" align=left><strong><span style="FONT-SIZE: 13.5pt; FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal">样例输入</span></strong><strong><span lang=EN-US style="FONT-SIZE: 13.5pt; FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></strong></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font size=3><font face="Times New Roman">0 144 72 288 216 <o:p></o:p></font></font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-outline-level: 3" align=left><strong><span style="FONT-SIZE: 13.5pt; FONT-FAMILY: 宋体; mso-hansi-font-family: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: airal">样例输出</span></strong><strong><span lang=EN-US style="FONT-SIZE: 13.5pt; FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></strong></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3>0.0144</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3></font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3><strong>我对问题的分析</strong>：</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3>1、把极角排序（有利于后续计算），转化为直角坐标系坐标</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3>2、求五角星内交点五个</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3>3、求五个个星顶三角形面积</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3>4、求出&#8220;弓形-三角形&#8221;面积，然后以五个小扇形为未知量解一个五元线性方程组，求出五个小扇形面积</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3>5、由圆的面积减去求出的十个面积，得到重心的五边形面积</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3>6、根据方差公式求出答案</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3>【评价】这个方法基本属于按部就班的方法，因为没有发掘到圆内接五角星的特殊性质，所以并没有涉及到什么技巧。</font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3></font></span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 17.55pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" align=left><span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt"><font face="Times New Roman" size=3><strong>我的代码</strong></font></span></p>
<span lang=EN-US style="FONT-FAMILY: airal; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-bidi-font-size: 10.5pt">
<pre class=xml name="code">#include &lt;stdio.h&gt;<stdio.h></stdio.h>
#include &lt;stdlib.h&gt;<stdlib.h></stdlib.h>
#include &lt;math.h&gt;<math.h></math.h>
#define PI 3.1415926535898
typedef struct POINT
{
double x;
double y;
}Point,*lpPoint;//点坐标
struct COMB
{
POINT p;
POINT PL;//左交点
POINT PR;//右交点
};//五端点的附带结构
COMB c[5];//结构数组
int arg[5];//角度
double areaG[5];//弓形
double area[11];//11个部分面积
//选择排序
void sort(int arr[], int n)
{
int i, j, min, t;
for (i = 0; i &lt; n -1; i++)
{
min = i;
for (j = i + 1; j &lt; n; j++)
{
if (arr[min] &gt; arr[j])
{
min = j;
}
}
if (min != i)
{
t = arr[i];
arr[i] = arr[min];
arr[min] = t;
}
}
}
//两线段交点
POINT GetCrossPoint(POINT p1, POINT p2, POINT q1, POINT q2)
{
/*根据两点式化为标准式，进而求线性方程组*/
POINT crossPoint;
double tempLeft,tempRight;
//求x坐标
tempLeft = (q2.x - q1.x) * (p1.y - p2.y) - (p2.x - p1.x) * (q1.y - q2.y);
tempRight = (p1.y - q1.y) * (p2.x - p1.x) * (q2.x - q1.x) + q1.x * (q2.y - q1.y) * (p2.x - p1.x) - p1.x * (p2.y - p1.y) * (q2.x - q1.x);
crossPoint.x =tempRight /tempLeft;
//求y坐标
tempLeft = (p1.x - p2.x) * (q2.y - q1.y) - (p2.y - p1.y) * (q1.x - q2.x);
tempRight = p2.y * (p1.x - p2.x) * (q2.y - q1.y) + (q2.x- p2.x) * (q2.y - q1.y) * (p1.y - p2.y) - q2.y * (q1.x - q2.x) * (p2.y - p1.y);
crossPoint.y =tempRight / tempLeft;
return crossPoint;
}
//求所有交点
void cross()
{
int i;
for (i=0;i&lt;5;i++)
{
c[i].PL = GetCrossPoint(c[i].p,c[(i+3)%5].p,c[(i+1)%5].p,c[(i+4)%5].p);
c[(i+4)%5].PR = c[i].PL;
}
}
//void Helen();
//double SideLength(POINT X,POINT Y);
//点到点的距离
double SideLength( POINT X,POINT Y )
{
double r=sqrt(((X.x-Y.x)*(X.x-Y.x)+(X.y-Y.y)*(X.y-Y.y)));
return r;
}
//海伦公式求三角形面积
void Helen()
{
double a,b,d;	//三边长
double p;		//平均值
for (int i=0; i&lt;5; i++)
{
a=SideLength(c[i].p,c[i].PL);
b=SideLength(c[i].PR,c[i].PL);
d=SideLength(c[i].p,c[i].PR);
p=0.5*(a+b+d);
area[i]=sqrt((p*(p-a)*(p-b)*(p-d)));
}
}
//求五个弓形面积
void Arch()
{
double x;
double arc;
double rui;
for (int i=0;i&lt;5;i++)
{
x=0.5*SideLength(c[(i+4)%5].p,c[(i+1)%5].p);
rui = acos(x);
arc=PI-2.0*acos(x);
areaG[i]=0.5*arc-0.5*sin(arc);
}
}
//解方程求弧边的五块小扇形面积
void Equation()
{
double temp[5];
//弓形减去三角(方程右边)
for (int i=0; i&lt;5; i++)
{
temp[i] = areaG[i] - area[i];
}
//求解
area[5] = (temp[0]+temp[2]+temp[4]-temp[1]-temp[3])/2;
area[6] = (temp[0]+temp[1]+temp[3]-temp[2]-temp[4])/2;
area[7] = (temp[1]+temp[2]+temp[4]-temp[0]-temp[3])/2;
area[8] = (temp[0]+temp[2]+temp[3]-temp[1]-temp[4])/2;
area[9] = (temp[1]+temp[3]+temp[4]-temp[0]-temp[2])/2;
}
//求最后中间一块面积
void LastArea()
{
double plus(0.0);
for (int i=0;i&lt;10;i++)
{
plus += area[i];
}
area[10] = PI - plus;
}
int main(void)
{
int i;
for (i=0;i&lt;5;i++)
{
scanf("%d",&amp;arg[i]);
}
sort(arg,5);//排序
double d[5];
for (i=0;i&lt;5;i++)
{
d[i] = (double)(arg[i])*PI/180.0;
}
//点坐标
for (i=0;i&lt;5;i++)
{
c[i].p.x = cos(d[i]);
c[i].p.y = sin(d[i]);
}
//求所有交点
cross();
//----------求面积--------------
Helen();//五个三角形面积
Arch();//弓形面积
Equation();//解方程求弧边的五块小扇形面积
LastArea();//求最后中间一块面积
//对area[11]求方差
double aver = PI/11.0;
//printf("%.4f\n",aver);
double result = 0.0;//加和
for (i=0;i&lt;11;i++)
{
result = result + (area[i]-aver)*(area[i]-aver);
}
result = result/11.0;
result = (float)((int)(result*10000+0.5))/10000.0;//四舍五入取四位
printf("%.4f\n",result);
return 0;
}</pre>
</span>
<img src ="http://www.cppblog.com/ivy-jie/aggbug/83439.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ivy-jie/" target="_blank">ivy-jie</a> 2009-05-20 09:22 <a href="http://www.cppblog.com/ivy-jie/articles/83439.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>低频词的过滤</title><link>http://www.cppblog.com/ivy-jie/articles/83426.html</link><dc:creator>ivy-jie</dc:creator><author>ivy-jie</author><pubDate>Wed, 20 May 2009 00:46:00 GMT</pubDate><guid>http://www.cppblog.com/ivy-jie/articles/83426.html</guid><wfw:comment>http://www.cppblog.com/ivy-jie/comments/83426.html</wfw:comment><comments>http://www.cppblog.com/ivy-jie/articles/83426.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ivy-jie/comments/commentRss/83426.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ivy-jie/services/trackbacks/83426.html</trackback:ping><description><![CDATA[题目描述：请编写程序，从包含大量单词的文本中删除出现次数最少的单词。如果有多个单词都出现最少的次数，则将这些单词都删除。 &nbsp; &nbsp; &nbsp; 输入数据：程序读入已被命名为corpus.txt的一个大数据量的文本文件，该文件包含英文单词和中文单词，词与词之间以一个或多个whitespace（制表符、空格符和换行符一般被统称为&#8220;白字符&#8221;(whitespace &nbsp; characters)）分隔。（为便于调试，您可下载测试corpus.txt文件，实际运行时我们会使用不同内容的输入文件。） &nbsp; &nbsp; &nbsp; 输出数据：在标准输出上打印删除了corpus.txt中出现次数最少的单词之后的文本（词与词保持原来的顺序，仍以空格分隔）。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp; 评分标准： &nbsp; <br>&nbsp; 程序输出结果必须正确，内存使用越少越好，程序的执行时间越快越好&nbsp;&nbsp; <br>#include&lt;iostream&gt;<br>#include&lt;fstream&gt;<br>#include&lt;map&gt;<br>#include&lt;vector&gt;<br>#include&lt;string&gt;<br>#include&lt;cstring&gt;<br>#include&lt;cstdlib&gt;<br>#include&lt;iterator&gt;<br>#include&lt;algorithm&gt;<br>#include&lt;cctype&gt;<br>using namespace std;
<p>typedef map&lt;string,int&gt;::iterator mit;<br>typedef string::size_type sit;</p>
<p>int main()<br>{<br>map&lt;string,int&gt; words_count;<br>vector&lt;string&gt; sve;<br>string word;<br>string s=",!?.:""\n;'";<br>ifstream fin("E:\\corpus.txt");<br>if(!fin)<br>{<br>&nbsp;&nbsp; cerr&lt;&lt;"unable to open file"&lt;&lt;endl;<br>&nbsp;&nbsp; exit(0);<br>}<br>//读取并统计单词单词 <br>while(fin&gt;&gt;word)<br>&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; sit iter=word.find_first_of(s);<br>&nbsp;&nbsp;&nbsp; if(iter!=string::npos)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word=word.substr(0,iter-0); //处理标点符号<br>&nbsp;&nbsp;&nbsp; string temp(strlwr(const_cast&lt;char*&gt;(word.c_str())));<br>&nbsp;&nbsp;&nbsp; word=temp;<br>&nbsp;&nbsp;&nbsp; sve.push_back(word);<br>&nbsp;&nbsp;&nbsp; ++words_count[word]; <br>&nbsp;&nbsp; }<br>fin.close();<br>&nbsp;&nbsp; <br>//删除个数最少的单词<br>mit i=words_count.begin(); <br>int n=i-&gt;second;<br>for(;i!=words_count.end();++i)<br>&nbsp;&nbsp; if(i-&gt;second&lt;n) n=i-&gt;second;<br>for(mit i=words_count.begin();i!=words_count.end(); )<br>&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(i-&gt;second==n)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sve.erase(remove(sve.begin(),sve.end(),i-&gt;first),sve.end());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else ++i;<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>//输出到屏幕<br>copy(sve.begin(),sve.end(),ostream_iterator&lt;string&gt;(cout," ")); <br>cout&lt;&lt;endl;<br>&nbsp;&nbsp; <br>system("pause");<br>return 0;<br>}</p>
<img src ="http://www.cppblog.com/ivy-jie/aggbug/83426.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ivy-jie/" target="_blank">ivy-jie</a> 2009-05-20 08:46 <a href="http://www.cppblog.com/ivy-jie/articles/83426.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串替换</title><link>http://www.cppblog.com/ivy-jie/articles/83331.html</link><dc:creator>ivy-jie</dc:creator><author>ivy-jie</author><pubDate>Mon, 18 May 2009 16:19:00 GMT</pubDate><guid>http://www.cppblog.com/ivy-jie/articles/83331.html</guid><wfw:comment>http://www.cppblog.com/ivy-jie/comments/83331.html</wfw:comment><comments>http://www.cppblog.com/ivy-jie/articles/83331.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ivy-jie/comments/commentRss/83331.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ivy-jie/services/trackbacks/83331.html</trackback:ping><description><![CDATA[<p><font size=2>字符串替换<br></font><font size=2>题目描述：请编写程序，根据指定的对应关系，把一个文本中的字符串替换成另外的字符串。 </font></p>
<p><font size=2>输入数据：程序读入已被命名为text.txt和dict.txt的两个输入数据文本文件，text.txt为一个包含大量字符串（含中文）的文本，以whitespace为分隔符；dict.txt为表示字符串（s1）与字符串（s2）的对应关系的另一个文本（含中文），大约在1万行左右，每行两个字符串（即s1和s2），用一个\t或空格分隔。dict.txt中各行的s1没有排序，并有可能有重复，这时以最后出现的那次s1所对应的s2为准。text.txt和dict.txt中的每个字符串都可能包含除whitespace之外的任何字符。text.txt中的字符串必须和dict.txt中的某s1完全匹配才能被替换。（为便于调试，您可下载测试text.txt和dict.txt文件，实际运行时我们会使用不同内容的输入文件。）</font></p>
<p><font size=2>输出数据：在标准输出上打印text.txt被dict.txt替换后了的整个文本。</font></p>
<p><font size=2>评分标准：程序输出结果必须正确，内存使用越少越好，程序的执行时间越快越好。PS：<font size=3><span style="COLOR: red">c_str()返回一个它自己的使用内存的地址（一个C-STYLE的string——）</span> &nbsp; <br>&nbsp; 会自动给最后添上一个'\0'结束符号&nbsp;&nbsp; </font><br></font></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>#include&lt;iostream&gt;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>#include&lt;fstream&gt;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>#include&lt;string&gt;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>#include&lt;map&gt;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>using namespace std;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>map&lt;string,string&gt;dic;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>int main()</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>{</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>string s,a,b;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>char c;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>fstream file("dict.txt");</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>while (file&gt;&gt;a&gt;&gt;b)</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>dic[a]=b;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>file.close();</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: red">freopen("text.txt","r",stdin);</span></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>while (scanf("%c",&amp;c)+1)</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>{</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>if (c&amp;128)</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>{</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>s+=c;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>scanf("%c",&amp;c);</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>}</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>if ((c&amp;128)==0&amp;&amp;isspace(c))</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>{</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>if (!s.empty())</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><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;</span>if (dic[s]!="")</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>printf("%s",dic[s].c_str());</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><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;</span>else</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>printf("%s",s.c_str());</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>printf("%c",c);</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>s="";</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>}</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>else</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>s+=c;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>}</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>if (!s.empty())</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>if (dic[s]!="")</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>printf("%s",dic[s].c_str());</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>else</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>printf("%s",s.c_str());</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>return 0;</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt"><span><font size=2>}</font></span></p>
<img src ="http://www.cppblog.com/ivy-jie/aggbug/83331.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ivy-jie/" target="_blank">ivy-jie</a> 2009-05-19 00:19 <a href="http://www.cppblog.com/ivy-jie/articles/83331.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>